forked from Mirrors/oauth2
Merge branch 'httpclient'
This commit is contained in:
commit
948919caa0
|
@ -7,6 +7,8 @@
|
||||||
package google
|
package google
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/golang/oauth2"
|
"github.com/golang/oauth2"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
|
@ -16,10 +18,10 @@ import (
|
||||||
// AppEngineConfig represents a configuration for an
|
// AppEngineConfig represents a configuration for an
|
||||||
// App Engine application's Google service account.
|
// App Engine application's Google service account.
|
||||||
type AppEngineConfig struct {
|
type AppEngineConfig struct {
|
||||||
// Transport is the transport to be used
|
// Transport is the http.RoundTripper to be used
|
||||||
// to construct new oauth2.Transport instances from
|
// to construct new oauth2.Transport instances from
|
||||||
// this configuration.
|
// this configuration.
|
||||||
Transport *urlfetch.Transport
|
Transport http.RoundTripper
|
||||||
|
|
||||||
context appengine.Context
|
context appengine.Context
|
||||||
scopes []string
|
scopes []string
|
||||||
|
@ -29,11 +31,6 @@ type AppEngineConfig struct {
|
||||||
// provided auth scopes.
|
// provided auth scopes.
|
||||||
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
||||||
return &AppEngineConfig{
|
return &AppEngineConfig{
|
||||||
Transport: &urlfetch.Transport{
|
|
||||||
Context: context,
|
|
||||||
Deadline: 0,
|
|
||||||
AllowInvalidServerCertificate: false,
|
|
||||||
},
|
|
||||||
context: context,
|
context: context,
|
||||||
scopes: scopes,
|
scopes: scopes,
|
||||||
}
|
}
|
||||||
|
@ -42,7 +39,7 @@ func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineCo
|
||||||
// NewTransport returns a transport that authorizes
|
// NewTransport returns a transport that authorizes
|
||||||
// the requests with the application's service account.
|
// the requests with the application's service account.
|
||||||
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
||||||
return oauth2.NewTransport(c.Transport, c, nil)
|
return oauth2.NewTransport(c.transport(), c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchToken fetches a new access token for the provided scopes.
|
// FetchToken fetches a new access token for the provided scopes.
|
||||||
|
@ -56,3 +53,10 @@ func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, err
|
||||||
Expiry: expiry,
|
Expiry: expiry,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *AppEngineConfig) transport() http.RoundTripper {
|
||||||
|
if c.Transport != nil {
|
||||||
|
return c.Transport
|
||||||
|
}
|
||||||
|
return &urlfetch.Transport{Context: c.context}
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
// AppEngineConfig represents a configuration for an
|
// AppEngineConfig represents a configuration for an
|
||||||
// App Engine application's Google service account.
|
// App Engine application's Google service account.
|
||||||
type AppEngineConfig struct {
|
type AppEngineConfig struct {
|
||||||
// Transport is the round tripper to be used
|
// Transport is the http.RoundTripper to be used
|
||||||
// to construct new oauth2.Transport instances from
|
// to construct new oauth2.Transport instances from
|
||||||
// this configuration.
|
// this configuration.
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
|
@ -29,16 +29,15 @@ type AppEngineConfig struct {
|
||||||
// provided auth scopes.
|
// provided auth scopes.
|
||||||
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
||||||
return &AppEngineConfig{
|
return &AppEngineConfig{
|
||||||
Transport: http.DefaultTransport,
|
context: context,
|
||||||
context: context,
|
scopes: scopes,
|
||||||
scopes: scopes,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransport returns a transport that authorizes
|
// NewTransport returns a transport that authorizes
|
||||||
// the requests with the application's service account.
|
// the requests with the application's service account.
|
||||||
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
||||||
return oauth2.NewTransport(c.Transport, c, nil)
|
return oauth2.NewTransport(c.transport(), c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchToken fetches a new access token for the provided scopes.
|
// FetchToken fetches a new access token for the provided scopes.
|
||||||
|
@ -52,3 +51,10 @@ func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, err
|
||||||
Expiry: expiry,
|
Expiry: expiry,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *AppEngineConfig) transport() http.RoundTripper {
|
||||||
|
if c.Transport != nil {
|
||||||
|
return c.Transport
|
||||||
|
}
|
||||||
|
return http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
|
@ -65,16 +65,12 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
|
||||||
// from Google Compute Engine instance's metaserver. If no account is
|
// from Google Compute Engine instance's metaserver. If no account is
|
||||||
// provided, default is used.
|
// provided, default is used.
|
||||||
func NewComputeEngineConfig(account string) *ComputeEngineConfig {
|
func NewComputeEngineConfig(account string) *ComputeEngineConfig {
|
||||||
return &ComputeEngineConfig{
|
return &ComputeEngineConfig{account: account}
|
||||||
Client: http.DefaultClient,
|
|
||||||
Transport: http.DefaultTransport,
|
|
||||||
account: account,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransport creates an authorized transport.
|
// NewTransport creates an authorized transport.
|
||||||
func (c *ComputeEngineConfig) NewTransport() *oauth2.Transport {
|
func (c *ComputeEngineConfig) NewTransport() *oauth2.Transport {
|
||||||
return oauth2.NewTransport(c.Transport, c, nil)
|
return oauth2.NewTransport(c.transport(), c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchToken retrieves a new access token via metadata server.
|
// FetchToken retrieves a new access token via metadata server.
|
||||||
|
@ -89,7 +85,7 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.Header.Add("X-Google-Metadata-Request", "True")
|
req.Header.Add("X-Google-Metadata-Request", "True")
|
||||||
resp, err := c.Client.Do(req)
|
resp, err := c.client().Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -106,3 +102,17 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ComputeEngineConfig) transport() http.RoundTripper {
|
||||||
|
if c.Transport != nil {
|
||||||
|
return c.Transport
|
||||||
|
}
|
||||||
|
return http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ComputeEngineConfig) client() *http.Client {
|
||||||
|
if c.Client != nil {
|
||||||
|
return c.Client
|
||||||
|
}
|
||||||
|
return http.DefaultClient
|
||||||
|
}
|
||||||
|
|
30
jwt.go
30
jwt.go
|
@ -58,11 +58,9 @@ func NewJWTConfig(opts *JWTOptions, aud string) (*JWTConfig, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &JWTConfig{
|
return &JWTConfig{
|
||||||
Client: http.DefaultClient,
|
opts: opts,
|
||||||
Transport: http.DefaultTransport,
|
aud: audURL,
|
||||||
opts: opts,
|
key: parsedKey,
|
||||||
aud: audURL,
|
|
||||||
key: parsedKey,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +71,7 @@ type JWTConfig struct {
|
||||||
// tokens from the OAuth 2.0 provider.
|
// tokens from the OAuth 2.0 provider.
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
|
||||||
// Transport is the round tripper to be used
|
// Transport is the http.RoundTripper to be used
|
||||||
// to construct new oauth2.Transport instances from
|
// to construct new oauth2.Transport instances from
|
||||||
// this configuration.
|
// this configuration.
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
|
@ -86,13 +84,13 @@ type JWTConfig struct {
|
||||||
// NewTransport creates a transport that is authorize with the
|
// NewTransport creates a transport that is authorize with the
|
||||||
// parent JWT configuration.
|
// parent JWT configuration.
|
||||||
func (c *JWTConfig) NewTransport() *Transport {
|
func (c *JWTConfig) NewTransport() *Transport {
|
||||||
return NewTransport(c.Transport, c, &Token{})
|
return NewTransport(c.transport(), c, &Token{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransportWithUser creates a transport that is authorized by
|
// NewTransportWithUser creates a transport that is authorized by
|
||||||
// the client and impersonates the specified user.
|
// the client and impersonates the specified user.
|
||||||
func (c *JWTConfig) NewTransportWithUser(user string) *Transport {
|
func (c *JWTConfig) NewTransportWithUser(user string) *Transport {
|
||||||
return NewTransport(c.Transport, c, &Token{Subject: user})
|
return NewTransport(c.transport(), c, &Token{Subject: user})
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchToken retrieves a new access token and updates the existing token
|
// fetchToken retrieves a new access token and updates the existing token
|
||||||
|
@ -124,7 +122,7 @@ func (c *JWTConfig) FetchToken(existing *Token) (*Token, error) {
|
||||||
v.Set("assertion", payload)
|
v.Set("assertion", payload)
|
||||||
|
|
||||||
// Make a request with assertion to get a new token.
|
// Make a request with assertion to get a new token.
|
||||||
resp, err := c.Client.PostForm(c.aud.String(), v)
|
resp, err := c.client().PostForm(c.aud.String(), v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
|
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -163,6 +161,20 @@ func (c *JWTConfig) FetchToken(existing *Token) (*Token, error) {
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *JWTConfig) transport() http.RoundTripper {
|
||||||
|
if c.Transport != nil {
|
||||||
|
return c.Transport
|
||||||
|
}
|
||||||
|
return http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *JWTConfig) client() *http.Client {
|
||||||
|
if c.Client != nil {
|
||||||
|
return c.Client
|
||||||
|
}
|
||||||
|
return http.DefaultClient
|
||||||
|
}
|
||||||
|
|
||||||
// parseKey converts the binary contents of a private key file
|
// parseKey converts the binary contents of a private key file
|
||||||
// to an *rsa.PrivateKey. It detects whether the private key is in a
|
// to an *rsa.PrivateKey. It detects whether the private key is in a
|
||||||
// PEM container or not. If so, it extracts the the private key
|
// PEM container or not. If so, it extracts the the private key
|
||||||
|
|
22
oauth2.go
22
oauth2.go
|
@ -115,7 +115,7 @@ type Config struct {
|
||||||
// tokens from the OAuth 2.0 provider.
|
// tokens from the OAuth 2.0 provider.
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
|
||||||
// Transport is the round tripper to be used
|
// Transport is the http.RoundTripper to be used
|
||||||
// to construct new oauth2.Transport instances from
|
// to construct new oauth2.Transport instances from
|
||||||
// this configuration.
|
// this configuration.
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
|
@ -161,7 +161,7 @@ func (c *Config) AuthCodeURL(state string) (authURL string) {
|
||||||
// you need to set a valid token (or an expired token with a valid
|
// you need to set a valid token (or an expired token with a valid
|
||||||
// refresh token) in order to be able to do authorized requests.
|
// refresh token) in order to be able to do authorized requests.
|
||||||
func (c *Config) NewTransport() *Transport {
|
func (c *Config) NewTransport() *Transport {
|
||||||
return NewTransport(c.Transport, c, nil)
|
return NewTransport(c.transport(), c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransportWithCode exchanges the OAuth 2.0 authorization code with
|
// NewTransportWithCode exchanges the OAuth 2.0 authorization code with
|
||||||
|
@ -173,7 +173,7 @@ func (c *Config) NewTransportWithCode(code string) (*Transport, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return NewTransport(c.Transport, c, token), nil
|
return NewTransport(c.transport(), c, token), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchToken retrieves a new access token and updates the existing token
|
// FetchToken retrieves a new access token and updates the existing token
|
||||||
|
@ -226,7 +226,7 @@ func (c *Config) retrieveToken(v url.Values) (*Token, error) {
|
||||||
// Dropbox accepts either, but not both.
|
// Dropbox accepts either, but not both.
|
||||||
// The spec requires servers to always support the Authorization header,
|
// The spec requires servers to always support the Authorization header,
|
||||||
// so that's all we use.
|
// so that's all we use.
|
||||||
r, err := c.Client.PostForm(c.tokenURL.String(), v)
|
r, err := c.client().PostForm(c.tokenURL.String(), v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -281,3 +281,17 @@ func (c *Config) retrieveToken(v url.Values) (*Token, error) {
|
||||||
}
|
}
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) transport() http.RoundTripper {
|
||||||
|
if c.Transport != nil {
|
||||||
|
return c.Transport
|
||||||
|
}
|
||||||
|
return http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) client() *http.Client {
|
||||||
|
if c.Client != nil {
|
||||||
|
return c.Client
|
||||||
|
}
|
||||||
|
return http.DefaultClient
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue