authhandler.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package authhandler implements a TokenSource to support
  5. // "three-legged OAuth 2.0" via a custom AuthorizationHandler.
  6. package authhandler
  7. import (
  8. "context"
  9. "errors"
  10. "golang.org/x/oauth2"
  11. )
  12. const (
  13. // Parameter keys for AuthCodeURL method to support PKCE.
  14. codeChallengeKey = "code_challenge"
  15. codeChallengeMethodKey = "code_challenge_method"
  16. // Parameter key for Exchange method to support PKCE.
  17. codeVerifierKey = "code_verifier"
  18. )
  19. // PKCEParams holds parameters to support PKCE.
  20. type PKCEParams struct {
  21. Challenge string // The unpadded, base64-url-encoded string of the encrypted code verifier.
  22. ChallengeMethod string // The encryption method (ex. S256).
  23. Verifier string // The original, non-encrypted secret.
  24. }
  25. // AuthorizationHandler is a 3-legged-OAuth helper that prompts
  26. // the user for OAuth consent at the specified auth code URL
  27. // and returns an auth code and state upon approval.
  28. type AuthorizationHandler func(authCodeURL string) (code string, state string, err error)
  29. // TokenSourceWithPKCE is an enhanced version of TokenSource with PKCE support.
  30. //
  31. // The pkce parameter supports PKCE flow, which uses code challenge and code verifier
  32. // to prevent CSRF attacks. A unique code challenge and code verifier should be generated
  33. // by the caller at runtime. See https://www.oauth.com/oauth2-servers/pkce/ for more info.
  34. func TokenSourceWithPKCE(ctx context.Context, config *oauth2.Config, state string, authHandler AuthorizationHandler, pkce *PKCEParams) oauth2.TokenSource {
  35. return oauth2.ReuseTokenSource(nil, authHandlerSource{config: config, ctx: ctx, authHandler: authHandler, state: state, pkce: pkce})
  36. }
  37. // TokenSource returns an oauth2.TokenSource that fetches access tokens
  38. // using 3-legged-OAuth flow.
  39. //
  40. // The provided context.Context is used for oauth2 Exchange operation.
  41. //
  42. // The provided oauth2.Config should be a full configuration containing AuthURL,
  43. // TokenURL, and Scope.
  44. //
  45. // An environment-specific AuthorizationHandler is used to obtain user consent.
  46. //
  47. // Per the OAuth protocol, a unique "state" string should be specified here.
  48. // This token source will verify that the "state" is identical in the request
  49. // and response before exchanging the auth code for OAuth token to prevent CSRF
  50. // attacks.
  51. func TokenSource(ctx context.Context, config *oauth2.Config, state string, authHandler AuthorizationHandler) oauth2.TokenSource {
  52. return TokenSourceWithPKCE(ctx, config, state, authHandler, nil)
  53. }
  54. type authHandlerSource struct {
  55. ctx context.Context
  56. config *oauth2.Config
  57. authHandler AuthorizationHandler
  58. state string
  59. pkce *PKCEParams
  60. }
  61. func (source authHandlerSource) Token() (*oauth2.Token, error) {
  62. // Step 1: Obtain auth code.
  63. var authCodeUrlOptions []oauth2.AuthCodeOption
  64. if source.pkce != nil && source.pkce.Challenge != "" && source.pkce.ChallengeMethod != "" {
  65. authCodeUrlOptions = []oauth2.AuthCodeOption{oauth2.SetAuthURLParam(codeChallengeKey, source.pkce.Challenge),
  66. oauth2.SetAuthURLParam(codeChallengeMethodKey, source.pkce.ChallengeMethod)}
  67. }
  68. url := source.config.AuthCodeURL(source.state, authCodeUrlOptions...)
  69. code, state, err := source.authHandler(url)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if state != source.state {
  74. return nil, errors.New("state mismatch in 3-legged-OAuth flow")
  75. }
  76. // Step 2: Exchange auth code for access token.
  77. var exchangeOptions []oauth2.AuthCodeOption
  78. if source.pkce != nil && source.pkce.Verifier != "" {
  79. exchangeOptions = []oauth2.AuthCodeOption{oauth2.SetAuthURLParam(codeVerifierKey, source.pkce.Verifier)}
  80. }
  81. return source.config.Exchange(source.ctx, code, exchangeOptions...)
  82. }