handler.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/2637309949/dolphin/packages/oauth2"
  6. "github.com/2637309949/dolphin/packages/oauth2/errors"
  7. )
  8. type (
  9. // ClientInfoHandler get client info from request
  10. ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error)
  11. // ClientAuthorizedHandler check the client allows to use this authorization grant type
  12. ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error)
  13. // ClientScopeHandler check the client allows to use scope
  14. ClientScopeHandler func(clientID, scope string) (allowed bool, err error)
  15. // UserAuthorizationHandler get user id from request authorization
  16. UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, domain string, err error)
  17. // PasswordAuthorizationHandler get user id from username and password
  18. PasswordAuthorizationHandler func(username, password string) (userID string, err error)
  19. // RefreshingScopeHandler check the scope of the refreshing token
  20. RefreshingScopeHandler func(newScope, oldScope string) (allowed bool, err error)
  21. // ResponseErrorHandler response error handing
  22. ResponseErrorHandler func(re *errors.Response)
  23. // InternalErrorHandler internal error handing
  24. InternalErrorHandler func(err error) (re *errors.Response)
  25. // AuthorizeScopeHandler set the authorized scope
  26. AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error)
  27. // AccessTokenExpHandler set expiration date for the access token
  28. AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error)
  29. // ExtensionFieldsHandler in response to the access token with the extension of the field
  30. ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{})
  31. )
  32. // ClientFormHandler get client data from form
  33. func ClientFormHandler(r *http.Request) (string, string, error) {
  34. clientID := r.Form.Get("client_id")
  35. clientSecret := r.Form.Get("client_secret")
  36. if clientID == "" || clientSecret == "" {
  37. return "", "", errors.ErrInvalidClient
  38. }
  39. return clientID, clientSecret, nil
  40. }
  41. // ClientBasicHandler get client data from basic authorization
  42. func ClientBasicHandler(r *http.Request) (string, string, error) {
  43. username, password, ok := r.BasicAuth()
  44. if !ok {
  45. return "", "", errors.ErrInvalidClient
  46. }
  47. return username, password, nil
  48. }