mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-26 06:09:30 +00:00
Consolidate error handling.
This commit is contained in:
+7
-7
@@ -62,14 +62,14 @@ func (t *TraefikOidc) handleCallback(rw http.ResponseWriter, req *http.Request)
|
||||
ctx := req.Context()
|
||||
session, err := t.store.Get(req, cookieName)
|
||||
if err != nil {
|
||||
handleError(rw, "Session error", http.StatusInternalServerError)
|
||||
handleError(rw, "Session error", http.StatusInternalServerError, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
callbackState := req.URL.Query().Get("state")
|
||||
sessionState, ok := session.Values["csrf"].(string)
|
||||
if !ok || callbackState != sessionState {
|
||||
handleError(rw, "Invalid state parameter", http.StatusBadRequest)
|
||||
handleError(rw, "Invalid state parameter", http.StatusBadRequest, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
@@ -77,24 +77,24 @@ func (t *TraefikOidc) handleCallback(rw http.ResponseWriter, req *http.Request)
|
||||
redirectURL := buildFullURL(t.scheme, req.Host, t.redirURLPath)
|
||||
oauth2Token, err := t.exchangeCodeForToken(ctx, code, redirectURL)
|
||||
if err != nil {
|
||||
handleError(rw, "Failed to exchange token", http.StatusInternalServerError)
|
||||
handleError(rw, "Failed to exchange token", http.StatusInternalServerError, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
rawIDToken, ok := oauth2Token["id_token"].(string)
|
||||
if !ok {
|
||||
handleError(rw, "No id_token field in oauth2 token", http.StatusInternalServerError)
|
||||
handleError(rw, "No id_token field in oauth2 token", http.StatusInternalServerError, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
if err := t.verifyToken(rawIDToken); err != nil {
|
||||
handleError(rw, "Failed to verify token", http.StatusUnauthorized)
|
||||
handleError(rw, "Failed to verify token", http.StatusUnauthorized, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
claims, err := extractClaims(rawIDToken)
|
||||
if err != nil {
|
||||
handleError(rw, "Failed to extract claims", http.StatusInternalServerError)
|
||||
handleError(rw, "Failed to extract claims", http.StatusInternalServerError, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ func (t *TraefikOidc) handleCallback(rw http.ResponseWriter, req *http.Request)
|
||||
session.Values["id_token"] = rawIDToken
|
||||
session.Values["email"] = email
|
||||
if err := session.Save(req, rw); err != nil {
|
||||
handleError(rw, "Failed to save session", http.StatusInternalServerError)
|
||||
handleError(rw, "Failed to save session", http.StatusInternalServerError, t.logger)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -75,6 +75,7 @@ type Logger interface {
|
||||
Errorf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, message string, code int) {
|
||||
func handleError(w http.ResponseWriter, message string, code int, logger Logger) {
|
||||
logger.Errorf(message)
|
||||
http.Error(w, message, code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user