From c3117c8abe63b05571e5f710201a9a670c2b981e Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 1 Jan 2021 15:43:07 +0100 Subject: [PATCH] Add logger to oidc_callback --- app/handlers/oidc_callback.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/handlers/oidc_callback.go b/app/handlers/oidc_callback.go index 82db0a6..30c6b05 100644 --- a/app/handlers/oidc_callback.go +++ b/app/handlers/oidc_callback.go @@ -22,6 +22,7 @@ const ( type oidcCallbackHandler struct { keySet *jwk.Set + logger *log.Logger oauth2Config *oauth2.Config } @@ -38,7 +39,7 @@ func (c *oidcCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) errorText := r.URL.Query().Get("error") errorDescription := r.URL.Query().Get("error_description") if errorText != "" { - c.RenderErrorTemplate(w, r, errorText, errorDescription) + c.RenderErrorTemplate(w, errorText, errorDescription, http.StatusForbidden) return } @@ -50,7 +51,7 @@ func (c *oidcCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) tok, err := c.oauth2Config.Exchange(ctx, code) if err != nil { - log.Error(err) + c.logger.Error(err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -68,11 +69,11 @@ func (c *oidcCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) session.Values[sessionKeyIdToken] = idToken if oidcToken, err := ParseIdToken(idToken, c.keySet); err != nil { - log.Error(err) + c.logger.Error(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } else { - log.Infof(` + c.logger.Debugf(` ID Token ======== @@ -105,17 +106,18 @@ Not valid after: %s w.WriteHeader(http.StatusFound) } -func (c *oidcCallbackHandler) RenderErrorTemplate(w http.ResponseWriter, r *http.Request, errorText string, errorDescription string) { +func (c *oidcCallbackHandler) RenderErrorTemplate(w http.ResponseWriter, errorText string, errorDescription string, status int) { if errorDescription != "" { - http.Error(w, errorDescription, http.StatusForbidden) + http.Error(w, errorDescription, status) } else { - http.Error(w, errorText, http.StatusForbidden) + http.Error(w, errorText, status) } } -func NewCallbackHandler(ctx context.Context) *oidcCallbackHandler { +func NewCallbackHandler(ctx context.Context, logger *log.Logger) *oidcCallbackHandler { return &oidcCallbackHandler{ keySet: commonServices.GetJwkSet(ctx), + logger: logger, oauth2Config: commonServices.GetOAuth2Config(ctx), } }