handler.go (2562B)
1 package server 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/superseriousbusiness/oauth2/v4" 8 "github.com/superseriousbusiness/oauth2/v4/errors" 9 ) 10 11 type ( 12 // ClientInfoHandler get client info from request 13 ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error) 14 15 // ClientAuthorizedHandler check the client allows to use this authorization grant type 16 ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error) 17 18 // ClientScopeHandler check the client allows to use scope 19 ClientScopeHandler func(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error) 20 21 // UserAuthorizationHandler get user id from request authorization 22 UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error) 23 24 // PasswordAuthorizationHandler get user id from username and password 25 PasswordAuthorizationHandler func(username, password string) (userID string, err error) 26 27 // RefreshingScopeHandler check the scope of the refreshing token 28 RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error) 29 30 // RefreshingValidationHandler check if refresh_token is still valid. eg no revocation or other 31 RefreshingValidationHandler func(ti oauth2.TokenInfo) (allowed bool, err error) 32 33 // ResponseErrorHandler response error handing 34 ResponseErrorHandler func(re *errors.Response) 35 36 // InternalErrorHandler internal error handing 37 InternalErrorHandler func(err error) (re *errors.Response) 38 39 // AuthorizeScopeHandler set the authorized scope 40 AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error) 41 42 // AccessTokenExpHandler set expiration date for the access token 43 AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error) 44 45 // ExtensionFieldsHandler in response to the access token with the extension of the field 46 ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{}) 47 ) 48 49 // ClientFormHandler get client data from form 50 func ClientFormHandler(r *http.Request) (string, string, error) { 51 clientID := r.Form.Get("client_id") 52 if clientID == "" { 53 return "", "", errors.ErrInvalidClient 54 } 55 clientSecret := r.Form.Get("client_secret") 56 return clientID, clientSecret, nil 57 } 58 59 // ClientBasicHandler get client data from basic authorization 60 func ClientBasicHandler(r *http.Request) (string, string, error) { 61 username, password, ok := r.BasicAuth() 62 if !ok { 63 return "", "", errors.ErrInvalidClient 64 } 65 return username, password, nil 66 }