Merge pull request #1572 from hmoffatt/oidc-path

Fix base URI not included in generated OpenID Connect URLs (#1524)
This commit is contained in:
Denis Gukov 2024-11-25 11:28:58 +05:00 committed by GitHub
commit 200c860103
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 11 deletions

View File

@ -423,7 +423,8 @@ func oidcLogin(w http.ResponseWriter, r *http.Request) {
_, oauth, err := getOidcProvider(pid, ctx, redirectPath) _, oauth, err := getOidcProvider(pid, ctx, redirectPath)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) loginURL, _ := url.JoinPath(util.Config.WebHost, "auth/login")
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
state := generateStateOauthCookie(w) state := generateStateOauthCookie(w)
@ -570,14 +571,16 @@ func getSecretFromFile(source string) (string, error) {
func oidcRedirect(w http.ResponseWriter, r *http.Request) { func oidcRedirect(w http.ResponseWriter, r *http.Request) {
pid := mux.Vars(r)["provider"] pid := mux.Vars(r)["provider"]
oauthState, err := r.Cookie("oauthstate") oauthState, err := r.Cookie("oauthstate")
loginURL, _ := url.JoinPath(util.Config.WebHost, "auth/login")
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
if r.FormValue("state") != oauthState.Value { if r.FormValue("state") != oauthState.Value {
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
@ -586,14 +589,14 @@ func oidcRedirect(w http.ResponseWriter, r *http.Request) {
_oidc, oauth, err := getOidcProvider(pid, ctx, r.URL.Path) _oidc, oauth, err := getOidcProvider(pid, ctx, r.URL.Path)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
provider, ok := util.Config.OidcProviders[pid] provider, ok := util.Config.OidcProviders[pid]
if !ok { if !ok {
log.Error(fmt.Errorf("no such provider: %s", pid)) log.Error(fmt.Errorf("no such provider: %s", pid))
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
@ -604,7 +607,7 @@ func oidcRedirect(w http.ResponseWriter, r *http.Request) {
oauth2Token, err := oauth.Exchange(ctx, code) oauth2Token, err := oauth.Exchange(ctx, code)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
@ -643,7 +646,7 @@ func oidcRedirect(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
@ -658,14 +661,14 @@ func oidcRedirect(w http.ResponseWriter, r *http.Request) {
user, err = helpers.Store(r).CreateUserWithoutPassword(user) user, err = helpers.Store(r).CreateUserWithoutPassword(user)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
} }
if !user.External { if !user.External {
log.Error(fmt.Errorf("OIDC user '%s' conflicts with local user", user.Username)) log.Error(fmt.Errorf("OIDC user '%s' conflicts with local user", user.Username))
http.Redirect(w, r, "/auth/login", http.StatusTemporaryRedirect) http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return return
} }
@ -673,5 +676,12 @@ func oidcRedirect(w http.ResponseWriter, r *http.Request) {
redirectPath := mux.Vars(r)["redirect_path"] redirectPath := mux.Vars(r)["redirect_path"]
http.Redirect(w, r, "/"+redirectPath, http.StatusTemporaryRedirect) redirectPath, err = url.JoinPath(util.Config.WebHost, redirectPath)
if err != nil {
log.Error(err)
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, redirectPath, http.StatusTemporaryRedirect)
} }

View File

@ -242,7 +242,7 @@ export default {
}, },
async oidcSignIn(provider) { async oidcSignIn(provider) {
document.location = `/api/auth/oidc/${provider}/login${window.location.search}`; document.location = `${document.baseURI}api/auth/oidc/${provider}/login`;
}, },
}, },
}; };