Auth UI: auto-prepare, success message, extend tests (#26077)

This commit is contained in:
Michael Geers 2025-12-20 15:52:10 +01:00 • committed by GitHub
parent 86aeb8e542
commit 4d09af22f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 376 additions and 89 deletions

View file

@ -152,20 +152,23 @@ func (a *Handler) handleLogout(w http.ResponseWriter, r *http.Request) {
jsonWrite(w, "OK")
}
func (a *Handler) redirectToError(w http.ResponseWriter, r *http.Request, message string) {
http.Redirect(w, r, "/#/config?callbackError="+url.QueryEscape(message), http.StatusFound)
}
func (a *Handler) handleCallback(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if q.Has("error") {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "error: %s: %s\n", q.Get("error"), q.Get("error_description"))
errorMsg := q.Get("error") + ": " + q.Get("error_description")
a.redirectToError(w, r, errorMsg)
return
}
encryptedState := q.Get("state")
state, err := DecryptState(encryptedState, a.secret)
if err != nil || !state.Valid() {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "invalid state")
a.redirectToError(w, r, "invalid state")
return
}
@ -175,15 +178,13 @@ func (a *Handler) handleCallback(w http.ResponseWriter, r *http.Request) {
// Find the corresponding provider
id, ok := a.states[encryptedState]
if !ok {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "no provider found for state")
a.redirectToError(w, r, "no provider found for state")
return
}
provider, ok := a.providers[id]
if !ok {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "internal provider state unexpected")
a.redirectToError(w, r, "internal provider state unexpected")
return
}
@ -193,8 +194,7 @@ func (a *Handler) handleCallback(w http.ResponseWriter, r *http.Request) {
// Handle the callback
if err := provider.HandleCallback(r.URL.Query()); err != nil {
a.log.ERROR.Printf("callback for provider %s failed: %v", id, err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "callback failed")
a.redirectToError(w, r, "callback failed")
return
}