feat(url): add logout url constructor

This commit is contained in:
Trong Huu Nguyen
2023-05-03 09:07:03 +02:00
parent 6ebc95a8e0
commit b0bb1aa8ea
2 changed files with 54 additions and 0 deletions

View File

@@ -40,6 +40,18 @@ func LoginRelative(prefix, redirect string) string {
return Login(u, redirect)
}
// Logout constructs a URL string that points to the logout path for the given target URL.
// The given redirect string should point to the location to be redirected to after logout.
func Logout(target *url.URL, redirect string) string {
u := target.JoinPath(paths.OAuth2, paths.Logout)
v := u.Query()
v.Set(RedirectQueryParameter, redirect)
u.RawQuery = v.Encode()
return u.String()
}
func LoginCallback(r *http.Request) (string, error) {
return makeCallbackURL(r, paths.LoginCallback)
}

View File

@@ -91,6 +91,48 @@ func TestLoginRelative(t *testing.T) {
}
}
func TestLogout(t *testing.T) {
for _, test := range []struct {
name string
targetURL string
redirectTarget string
want string
}{
{
name: "root path",
targetURL: "https://sso.wonderwall",
redirectTarget: "https://test.example.com?some=param&other=param2",
want: "https://sso.wonderwall/oauth2/logout?redirect=https%3A%2F%2Ftest.example.com%3Fsome%3Dparam%26other%3Dparam2",
},
{
name: "with prefix",
targetURL: "https://sso.wonderwall/path",
redirectTarget: "https://test.example.com?some=param&other=param2",
want: "https://sso.wonderwall/path/oauth2/logout?redirect=https%3A%2F%2Ftest.example.com%3Fsome%3Dparam%26other%3Dparam2",
},
{
name: "we need to go deeper",
targetURL: "https://sso.wonderwall/deeper/path",
redirectTarget: "https://test.example.com?some=param&other=param2",
want: "https://sso.wonderwall/deeper/path/oauth2/logout?redirect=https%3A%2F%2Ftest.example.com%3Fsome%3Dparam%26other%3Dparam2",
},
{
name: "relative redirect target",
targetURL: "https://sso.wonderwall",
redirectTarget: "/path?some=param&other=param2",
want: "https://sso.wonderwall/oauth2/logout?redirect=%2Fpath%3Fsome%3Dparam%26other%3Dparam2",
},
} {
t.Run(test.name, func(t *testing.T) {
targetURL, err := url.Parse(test.targetURL)
assert.NoError(t, err)
logoutUrl := urlpkg.Logout(targetURL, test.redirectTarget)
assert.Equal(t, test.want, logoutUrl)
})
}
}
func TestLoginCallback(t *testing.T) {
cfg := mock.Config()
cfg.Ingresses = []string{