Skip to content

Commit

Permalink
Merge pull request #206 from clowre/master
Browse files Browse the repository at this point in the history
Added an error handler that is invoked before s.redirectError
  • Loading branch information
LyricTian authored Jan 26, 2022
2 parents 79d5489 + 69dfd47 commit b3af9ce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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)

Expand Down
16 changes: 13 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Server struct {
UserAuthorizationHandler UserAuthorizationHandler
PasswordAuthorizationHandler PasswordAuthorizationHandler
RefreshingValidationHandler RefreshingValidationHandler
PreRedirectErrorHandler PreRedirectErrorHandler
RefreshingScopeHandler RefreshingScopeHandler
ResponseErrorHandler ResponseErrorHandler
InternalErrorHandler InternalErrorHandler
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -88,4 +92,4 @@ func (s *Server) SetAuthorizeScopeHandler(handler AuthorizeScopeHandler) {
// SetResponseTokenHandler response token handing
func (s *Server) SetResponseTokenHandler(handler ResponseTokenHandler) {
s.ResponseTokenHandler = handler
}
}

0 comments on commit b3af9ce

Please sign in to comment.