mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-20 04:57:02 +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()
|
ctx := req.Context()
|
||||||
session, err := t.store.Get(req, cookieName)
|
session, err := t.store.Get(req, cookieName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(rw, "Session error", http.StatusInternalServerError)
|
handleError(rw, "Session error", http.StatusInternalServerError, t.logger)
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
callbackState := req.URL.Query().Get("state")
|
callbackState := req.URL.Query().Get("state")
|
||||||
sessionState, ok := session.Values["csrf"].(string)
|
sessionState, ok := session.Values["csrf"].(string)
|
||||||
if !ok || callbackState != sessionState {
|
if !ok || callbackState != sessionState {
|
||||||
handleError(rw, "Invalid state parameter", http.StatusBadRequest)
|
handleError(rw, "Invalid state parameter", http.StatusBadRequest, t.logger)
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,24 +77,24 @@ func (t *TraefikOidc) handleCallback(rw http.ResponseWriter, req *http.Request)
|
|||||||
redirectURL := buildFullURL(t.scheme, req.Host, t.redirURLPath)
|
redirectURL := buildFullURL(t.scheme, req.Host, t.redirURLPath)
|
||||||
oauth2Token, err := t.exchangeCodeForToken(ctx, code, redirectURL)
|
oauth2Token, err := t.exchangeCodeForToken(ctx, code, redirectURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(rw, "Failed to exchange token", http.StatusInternalServerError)
|
handleError(rw, "Failed to exchange token", http.StatusInternalServerError, t.logger)
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
rawIDToken, ok := oauth2Token["id_token"].(string)
|
rawIDToken, ok := oauth2Token["id_token"].(string)
|
||||||
if !ok {
|
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, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.verifyToken(rawIDToken); err != nil {
|
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, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, err := extractClaims(rawIDToken)
|
claims, err := extractClaims(rawIDToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(rw, "Failed to extract claims", http.StatusInternalServerError)
|
handleError(rw, "Failed to extract claims", http.StatusInternalServerError, t.logger)
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ func (t *TraefikOidc) handleCallback(rw http.ResponseWriter, req *http.Request)
|
|||||||
session.Values["id_token"] = rawIDToken
|
session.Values["id_token"] = rawIDToken
|
||||||
session.Values["email"] = email
|
session.Values["email"] = email
|
||||||
if err := session.Save(req, rw); err != nil {
|
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, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -75,6 +75,7 @@ type Logger interface {
|
|||||||
Errorf(format string, args ...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)
|
http.Error(w, message, code)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user