token.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2014 The oauth2 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 oauth2
  5. import (
  6. "net/http"
  7. "net/url"
  8. "time"
  9. )
  10. // expiryDelta determines how earlier a token should be considered
  11. // expired than its actual expiration time. It is used to avoid late
  12. // expirations due to client-server time mismatches.
  13. const expiryDelta = 10 * time.Second
  14. // Token represents the crendentials used to authorize
  15. // the requests to access protected resources on the OAuth 2.0
  16. // provider's backend.
  17. //
  18. // Most users of this package should not access fields of Token
  19. // directly. They're exported mostly for use by related packages
  20. // implementing derivative OAuth2 flows.
  21. type Token struct {
  22. // AccessToken is the token that authorizes and authenticates
  23. // the requests.
  24. AccessToken string `json:"access_token"`
  25. // TokenType is the type of token.
  26. // The Type method returns either this or "Bearer", the default.
  27. TokenType string `json:"token_type,omitempty"`
  28. // RefreshToken is a token that's used by the application
  29. // (as opposed to the user) to refresh the access token
  30. // if it expires.
  31. RefreshToken string `json:"refresh_token,omitempty"`
  32. // Expiry is the optional expiration time of the access token.
  33. //
  34. // If zero, TokenSource implementations will reuse the same
  35. // token forever and RefreshToken or equivalent
  36. // mechanisms for that TokenSource will not be used.
  37. Expiry time.Time `json:"expiry,omitempty"`
  38. // raw optionally contains extra metadata from the server
  39. // when updating a token.
  40. raw interface{}
  41. }
  42. // Type returns t.TokenType if non-empty, else "Bearer".
  43. func (t *Token) Type() string {
  44. if t.TokenType != "" {
  45. return t.TokenType
  46. }
  47. return "Bearer"
  48. }
  49. // SetAuthHeader sets the Authorization header to r using the access
  50. // token in t.
  51. //
  52. // This method is unnecessary when using Transport or an HTTP Client
  53. // returned by this package.
  54. func (t *Token) SetAuthHeader(r *http.Request) {
  55. r.Header.Set("Authorization", t.Type()+" "+t.AccessToken)
  56. }
  57. // WithExtra returns a new Token that's a clone of t, but using the
  58. // provided raw extra map. This is only intended for use by packages
  59. // implementing derivative OAuth2 flows.
  60. func (t *Token) WithExtra(extra interface{}) *Token {
  61. t2 := new(Token)
  62. *t2 = *t
  63. t2.raw = extra
  64. return t2
  65. }
  66. // Extra returns an extra field.
  67. // Extra fields are key-value pairs returned by the server as a
  68. // part of the token retrieval response.
  69. func (t *Token) Extra(key string) interface{} {
  70. if vals, ok := t.raw.(url.Values); ok {
  71. // TODO(jbd): Cast numeric values to int64 or float64.
  72. return vals.Get(key)
  73. }
  74. if raw, ok := t.raw.(map[string]interface{}); ok {
  75. return raw[key]
  76. }
  77. return nil
  78. }
  79. // expired reports whether the token is expired.
  80. // t must be non-nil.
  81. func (t *Token) expired() bool {
  82. if t.Expiry.IsZero() {
  83. return false
  84. }
  85. return t.Expiry.Add(-expiryDelta).Before(time.Now())
  86. }
  87. // Valid reports whether t is non-nil, has an AccessToken, and is not expired.
  88. func (t *Token) Valid() bool {
  89. return t != nil && t.AccessToken != "" && !t.expired()
  90. }