本站源代码
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

76 líneas
2.0KB

  1. package goth
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "golang.org/x/oauth2"
  7. )
  8. // Provider needs to be implemented for each 3rd party authentication provider
  9. // e.g. Facebook, Twitter, etc...
  10. type Provider interface {
  11. Name() string
  12. SetName(name string)
  13. BeginAuth(state string) (Session, error)
  14. UnmarshalSession(string) (Session, error)
  15. FetchUser(Session) (User, error)
  16. Debug(bool)
  17. RefreshToken(refreshToken string) (*oauth2.Token, error) //Get new access token based on the refresh token
  18. RefreshTokenAvailable() bool //Refresh token is provided by auth provider or not
  19. }
  20. const NoAuthUrlErrorMessage = "an AuthURL has not been set"
  21. // Providers is list of known/available providers.
  22. type Providers map[string]Provider
  23. var providers = Providers{}
  24. // UseProviders adds a list of available providers for use with Goth.
  25. // Can be called multiple times. If you pass the same provider more
  26. // than once, the last will be used.
  27. func UseProviders(viders ...Provider) {
  28. for _, provider := range viders {
  29. providers[provider.Name()] = provider
  30. }
  31. }
  32. // GetProviders returns a list of all the providers currently in use.
  33. func GetProviders() Providers {
  34. return providers
  35. }
  36. // GetProvider returns a previously created provider. If Goth has not
  37. // been told to use the named provider it will return an error.
  38. func GetProvider(name string) (Provider, error) {
  39. provider := providers[name]
  40. if provider == nil {
  41. return nil, fmt.Errorf("no provider for %s exists", name)
  42. }
  43. return provider, nil
  44. }
  45. // ClearProviders will remove all providers currently in use.
  46. // This is useful, mostly, for testing purposes.
  47. func ClearProviders() {
  48. providers = Providers{}
  49. }
  50. // ContextForClient provides a context for use with oauth2.
  51. func ContextForClient(h *http.Client) context.Context {
  52. if h == nil {
  53. return oauth2.NoContext
  54. }
  55. return context.WithValue(oauth2.NoContext, oauth2.HTTPClient, h)
  56. }
  57. // HTTPClientWithFallBack to be used in all fetch operations.
  58. func HTTPClientWithFallBack(h *http.Client) *http.Client {
  59. if h != nil {
  60. return h
  61. }
  62. return http.DefaultClient
  63. }
上海开阖软件有限公司 沪ICP备12045867号-1