2014-05-17 11:26:57 -04:00
|
|
|
// Copyright 2014 The oauth2 Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-05-13 14:06:46 -04:00
|
|
|
|
2014-05-05 17:54:23 -04:00
|
|
|
// Package oauth2 provides support for making
|
|
|
|
// OAuth2 authorized and authenticated HTTP requests.
|
|
|
|
// It can additionally grant authorization with Bearer JWT.
|
2014-11-26 14:44:45 -05:00
|
|
|
package oauth2 // import "golang.org/x/oauth2"
|
2014-05-05 17:54:23 -04:00
|
|
|
|
|
|
|
import (
|
2014-12-09 18:17:33 -05:00
|
|
|
"bytes"
|
2014-05-05 17:54:23 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-09-05 01:43:23 -04:00
|
|
|
"fmt"
|
2014-12-09 18:17:33 -05:00
|
|
|
"io"
|
2014-05-05 17:54:23 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-08-12 22:50:34 -04:00
|
|
|
"strconv"
|
2014-05-05 17:54:23 -04:00
|
|
|
"strings"
|
2014-12-09 18:17:33 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2014-05-05 17:54:23 -04:00
|
|
|
)
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Context can be an golang.org/x/net.Context, or an App Engine Context.
|
2014-12-11 02:30:13 -05:00
|
|
|
// If you don't care and aren't running on App Engine, you may use NoContext.
|
2014-12-09 18:17:33 -05:00
|
|
|
type Context interface{}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
// NoContext is the default context. If you're not running this code
|
|
|
|
// on App Engine or not using golang.org/x/net.Context to provide a custom
|
|
|
|
// HTTP client, you should use NoContext.
|
|
|
|
var NoContext Context = nil
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Config describes a typical 3-legged OAuth2 flow, with both the
|
2014-12-30 16:25:01 -05:00
|
|
|
// client application information and the server's endpoint URLs.
|
2014-12-09 18:17:33 -05:00
|
|
|
type Config struct {
|
|
|
|
// ClientID is the application's ID.
|
|
|
|
ClientID string
|
|
|
|
|
|
|
|
// ClientSecret is the application's secret.
|
|
|
|
ClientSecret string
|
|
|
|
|
|
|
|
// Endpoint contains the resource server's token endpoint
|
2014-12-30 16:25:01 -05:00
|
|
|
// URLs. These are constants specific to each server and are
|
|
|
|
// often available via site-specific packages, such as
|
|
|
|
// google.Endpoint or github.Endpoint.
|
2014-12-09 18:17:33 -05:00
|
|
|
Endpoint Endpoint
|
|
|
|
|
|
|
|
// RedirectURL is the URL to redirect users going through
|
|
|
|
// the OAuth flow, after the resource owner's URLs.
|
|
|
|
RedirectURL string
|
|
|
|
|
|
|
|
// Scope specifies optional requested permissions.
|
|
|
|
Scopes []string
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// A TokenSource is anything that can return a token.
|
|
|
|
type TokenSource interface {
|
|
|
|
// Token returns a token or an error.
|
2014-12-30 16:25:01 -05:00
|
|
|
// Token must be safe for concurrent use by multiple goroutines.
|
2014-12-30 17:30:46 -05:00
|
|
|
// The returned Token must not be modified.
|
2014-12-09 18:17:33 -05:00
|
|
|
Token() (*Token, error)
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Endpoint contains the OAuth 2.0 provider's authorization and token
|
|
|
|
// endpoint URLs.
|
|
|
|
type Endpoint struct {
|
|
|
|
AuthURL string
|
|
|
|
TokenURL string
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
var (
|
|
|
|
// AccessTypeOnline and AccessTypeOffline are options passed
|
|
|
|
// to the Options.AuthCodeURL method. They modify the
|
|
|
|
// "access_type" field that gets sent in the URL returned by
|
|
|
|
// AuthCodeURL.
|
|
|
|
//
|
2015-02-04 22:49:57 -05:00
|
|
|
// Online is the default if neither is specified. If your
|
|
|
|
// application needs to refresh access tokens when the user
|
|
|
|
// is not present at the browser, then use offline. This will
|
|
|
|
// result in your application obtaining a refresh token the
|
|
|
|
// first time your application exchanges an authorization
|
2014-12-09 18:17:33 -05:00
|
|
|
// code for a user.
|
|
|
|
AccessTypeOnline AuthCodeOption = setParam{"access_type", "online"}
|
|
|
|
AccessTypeOffline AuthCodeOption = setParam{"access_type", "offline"}
|
|
|
|
|
|
|
|
// ApprovalForce forces the users to view the consent dialog
|
|
|
|
// and confirm the permissions request at the URL returned
|
|
|
|
// from AuthCodeURL, even if they've already done so.
|
|
|
|
ApprovalForce AuthCodeOption = setParam{"approval_prompt", "force"}
|
|
|
|
)
|
|
|
|
|
|
|
|
type setParam struct{ k, v string }
|
|
|
|
|
|
|
|
func (p setParam) setValue(m url.Values) { m.Set(p.k, p.v) }
|
|
|
|
|
|
|
|
// An AuthCodeOption is passed to Config.AuthCodeURL.
|
|
|
|
type AuthCodeOption interface {
|
|
|
|
setValue(url.Values)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
|
|
|
|
// that asks for permissions for the required scopes explicitly.
|
2014-09-05 01:43:23 -04:00
|
|
|
//
|
|
|
|
// State is a token to protect the user from CSRF attacks. You must
|
|
|
|
// always provide a non-zero string and validate that it matches the
|
|
|
|
// the state query parameter on your redirect callback.
|
|
|
|
// See http://tools.ietf.org/html/rfc6749#section-10.12 for more info.
|
|
|
|
//
|
2014-12-09 18:17:33 -05:00
|
|
|
// Opts may include AccessTypeOnline or AccessTypeOffline, as well
|
|
|
|
// as ApprovalForce.
|
|
|
|
func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(c.Endpoint.AuthURL)
|
2014-09-04 16:28:18 -04:00
|
|
|
v := url.Values{
|
2014-12-09 18:17:33 -05:00
|
|
|
"response_type": {"code"},
|
|
|
|
"client_id": {c.ClientID},
|
|
|
|
"redirect_uri": condVal(c.RedirectURL),
|
|
|
|
"scope": condVal(strings.Join(c.Scopes, " ")),
|
|
|
|
"state": condVal(state),
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.setValue(v)
|
2014-07-29 15:39:33 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
if strings.Contains(c.Endpoint.AuthURL, "?") {
|
|
|
|
buf.WriteByte('&')
|
2014-05-05 17:54:23 -04:00
|
|
|
} else {
|
2014-12-09 18:17:33 -05:00
|
|
|
buf.WriteByte('?')
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
buf.WriteString(v.Encode())
|
|
|
|
return buf.String()
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Exchange converts an authorization code into a token.
|
|
|
|
//
|
|
|
|
// It is used after a resource provider redirects the user back
|
|
|
|
// to the Redirect URI (the URL obtained from AuthCodeURL).
|
|
|
|
//
|
|
|
|
// The HTTP client to use is derived from the context. If nil,
|
|
|
|
// http.DefaultClient is used. See the Context type's documentation.
|
|
|
|
//
|
|
|
|
// The code will be in the *http.Request.FormValue("code"). Before
|
|
|
|
// calling Exchange, be sure to validate FormValue("state").
|
|
|
|
func (c *Config) Exchange(ctx Context, code string) (*Token, error) {
|
|
|
|
return retrieveToken(ctx, c, url.Values{
|
2014-11-06 19:36:41 -05:00
|
|
|
"grant_type": {"authorization_code"},
|
|
|
|
"code": {code},
|
2014-12-09 18:17:33 -05:00
|
|
|
"redirect_uri": condVal(c.RedirectURL),
|
|
|
|
"scope": condVal(strings.Join(c.Scopes, " ")),
|
2014-11-06 19:36:41 -05:00
|
|
|
})
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// contextClientFunc is a func which tries to return an *http.Client
|
|
|
|
// given a Context value. If it returns an error, the search stops
|
|
|
|
// with that error. If it returns (nil, nil), the search continues
|
|
|
|
// down the list of registered funcs.
|
|
|
|
type contextClientFunc func(Context) (*http.Client, error)
|
|
|
|
|
|
|
|
var contextClientFuncs []contextClientFunc
|
|
|
|
|
2014-12-16 13:06:06 -05:00
|
|
|
func registerContextClientFunc(fn contextClientFunc) {
|
2014-12-09 18:17:33 -05:00
|
|
|
contextClientFuncs = append(contextClientFuncs, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func contextClient(ctx Context) (*http.Client, error) {
|
|
|
|
for _, fn := range contextClientFuncs {
|
|
|
|
c, err := fn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c != nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
if xc, ok := ctx.(context.Context); ok {
|
|
|
|
if hc, ok := xc.Value(HTTPClient).(*http.Client); ok {
|
|
|
|
return hc, nil
|
|
|
|
}
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
return http.DefaultClient, nil
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
func contextTransport(ctx Context) http.RoundTripper {
|
|
|
|
hc, err := contextClient(ctx)
|
2014-05-05 17:54:23 -04:00
|
|
|
if err != nil {
|
2014-12-09 18:17:33 -05:00
|
|
|
// This is a rare error case (somebody using nil on App Engine),
|
|
|
|
// so I'd rather not everybody do an error check on this Client
|
|
|
|
// method. They can get the error that they're doing it wrong
|
|
|
|
// later, at client.Get/PostForm time.
|
|
|
|
return errorTransport{err}
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
return hc.Transport
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Client returns an HTTP client using the provided token.
|
|
|
|
// The token will auto-refresh as necessary. The underlying
|
|
|
|
// HTTP transport will be obtained using the provided context.
|
|
|
|
// The returned client and its Transport should not be modified.
|
|
|
|
func (c *Config) Client(ctx Context, t *Token) *http.Client {
|
2014-12-16 18:39:29 -05:00
|
|
|
return NewClient(ctx, c.TokenSource(ctx, t))
|
2014-06-26 20:27:53 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// TokenSource returns a TokenSource that returns t until t expires,
|
|
|
|
// automatically refreshing it as necessary using the provided context.
|
|
|
|
// See the the Context documentation.
|
|
|
|
//
|
|
|
|
// Most users will use Config.Client instead.
|
|
|
|
func (c *Config) TokenSource(ctx Context, t *Token) TokenSource {
|
2014-12-30 16:25:01 -05:00
|
|
|
nwn := &reuseTokenSource{t: t}
|
2014-12-09 18:17:33 -05:00
|
|
|
nwn.new = tokenRefresher{
|
|
|
|
ctx: ctx,
|
|
|
|
conf: c,
|
|
|
|
oldToken: &nwn.t,
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
return nwn
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// tokenRefresher is a TokenSource that makes "grant_type"=="refresh_token"
|
|
|
|
// HTTP requests to renew a token using a RefreshToken.
|
|
|
|
type tokenRefresher struct {
|
|
|
|
ctx Context // used to get HTTP requests
|
|
|
|
conf *Config
|
|
|
|
oldToken **Token // pointer to old *Token w/ RefreshToken
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
func (tf tokenRefresher) Token() (*Token, error) {
|
|
|
|
t := *tf.oldToken
|
|
|
|
if t == nil {
|
|
|
|
return nil, errors.New("oauth2: attempted use of nil Token")
|
|
|
|
}
|
|
|
|
if t.RefreshToken == "" {
|
|
|
|
return nil, errors.New("oauth2: token expired and refresh token is not set")
|
|
|
|
}
|
|
|
|
return retrieveToken(tf.ctx, tf.conf, url.Values{
|
|
|
|
"grant_type": {"refresh_token"},
|
|
|
|
"refresh_token": {t.RefreshToken},
|
|
|
|
})
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2014-12-30 16:25:01 -05:00
|
|
|
// reuseTokenSource is a TokenSource that holds a single token in memory
|
2014-12-09 18:17:33 -05:00
|
|
|
// and validates its expiry before each call to retrieve it with
|
|
|
|
// Token. If it's expired, it will be auto-refreshed using the
|
|
|
|
// new TokenSource.
|
|
|
|
//
|
|
|
|
// The first call to TokenRefresher must be SetToken.
|
2014-12-30 16:25:01 -05:00
|
|
|
type reuseTokenSource struct {
|
2014-12-09 18:17:33 -05:00
|
|
|
new TokenSource // called when t is expired.
|
2014-11-12 23:41:14 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
mu sync.Mutex // guards t
|
|
|
|
t *Token
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Token returns the current token if it's still valid, else will
|
|
|
|
// refresh the current token (using r.Context for HTTP client
|
|
|
|
// information) and return the new one.
|
2014-12-30 16:25:01 -05:00
|
|
|
func (s *reuseTokenSource) Token() (*Token, error) {
|
2014-12-09 18:17:33 -05:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2014-12-30 16:25:01 -05:00
|
|
|
if s.t.Valid() {
|
2014-12-09 18:17:33 -05:00
|
|
|
return s.t, nil
|
|
|
|
}
|
|
|
|
t, err := s.new.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.t = t
|
|
|
|
return t, nil
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
func retrieveToken(ctx Context, c *Config, v url.Values) (*Token, error) {
|
|
|
|
hc, err := contextClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v.Set("client_id", c.ClientID)
|
|
|
|
bustedAuth := !providerAuthHeaderWorks(c.Endpoint.TokenURL)
|
|
|
|
if bustedAuth && c.ClientSecret != "" {
|
|
|
|
v.Set("client_secret", c.ClientSecret)
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
req, err := http.NewRequest("POST", c.Endpoint.TokenURL, strings.NewReader(v.Encode()))
|
2014-09-04 16:28:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2014-12-09 18:17:33 -05:00
|
|
|
if !bustedAuth && c.ClientSecret != "" {
|
|
|
|
req.SetBasicAuth(c.ClientID, c.ClientSecret)
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
r, err := hc.Do(req)
|
2014-05-05 17:54:23 -04:00
|
|
|
if err != nil {
|
2014-08-13 19:47:57 -04:00
|
|
|
return nil, err
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
2014-12-09 18:17:33 -05:00
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20))
|
2014-09-05 01:43:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
if code := r.StatusCode; code < 200 || code > 299 {
|
2014-09-05 01:43:23 -04:00
|
|
|
return nil, fmt.Errorf("oauth2: cannot fetch token: %v\nResponse: %s", r.Status, body)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:20:30 -05:00
|
|
|
var token *Token
|
2014-05-05 17:54:23 -04:00
|
|
|
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 {
|
2014-08-13 19:47:57 -04:00
|
|
|
return nil, err
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-12-19 00:20:30 -05:00
|
|
|
token = &Token{
|
|
|
|
AccessToken: vals.Get("access_token"),
|
|
|
|
TokenType: vals.Get("token_type"),
|
|
|
|
RefreshToken: vals.Get("refresh_token"),
|
|
|
|
raw: vals,
|
|
|
|
}
|
2014-10-27 20:35:35 -04:00
|
|
|
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")
|
|
|
|
}
|
2014-12-19 00:20:30 -05:00
|
|
|
expires, _ := strconv.Atoi(e)
|
|
|
|
if expires != 0 {
|
|
|
|
token.Expiry = time.Now().Add(time.Duration(expires) * time.Second)
|
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
default:
|
2014-12-19 00:20:30 -05:00
|
|
|
var tj tokenJSON
|
|
|
|
if err = json.Unmarshal(body, &tj); err != nil {
|
2014-08-13 19:47:57 -04:00
|
|
|
return nil, err
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-12-19 00:20:30 -05:00
|
|
|
token = &Token{
|
|
|
|
AccessToken: tj.AccessToken,
|
|
|
|
TokenType: tj.TokenType,
|
|
|
|
RefreshToken: tj.RefreshToken,
|
|
|
|
Expiry: tj.expiry(),
|
|
|
|
raw: make(map[string]interface{}),
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2014-12-19 00:20:30 -05:00
|
|
|
json.Unmarshal(body, &token.raw) // no error checks for optional fields
|
2014-08-13 19:47:57 -04:00
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
// Don't overwrite `RefreshToken` with an empty value
|
2014-08-13 19:47:57 -04:00
|
|
|
// if this was a token refreshing request.
|
2014-10-27 20:35:35 -04:00
|
|
|
if token.RefreshToken == "" {
|
2014-08-13 19:47:57 -04:00
|
|
|
token.RefreshToken = v.Get("refresh_token")
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-08-13 19:47:57 -04:00
|
|
|
return token, nil
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-09-02 17:06:51 -04:00
|
|
|
|
2014-12-19 00:20:30 -05:00
|
|
|
// 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 int32 `json:"expires_in"`
|
|
|
|
Expires int32 `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
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func condVal(v string) []string {
|
|
|
|
if v == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []string{v}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func providerAuthHeaderWorks(tokenURL string) bool {
|
|
|
|
if strings.HasPrefix(tokenURL, "https://accounts.google.com/") ||
|
2015-02-04 08:17:40 -05:00
|
|
|
strings.HasPrefix(tokenURL, "https://www.googleapis.com/") ||
|
2014-09-04 16:28:18 -04:00
|
|
|
strings.HasPrefix(tokenURL, "https://github.com/") ||
|
|
|
|
strings.HasPrefix(tokenURL, "https://api.instagram.com/") ||
|
2014-09-07 13:05:03 -04:00
|
|
|
strings.HasPrefix(tokenURL, "https://www.douban.com/") ||
|
2014-09-10 22:49:49 -04:00
|
|
|
strings.HasPrefix(tokenURL, "https://api.dropbox.com/") ||
|
2014-10-04 02:16:01 -04:00
|
|
|
strings.HasPrefix(tokenURL, "https://api.soundcloud.com/") ||
|
2014-09-10 22:49:49 -04:00
|
|
|
strings.HasPrefix(tokenURL, "https://www.linkedin.com/") {
|
2014-09-04 16:28:18 -04:00
|
|
|
// Some sites fail to implement the OAuth2 spec fully.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
|
|
|
|
// HTTPClient is the context key to use with golang.org/x/net/context's
|
|
|
|
// WithValue function to associate an *http.Client value with a context.
|
|
|
|
var HTTPClient contextKey
|
|
|
|
|
|
|
|
// contextKey is just an empty struct. It exists so HTTPClient can be
|
|
|
|
// an immutable public variable with a unique type. It's immutable
|
|
|
|
// because nobody else can create a contextKey, being unexported.
|
|
|
|
type contextKey struct{}
|
2014-12-16 18:39:29 -05:00
|
|
|
|
|
|
|
// NewClient creates an *http.Client from a Context and TokenSource.
|
2014-12-30 16:25:01 -05:00
|
|
|
// The returned client is not valid beyond the lifetime of the context.
|
2014-12-30 18:11:02 -05:00
|
|
|
//
|
|
|
|
// As a special case, if src is nil, a non-OAuth2 client is returned
|
|
|
|
// using the provided context. This exists to support related OAuth2
|
|
|
|
// packages.
|
2014-12-16 18:39:29 -05:00
|
|
|
func NewClient(ctx Context, src TokenSource) *http.Client {
|
2014-12-30 18:11:02 -05:00
|
|
|
if src == nil {
|
|
|
|
c, err := contextClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &http.Client{Transport: errorTransport{err}}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2014-12-16 18:39:29 -05:00
|
|
|
return &http.Client{
|
|
|
|
Transport: &Transport{
|
|
|
|
Base: contextTransport(ctx),
|
2014-12-30 16:25:01 -05:00
|
|
|
Source: ReuseTokenSource(nil, src),
|
2014-12-16 18:39:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
|
|
|
|
// ReuseTokenSource returns a TokenSource which repeatedly returns the
|
|
|
|
// same token as long as it's valid, starting with t.
|
|
|
|
// When its cached token is invalid, a new token is obtained from src.
|
|
|
|
//
|
|
|
|
// ReuseTokenSource is typically used to reuse tokens from a cache
|
|
|
|
// (such as a file on disk) between runs of a program, rather than
|
|
|
|
// obtaining new tokens unnecessarily.
|
|
|
|
//
|
|
|
|
// The initial token t may be nil, in which case the TokenSource is
|
|
|
|
// wrapped in a caching version if it isn't one already. This also
|
|
|
|
// means it's always safe to wrap ReuseTokenSource around any other
|
|
|
|
// TokenSource without adverse effects.
|
|
|
|
func ReuseTokenSource(t *Token, src TokenSource) TokenSource {
|
|
|
|
// Don't wrap a reuseTokenSource in itself. That would work,
|
|
|
|
// but cause an unnecessary number of mutex operations.
|
|
|
|
// Just build the equivalent one.
|
|
|
|
if rt, ok := src.(*reuseTokenSource); ok {
|
|
|
|
if t == nil {
|
|
|
|
// Just use it directly.
|
|
|
|
return rt
|
|
|
|
}
|
|
|
|
src = rt.new
|
|
|
|
}
|
|
|
|
return &reuseTokenSource{
|
|
|
|
t: t,
|
|
|
|
new: src,
|
|
|
|
}
|
|
|
|
}
|