token.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 internal contains support packages for oauth2 package.
  5. package internal
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "mime"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "golang.org/x/net/context"
  18. )
  19. // Token represents the crendentials used to authorize
  20. // the requests to access protected resources on the OAuth 2.0
  21. // provider's backend.
  22. //
  23. // This type is a mirror of oauth2.Token and exists to break
  24. // an otherwise-circular dependency. Other internal packages
  25. // should convert this Token into an oauth2.Token before use.
  26. type Token struct {
  27. // AccessToken is the token that authorizes and authenticates
  28. // the requests.
  29. AccessToken string
  30. // TokenType is the type of token.
  31. // The Type method returns either this or "Bearer", the default.
  32. TokenType string
  33. // RefreshToken is a token that's used by the application
  34. // (as opposed to the user) to refresh the access token
  35. // if it expires.
  36. RefreshToken string
  37. // Expiry is the optional expiration time of the access token.
  38. //
  39. // If zero, TokenSource implementations will reuse the same
  40. // token forever and RefreshToken or equivalent
  41. // mechanisms for that TokenSource will not be used.
  42. Expiry time.Time
  43. // Raw optionally contains extra metadata from the server
  44. // when updating a token.
  45. Raw interface{}
  46. }
  47. // tokenJSON is the struct representing the HTTP response from OAuth2
  48. // providers returning a token in JSON form.
  49. type tokenJSON struct {
  50. AccessToken string `json:"access_token"`
  51. TokenType string `json:"token_type"`
  52. RefreshToken string `json:"refresh_token"`
  53. ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
  54. Expires expirationTime `json:"expires"` // broken Facebook spelling of expires_in
  55. }
  56. func (e *tokenJSON) expiry() (t time.Time) {
  57. if v := e.ExpiresIn; v != 0 {
  58. return time.Now().Add(time.Duration(v) * time.Second)
  59. }
  60. if v := e.Expires; v != 0 {
  61. return time.Now().Add(time.Duration(v) * time.Second)
  62. }
  63. return
  64. }
  65. type expirationTime int32
  66. func (e *expirationTime) UnmarshalJSON(b []byte) error {
  67. var n json.Number
  68. err := json.Unmarshal(b, &n)
  69. if err != nil {
  70. return err
  71. }
  72. i, err := n.Int64()
  73. if err != nil {
  74. return err
  75. }
  76. *e = expirationTime(i)
  77. return nil
  78. }
  79. var brokenAuthHeaderProviders = []string{
  80. "https://accounts.google.com/",
  81. "https://api.dropbox.com/",
  82. "https://api.instagram.com/",
  83. "https://api.netatmo.net/",
  84. "https://api.odnoklassniki.ru/",
  85. "https://api.pushbullet.com/",
  86. "https://api.soundcloud.com/",
  87. "https://api.twitch.tv/",
  88. "https://app.box.com/",
  89. "https://connect.stripe.com/",
  90. "https://login.microsoftonline.com/",
  91. "https://login.salesforce.com/",
  92. "https://oauth.sandbox.trainingpeaks.com/",
  93. "https://oauth.trainingpeaks.com/",
  94. "https://oauth.vk.com/",
  95. "https://slack.com/",
  96. "https://test-sandbox.auth.corp.google.com",
  97. "https://test.salesforce.com/",
  98. "https://user.gini.net/",
  99. "https://www.douban.com/",
  100. "https://www.googleapis.com/",
  101. "https://www.linkedin.com/",
  102. "https://www.strava.com/oauth/",
  103. }
  104. func RegisterBrokenAuthHeaderProvider(tokenURL string) {
  105. brokenAuthHeaderProviders = append(brokenAuthHeaderProviders, tokenURL)
  106. }
  107. // providerAuthHeaderWorks reports whether the OAuth2 server identified by the tokenURL
  108. // implements the OAuth2 spec correctly
  109. // See https://code.google.com/p/goauth2/issues/detail?id=31 for background.
  110. // In summary:
  111. // - Reddit only accepts client secret in the Authorization header
  112. // - Dropbox accepts either it in URL param or Auth header, but not both.
  113. // - Google only accepts URL param (not spec compliant?), not Auth header
  114. // - Stripe only accepts client secret in Auth header with Bearer method, not Basic
  115. func providerAuthHeaderWorks(tokenURL string) bool {
  116. for _, s := range brokenAuthHeaderProviders {
  117. if strings.HasPrefix(tokenURL, s) {
  118. // Some sites fail to implement the OAuth2 spec fully.
  119. return false
  120. }
  121. }
  122. // Assume the provider implements the spec properly
  123. // otherwise. We can add more exceptions as they're
  124. // discovered. We will _not_ be adding configurable hooks
  125. // to this package to let users select server bugs.
  126. return true
  127. }
  128. func RetrieveToken(ctx context.Context, ClientID, ClientSecret, TokenURL string, v url.Values) (*Token, error) {
  129. hc, err := ContextClient(ctx)
  130. if err != nil {
  131. return nil, err
  132. }
  133. v.Set("client_id", ClientID)
  134. bustedAuth := !providerAuthHeaderWorks(TokenURL)
  135. if bustedAuth && ClientSecret != "" {
  136. v.Set("client_secret", ClientSecret)
  137. }
  138. req, err := http.NewRequest("POST", TokenURL, strings.NewReader(v.Encode()))
  139. if err != nil {
  140. return nil, err
  141. }
  142. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  143. if !bustedAuth {
  144. req.SetBasicAuth(ClientID, ClientSecret)
  145. }
  146. r, err := hc.Do(req)
  147. if err != nil {
  148. return nil, err
  149. }
  150. defer r.Body.Close()
  151. body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20))
  152. if err != nil {
  153. return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
  154. }
  155. if code := r.StatusCode; code < 200 || code > 299 {
  156. return nil, fmt.Errorf("oauth2: cannot fetch token: %v\nResponse: %s", r.Status, body)
  157. }
  158. var token *Token
  159. content, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
  160. switch content {
  161. case "application/x-www-form-urlencoded", "text/plain":
  162. vals, err := url.ParseQuery(string(body))
  163. if err != nil {
  164. return nil, err
  165. }
  166. token = &Token{
  167. AccessToken: vals.Get("access_token"),
  168. TokenType: vals.Get("token_type"),
  169. RefreshToken: vals.Get("refresh_token"),
  170. Raw: vals,
  171. }
  172. e := vals.Get("expires_in")
  173. if e == "" {
  174. // TODO(jbd): Facebook's OAuth2 implementation is broken and
  175. // returns expires_in field in expires. Remove the fallback to expires,
  176. // when Facebook fixes their implementation.
  177. e = vals.Get("expires")
  178. }
  179. expires, _ := strconv.Atoi(e)
  180. if expires != 0 {
  181. token.Expiry = time.Now().Add(time.Duration(expires) * time.Second)
  182. }
  183. default:
  184. var tj tokenJSON
  185. if err = json.Unmarshal(body, &tj); err != nil {
  186. return nil, err
  187. }
  188. token = &Token{
  189. AccessToken: tj.AccessToken,
  190. TokenType: tj.TokenType,
  191. RefreshToken: tj.RefreshToken,
  192. Expiry: tj.expiry(),
  193. Raw: make(map[string]interface{}),
  194. }
  195. json.Unmarshal(body, &token.Raw) // no error checks for optional fields
  196. }
  197. // Don't overwrite `RefreshToken` with an empty value
  198. // if this was a token refreshing request.
  199. if token.RefreshToken == "" {
  200. token.RefreshToken = v.Get("refresh_token")
  201. }
  202. return token, nil
  203. }