2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-01-16 17:43:33 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2018-11-01 11:53:36 -04:00
|
|
|
"context"
|
2015-01-16 17:43:33 -05:00
|
|
|
"encoding/json"
|
2017-12-28 14:07:42 -05:00
|
|
|
"errors"
|
2015-01-16 17:43:33 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-06-28 04:57:20 -04:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2015-01-16 17:43:33 -05:00
|
|
|
)
|
|
|
|
|
2017-10-24 12:40:23 -04:00
|
|
|
// Token represents the credentials used to authorize
|
2015-01-16 17:43:33 -05:00
|
|
|
// the requests to access protected resources on the OAuth 2.0
|
|
|
|
// provider's backend.
|
|
|
|
//
|
|
|
|
// This type is a mirror of oauth2.Token and exists to break
|
|
|
|
// an otherwise-circular dependency. Other internal packages
|
|
|
|
// should convert this Token into an oauth2.Token before use.
|
|
|
|
type Token struct {
|
|
|
|
// AccessToken is the token that authorizes and authenticates
|
|
|
|
// the requests.
|
|
|
|
AccessToken string
|
|
|
|
|
|
|
|
// TokenType is the type of token.
|
|
|
|
// The Type method returns either this or "Bearer", the default.
|
|
|
|
TokenType string
|
|
|
|
|
|
|
|
// RefreshToken is a token that's used by the application
|
|
|
|
// (as opposed to the user) to refresh the access token
|
|
|
|
// if it expires.
|
|
|
|
RefreshToken string
|
|
|
|
|
|
|
|
// Expiry is the optional expiration time of the access token.
|
|
|
|
//
|
|
|
|
// If zero, TokenSource implementations will reuse the same
|
|
|
|
// token forever and RefreshToken or equivalent
|
|
|
|
// mechanisms for that TokenSource will not be used.
|
|
|
|
Expiry time.Time
|
|
|
|
|
|
|
|
// Raw optionally contains extra metadata from the server
|
|
|
|
// when updating a token.
|
|
|
|
Raw interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tokenJSON is the struct representing the HTTP response from OAuth2
|
|
|
|
// providers returning a token in JSON form.
|
|
|
|
type tokenJSON struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
|
|
|
|
Expires expirationTime `json:"expires"` // broken Facebook spelling of expires_in
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *tokenJSON) expiry() (t time.Time) {
|
|
|
|
if v := e.ExpiresIn; v != 0 {
|
|
|
|
return time.Now().Add(time.Duration(v) * time.Second)
|
|
|
|
}
|
|
|
|
if v := e.Expires; v != 0 {
|
|
|
|
return time.Now().Add(time.Duration(v) * time.Second)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type expirationTime int32
|
|
|
|
|
|
|
|
func (e *expirationTime) UnmarshalJSON(b []byte) error {
|
|
|
|
var n json.Number
|
|
|
|
err := json.Unmarshal(b, &n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i, err := n.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = expirationTime(i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var brokenAuthHeaderProviders = []string{
|
|
|
|
"https://accounts.google.com/",
|
2017-02-08 18:51:47 -05:00
|
|
|
"https://api.codeswholesale.com/oauth/token",
|
2015-01-16 17:43:33 -05:00
|
|
|
"https://api.dropbox.com/",
|
2016-03-25 13:08:19 -04:00
|
|
|
"https://api.dropboxapi.com/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://api.instagram.com/",
|
|
|
|
"https://api.netatmo.net/",
|
|
|
|
"https://api.odnoklassniki.ru/",
|
|
|
|
"https://api.pushbullet.com/",
|
2015-01-16 17:43:33 -05:00
|
|
|
"https://api.soundcloud.com/",
|
|
|
|
"https://api.twitch.tv/",
|
2018-06-18 12:47:08 -04:00
|
|
|
"https://id.twitch.tv/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://app.box.com/",
|
2018-08-20 14:31:54 -04:00
|
|
|
"https://api.box.com/",
|
2015-01-16 17:43:33 -05:00
|
|
|
"https://connect.stripe.com/",
|
2018-02-23 06:17:35 -05:00
|
|
|
"https://login.mailchimp.com/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://login.microsoftonline.com/",
|
2015-11-16 01:29:26 -05:00
|
|
|
"https://login.salesforce.com/",
|
2017-06-29 15:00:38 -04:00
|
|
|
"https://login.windows.net",
|
2017-11-17 17:55:17 -05:00
|
|
|
"https://login.live.com/",
|
2018-10-03 14:30:08 -04:00
|
|
|
"https://login.live-int.com/",
|
2015-01-16 17:43:33 -05:00
|
|
|
"https://oauth.sandbox.trainingpeaks.com/",
|
|
|
|
"https://oauth.trainingpeaks.com/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://oauth.vk.com/",
|
2016-02-20 04:26:45 -05:00
|
|
|
"https://openapi.baidu.com/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://slack.com/",
|
2015-04-27 16:33:56 -04:00
|
|
|
"https://test-sandbox.auth.corp.google.com",
|
2015-11-16 01:29:26 -05:00
|
|
|
"https://test.salesforce.com/",
|
2015-05-08 06:42:44 -04:00
|
|
|
"https://user.gini.net/",
|
2015-11-09 17:27:09 -05:00
|
|
|
"https://www.douban.com/",
|
|
|
|
"https://www.googleapis.com/",
|
|
|
|
"https://www.linkedin.com/",
|
|
|
|
"https://www.strava.com/oauth/",
|
2016-02-21 22:00:05 -05:00
|
|
|
"https://www.wunderlist.com/oauth/",
|
2016-03-04 16:22:30 -05:00
|
|
|
"https://api.patreon.com/",
|
2016-12-09 06:58:39 -05:00
|
|
|
"https://sandbox.codeswholesale.com/oauth/token",
|
2017-05-10 17:51:24 -04:00
|
|
|
"https://api.sipgate.com/v1/authorization/oauth",
|
2018-02-07 11:52:40 -05:00
|
|
|
"https://api.medium.com/v1/tokens",
|
|
|
|
"https://log.finalsurge.com/oauth/token",
|
2018-04-07 12:16:47 -04:00
|
|
|
"https://multisport.todaysplan.com.au/rest/oauth/access_token",
|
|
|
|
"https://whats.todaysplan.com.au/rest/oauth/access_token",
|
2018-06-20 13:47:24 -04:00
|
|
|
"https://stackoverflow.com/oauth/access_token",
|
2018-08-21 17:02:52 -04:00
|
|
|
"https://account.health.nokia.com",
|
2018-10-17 09:59:35 -04:00
|
|
|
"https://accounts.zoho.com",
|
2018-11-20 14:07:42 -05:00
|
|
|
"https://gitter.im/login/oauth/token",
|
2018-11-28 16:13:12 -05:00
|
|
|
"https://openid-connect.onelogin.com/oidc",
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
|
|
|
|
2017-03-20 14:04:02 -04:00
|
|
|
// brokenAuthHeaderDomains lists broken providers that issue dynamic endpoints.
|
|
|
|
var brokenAuthHeaderDomains = []string{
|
2017-10-11 13:22:33 -04:00
|
|
|
".auth0.com",
|
2017-03-20 14:04:02 -04:00
|
|
|
".force.com",
|
2017-05-04 01:35:22 -04:00
|
|
|
".myshopify.com",
|
2017-03-20 14:04:02 -04:00
|
|
|
".okta.com",
|
|
|
|
".oktapreview.com",
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:49:40 -05:00
|
|
|
func RegisterBrokenAuthHeaderProvider(tokenURL string) {
|
|
|
|
brokenAuthHeaderProviders = append(brokenAuthHeaderProviders, tokenURL)
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:43:33 -05:00
|
|
|
// providerAuthHeaderWorks reports whether the OAuth2 server identified by the tokenURL
|
|
|
|
// implements the OAuth2 spec correctly
|
|
|
|
// See https://code.google.com/p/goauth2/issues/detail?id=31 for background.
|
|
|
|
// In summary:
|
|
|
|
// - Reddit only accepts client secret in the Authorization header
|
|
|
|
// - Dropbox accepts either it in URL param or Auth header, but not both.
|
|
|
|
// - Google only accepts URL param (not spec compliant?), not Auth header
|
|
|
|
// - Stripe only accepts client secret in Auth header with Bearer method, not Basic
|
|
|
|
func providerAuthHeaderWorks(tokenURL string) bool {
|
|
|
|
for _, s := range brokenAuthHeaderProviders {
|
|
|
|
if strings.HasPrefix(tokenURL, s) {
|
|
|
|
// Some sites fail to implement the OAuth2 spec fully.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 14:04:02 -04:00
|
|
|
if u, err := url.Parse(tokenURL); err == nil {
|
|
|
|
for _, s := range brokenAuthHeaderDomains {
|
|
|
|
if strings.HasSuffix(u.Host, s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:43:33 -05:00
|
|
|
// Assume the provider implements the spec properly
|
|
|
|
// otherwise. We can add more exceptions as they're
|
|
|
|
// discovered. We will _not_ be adding configurable hooks
|
|
|
|
// to this package to let users select server bugs.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:03:55 -04:00
|
|
|
func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string, v url.Values) (*Token, error) {
|
|
|
|
bustedAuth := !providerAuthHeaderWorks(tokenURL)
|
2017-03-13 15:46:53 -04:00
|
|
|
if bustedAuth {
|
|
|
|
if clientID != "" {
|
|
|
|
v.Set("client_id", clientID)
|
|
|
|
}
|
|
|
|
if clientSecret != "" {
|
|
|
|
v.Set("client_secret", clientSecret)
|
|
|
|
}
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
2016-07-06 19:03:55 -04:00
|
|
|
req, err := http.NewRequest("POST", tokenURL, strings.NewReader(v.Encode()))
|
2015-01-16 17:43:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
if !bustedAuth {
|
2017-06-22 11:12:08 -04:00
|
|
|
req.SetBasicAuth(url.QueryEscape(clientID), url.QueryEscape(clientSecret))
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
2018-01-02 19:32:36 -05:00
|
|
|
r, err := ctxhttp.Do(ctx, ContextClient(ctx), req)
|
2015-01-16 17:43:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
|
|
|
|
}
|
|
|
|
if code := r.StatusCode; code < 200 || code > 299 {
|
2017-12-14 18:38:05 -05:00
|
|
|
return nil, &RetrieveError{
|
|
|
|
Response: r,
|
|
|
|
Body: body,
|
|
|
|
}
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var token *Token
|
|
|
|
content, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
|
|
|
switch content {
|
|
|
|
case "application/x-www-form-urlencoded", "text/plain":
|
|
|
|
vals, err := url.ParseQuery(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token = &Token{
|
|
|
|
AccessToken: vals.Get("access_token"),
|
|
|
|
TokenType: vals.Get("token_type"),
|
|
|
|
RefreshToken: vals.Get("refresh_token"),
|
|
|
|
Raw: vals,
|
|
|
|
}
|
|
|
|
e := vals.Get("expires_in")
|
|
|
|
if e == "" {
|
|
|
|
// TODO(jbd): Facebook's OAuth2 implementation is broken and
|
|
|
|
// returns expires_in field in expires. Remove the fallback to expires,
|
|
|
|
// when Facebook fixes their implementation.
|
|
|
|
e = vals.Get("expires")
|
|
|
|
}
|
|
|
|
expires, _ := strconv.Atoi(e)
|
|
|
|
if expires != 0 {
|
|
|
|
token.Expiry = time.Now().Add(time.Duration(expires) * time.Second)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
var tj tokenJSON
|
|
|
|
if err = json.Unmarshal(body, &tj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token = &Token{
|
|
|
|
AccessToken: tj.AccessToken,
|
|
|
|
TokenType: tj.TokenType,
|
|
|
|
RefreshToken: tj.RefreshToken,
|
|
|
|
Expiry: tj.expiry(),
|
|
|
|
Raw: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
json.Unmarshal(body, &token.Raw) // no error checks for optional fields
|
|
|
|
}
|
|
|
|
// Don't overwrite `RefreshToken` with an empty value
|
|
|
|
// if this was a token refreshing request.
|
|
|
|
if token.RefreshToken == "" {
|
|
|
|
token.RefreshToken = v.Get("refresh_token")
|
|
|
|
}
|
2017-12-28 14:07:42 -05:00
|
|
|
if token.AccessToken == "" {
|
|
|
|
return token, errors.New("oauth2: server response missing access_token")
|
|
|
|
}
|
2015-01-16 17:43:33 -05:00
|
|
|
return token, nil
|
|
|
|
}
|
2017-12-14 18:38:05 -05:00
|
|
|
|
|
|
|
type RetrieveError struct {
|
|
|
|
Response *http.Response
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RetrieveError) Error() string {
|
|
|
|
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
|
|
|
|
}
|