From 69dfd47f743101ac0b6921e8523f3b15188cb59b Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 24 Jan 2022 14:28:06 +0530 Subject: [PATCH] Added an error handler that is invoked before s.redirectError --- server/handler.go | 3 +++ server/server.go | 16 +++++++++++++--- server/server_config.go | 8 ++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/server/handler.go b/server/handler.go index ad2cc18..ffdb6ec 100755 --- a/server/handler.go +++ b/server/handler.go @@ -36,6 +36,9 @@ type ( // InternalErrorHandler internal error handing InternalErrorHandler func(err error) (re *errors.Response) + // PreRedirectErrorHandler is used to override "redirect-on-error" behavior + PreRedirectErrorHandler func(w http.ResponseWriter, req *AuthorizeRequest, err error) error + // AuthorizeScopeHandler set the authorized scope AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error) diff --git a/server/server.go b/server/server.go index 0fb18e7..3df5ca1 100755 --- a/server/server.go +++ b/server/server.go @@ -48,6 +48,7 @@ type Server struct { UserAuthorizationHandler UserAuthorizationHandler PasswordAuthorizationHandler PasswordAuthorizationHandler RefreshingValidationHandler RefreshingValidationHandler + PreRedirectErrorHandler PreRedirectErrorHandler RefreshingScopeHandler RefreshingScopeHandler ResponseErrorHandler ResponseErrorHandler InternalErrorHandler InternalErrorHandler @@ -57,10 +58,19 @@ type Server struct { ResponseTokenHandler ResponseTokenHandler } +func (s *Server) handleError(w http.ResponseWriter, req *AuthorizeRequest, err error) error { + if fn := s.PreRedirectErrorHandler; fn != nil { + return fn(w, req, err) + } + + return s.redirectError(w, req, err) +} + func (s *Server) redirectError(w http.ResponseWriter, req *AuthorizeRequest, err error) error { if req == nil { return err } + data, _, _ := s.GetErrorData(err) return s.redirect(w, req, data) } @@ -257,13 +267,13 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request) req, err := s.ValidationAuthorizeRequest(r) if err != nil { - return s.redirectError(w, req, err) + return s.handleError(w, req, err) } // user authorization userID, err := s.UserAuthorizationHandler(w, r) if err != nil { - return s.redirectError(w, req, err) + return s.handleError(w, req, err) } else if userID == "" { return nil } @@ -290,7 +300,7 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request) ti, err := s.GetAuthorizeToken(ctx, req) if err != nil { - return s.redirectError(w, req, err) + return s.handleError(w, req, err) } // If the redirect URI is empty, the default domain provided by the client is used. diff --git a/server/server_config.go b/server/server_config.go index 934c543..d4f0404 100644 --- a/server/server_config.go +++ b/server/server_config.go @@ -59,7 +59,6 @@ func (s *Server) SetRefreshingValidationHandler(handler RefreshingValidationHand s.RefreshingValidationHandler = handler } - // SetResponseErrorHandler response error handling func (s *Server) SetResponseErrorHandler(handler ResponseErrorHandler) { s.ResponseErrorHandler = handler @@ -70,6 +69,11 @@ func (s *Server) SetInternalErrorHandler(handler InternalErrorHandler) { s.InternalErrorHandler = handler } +// SetPreRedirectErrorHandler sets the PreRedirectErrorHandler in current Server instance +func (s *Server) SetPreRedirectErrorHandler(handler PreRedirectErrorHandler) { + s.PreRedirectErrorHandler = handler +} + // SetExtensionFieldsHandler in response to the access token with the extension of the field func (s *Server) SetExtensionFieldsHandler(handler ExtensionFieldsHandler) { s.ExtensionFieldsHandler = handler @@ -88,4 +92,4 @@ func (s *Server) SetAuthorizeScopeHandler(handler AuthorizeScopeHandler) { // SetResponseTokenHandler response token handing func (s *Server) SetResponseTokenHandler(handler ResponseTokenHandler) { s.ResponseTokenHandler = handler -} \ No newline at end of file +}