mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-07 06:03:09 -06:00
Fix callback URL building in SSOCallback.
It ended up without scheme and host. Do what SSORedirect does instead.
This commit is contained in:
parent
618e18f259
commit
43bac75c5f
|
|
@ -577,7 +577,7 @@ func Setup(
|
||||||
|
|
||||||
v3mux.Handle("/login/sso/callback",
|
v3mux.Handle("/login/sso/callback",
|
||||||
httputil.MakeExternalAPI("login", func(req *http.Request) util.JSONResponse {
|
httputil.MakeExternalAPI("login", func(req *http.Request) util.JSONResponse {
|
||||||
return SSOCallback(req, userAPI, ssoAuthenticator, cfg.Matrix.ServerName)
|
return SSOCallback(req, userAPI, ssoAuthenticator, &cfg.Login.SSO, cfg.Matrix.ServerName)
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ func SSORedirect(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callbackURL, err := buildCallbackURLFromRedirect(cfg, req)
|
callbackURL, err := buildCallbackURLFromOther(cfg, req, "/login/sso/redirect")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.GetLogger(ctx).WithError(err).Error("Failed to build callback URL")
|
util.GetLogger(ctx).WithError(err).Error("Failed to build callback URL")
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
|
|
@ -107,9 +107,9 @@ func SSORedirect(
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildCallbackURLFromRedirect builds a callback URL from a redirect
|
// buildCallbackURLFromOther builds a callback URL from another SSO
|
||||||
// request and configuration.
|
// request and configuration.
|
||||||
func buildCallbackURLFromRedirect(cfg *config.SSO, req *http.Request) (*url.URL, error) {
|
func buildCallbackURLFromOther(cfg *config.SSO, req *http.Request, expectedPath string) (*url.URL, error) {
|
||||||
u := &url.URL{
|
u := &url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
User: req.URL.User,
|
User: req.URL.User,
|
||||||
|
|
@ -122,10 +122,9 @@ func buildCallbackURLFromRedirect(cfg *config.SSO, req *http.Request) (*url.URL,
|
||||||
|
|
||||||
// Find the v3mux base, handling both `redirect` and
|
// Find the v3mux base, handling both `redirect` and
|
||||||
// `redirect/{idp}` and not hard-coding the Matrix version.
|
// `redirect/{idp}` and not hard-coding the Matrix version.
|
||||||
const redirectPath = "/login/sso/redirect"
|
i := strings.Index(u.Path, expectedPath)
|
||||||
i := strings.Index(u.Path, redirectPath)
|
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return nil, fmt.Errorf("cannot find %q to replace in URL %q", redirectPath, u.Path)
|
return nil, fmt.Errorf("cannot find %q to replace in URL %q", expectedPath, u.Path)
|
||||||
}
|
}
|
||||||
u.Path = u.Path[:i] + "/login/sso/callback"
|
u.Path = u.Path[:i] + "/login/sso/callback"
|
||||||
|
|
||||||
|
|
@ -142,6 +141,7 @@ func SSOCallback(
|
||||||
req *http.Request,
|
req *http.Request,
|
||||||
userAPI userAPIForSSO,
|
userAPI userAPIForSSO,
|
||||||
auth *sso.Authenticator,
|
auth *sso.Authenticator,
|
||||||
|
cfg *config.SSO,
|
||||||
serverName gomatrixserverlib.ServerName,
|
serverName gomatrixserverlib.ServerName,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
if auth == nil {
|
if auth == nil {
|
||||||
|
|
@ -177,14 +177,18 @@ func SSOCallback(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callbackURL := &url.URL{
|
callbackURL, err := buildCallbackURLFromOther(cfg, req, "/login/sso/callback")
|
||||||
Scheme: req.URL.Scheme,
|
if err != nil {
|
||||||
Host: req.URL.Host,
|
util.GetLogger(ctx).WithError(err).Error("Failed to build callback URL")
|
||||||
Path: req.URL.Path,
|
return util.JSONResponse{
|
||||||
RawQuery: url.Values{
|
Code: http.StatusInternalServerError,
|
||||||
"provider": []string{idpID},
|
JSON: err,
|
||||||
}.Encode(),
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callbackURL = callbackURL.ResolveReference(&url.URL{
|
||||||
|
RawQuery: url.Values{"provider": []string{idpID}}.Encode(),
|
||||||
|
})
|
||||||
result, err := auth.ProcessCallback(ctx, idpID, callbackURL.String(), nonce.Value, query)
|
result, err := auth.ProcessCallback(ctx, idpID, callbackURL.String(), nonce.Value, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.GetLogger(ctx).WithError(err).Error("Failed to process callback")
|
util.GetLogger(ctx).WithError(err).Error("Failed to process callback")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue