Skip to content

Commit

Permalink
Merge pull request #168 from 959666690/master
Browse files Browse the repository at this point in the history
add RefreshingValidationHandler when refershing token
  • Loading branch information
LyricTian authored Dec 24, 2020
2 parents b46cf9f + 12db95c commit 07c72de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type (
// RefreshingScopeHandler check the scope of the refreshing token
RefreshingScopeHandler func(newScope, oldScope string) (allowed bool, err error)

//RefreshingValidationHandler check if refresh_token is still valid. eg no revocation or other
RefreshingValidationHandler func(ti oauth2.TokenInfo) (allowed bool, err error)

// ResponseErrorHandler response error handing
ResponseErrorHandler func(re *errors.Response)

Expand Down
17 changes: 17 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Server struct {
ClientScopeHandler ClientScopeHandler
UserAuthorizationHandler UserAuthorizationHandler
PasswordAuthorizationHandler PasswordAuthorizationHandler
RefreshingValidationHandler RefreshingValidationHandler
RefreshingScopeHandler RefreshingScopeHandler
ResponseErrorHandler ResponseErrorHandler
InternalErrorHandler InternalErrorHandler
Expand Down Expand Up @@ -392,6 +393,22 @@ func (s *Server) GetAccessToken(ctx context.Context, gt oauth2.GrantType, tgr *o
}
}

if validationFn := s.RefreshingValidationHandler; validationFn != nil {
rti, err := s.Manager.LoadRefreshToken(ctx, tgr.Refresh)
if err != nil {
if err == errors.ErrInvalidRefreshToken || err == errors.ErrExpiredRefreshToken {
return nil, errors.ErrInvalidGrant
}
return nil, err
}
allowed, err := validationFn(rti)
if err != nil {
return nil, err
} else if !allowed {
return nil, errors.ErrInvalidScope
}
}

ti, err := s.Manager.RefreshAccessToken(ctx, tgr)
if err != nil {
if err == errors.ErrInvalidRefreshToken || err == errors.ErrExpiredRefreshToken {
Expand Down
6 changes: 6 additions & 0 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func (s *Server) SetRefreshingScopeHandler(handler RefreshingScopeHandler) {
s.RefreshingScopeHandler = handler
}

// SetRefreshingValidationHandler check if refresh_token is still valid. eg no revocation or other
func (s *Server) SetRefreshingValidationHandler(handler RefreshingValidationHandler) {
s.RefreshingValidationHandler = handler
}


// SetResponseErrorHandler response error handling
func (s *Server) SetResponseErrorHandler(handler ResponseErrorHandler) {
s.ResponseErrorHandler = handler
Expand Down

0 comments on commit 07c72de

Please sign in to comment.