clientcredentials.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2014 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 clientcredentials implements the OAuth2.0 "client credentials" token flow,
  5. // also known as the "two-legged OAuth 2.0".
  6. //
  7. // This should be used when the client is acting on its own behalf or when the client
  8. // is the resource owner. It may also be used when requesting access to protected
  9. // resources based on an authorization previously arranged with the authorization
  10. // server.
  11. //
  12. // See http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.4
  13. package clientcredentials // import "golang.org/x/oauth2/clientcredentials"
  14. import (
  15. "net/http"
  16. "net/url"
  17. "strings"
  18. "golang.org/x/net/context"
  19. "golang.org/x/oauth2"
  20. "golang.org/x/oauth2/internal"
  21. )
  22. // tokenFromInternal maps an *internal.Token struct into
  23. // an *oauth2.Token struct.
  24. func tokenFromInternal(t *internal.Token) *oauth2.Token {
  25. if t == nil {
  26. return nil
  27. }
  28. tk := &oauth2.Token{
  29. AccessToken: t.AccessToken,
  30. TokenType: t.TokenType,
  31. RefreshToken: t.RefreshToken,
  32. Expiry: t.Expiry,
  33. }
  34. return tk.WithExtra(t.Raw)
  35. }
  36. // retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
  37. // This token is then mapped from *internal.Token into an *oauth2.Token which is
  38. // returned along with an error.
  39. func retrieveToken(ctx context.Context, c *Config, v url.Values) (*oauth2.Token, error) {
  40. tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.TokenURL, v)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return tokenFromInternal(tk), nil
  45. }
  46. // Client Credentials Config describes a 2-legged OAuth2 flow, with both the
  47. // client application information and the server's endpoint URLs.
  48. type Config struct {
  49. // ClientID is the application's ID.
  50. ClientID string
  51. // ClientSecret is the application's secret.
  52. ClientSecret string
  53. // TokenURL is the resource server's token endpoint
  54. // URL. This is a constant specific to each server.
  55. TokenURL string
  56. // Scope specifies optional requested permissions.
  57. Scopes []string
  58. }
  59. // Token uses client credentials to retrieve a token.
  60. // The HTTP client to use is derived from the context.
  61. // If nil, http.DefaultClient is used.
  62. func (c *Config) Token(ctx context.Context) (*oauth2.Token, error) {
  63. return retrieveToken(ctx, c, url.Values{
  64. "grant_type": {"client_credentials"},
  65. "scope": internal.CondVal(strings.Join(c.Scopes, " ")),
  66. })
  67. }
  68. // Client returns an HTTP client using the provided token.
  69. // The token will auto-refresh as necessary. The underlying
  70. // HTTP transport will be obtained using the provided context.
  71. // The returned client and its Transport should not be modified.
  72. func (c *Config) Client(ctx context.Context) *http.Client {
  73. return oauth2.NewClient(ctx, c.TokenSource(ctx))
  74. }
  75. // TokenSource returns a TokenSource that returns t until t expires,
  76. // automatically refreshing it as necessary using the provided context and the
  77. // client ID and client secret.
  78. //
  79. // Most users will use Config.Client instead.
  80. func (c *Config) TokenSource(ctx context.Context) oauth2.TokenSource {
  81. source := &tokenSource{
  82. ctx: ctx,
  83. conf: c,
  84. }
  85. return oauth2.ReuseTokenSource(nil, source)
  86. }
  87. type tokenSource struct {
  88. ctx context.Context
  89. conf *Config
  90. }
  91. // Token refreshes the token by using a new client credentials request.
  92. // tokens received this way do not include a refresh token
  93. func (c *tokenSource) Token() (*oauth2.Token, error) {
  94. return retrieveToken(c.ctx, c.conf, url.Values{
  95. "grant_type": {"client_credentials"},
  96. "scope": internal.CondVal(strings.Join(c.conf.Scopes, " ")),
  97. })
  98. }