forked from remote/oauth2
Do not assume that http.DefaultClient and http.DefaultTransport is always available.
This commit is contained in:
@@ -12,31 +12,32 @@ import (
|
||||
// AppEngineConfig represents a configuration for an
|
||||
// App Engine application's Google service account.
|
||||
type AppEngineConfig struct {
|
||||
// Default transport to be used while constructing
|
||||
// Transport represents the default transport to be used while constructing
|
||||
// oauth2.Transport instances from this configuration.
|
||||
Transport *urlfetch.Transport
|
||||
context appengine.Context
|
||||
scopes []string
|
||||
|
||||
context appengine.Context
|
||||
scopes []string
|
||||
}
|
||||
|
||||
// NewAppEngineConfig creates a new AppEngineConfig for the
|
||||
// provided auth scopes.
|
||||
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
||||
return &AppEngineConfig{context: context, scopes: scopes}
|
||||
return &AppEngineConfig{
|
||||
Transport: &urlfetch.Transport{
|
||||
Context: context,
|
||||
Deadline: 0,
|
||||
AllowInvalidServerCertificate: false,
|
||||
},
|
||||
context: context,
|
||||
scopes: scopes,
|
||||
}
|
||||
}
|
||||
|
||||
// NewTransport returns a transport that authorizes
|
||||
// the requests with the application's service account.
|
||||
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
||||
if c.Transport != nil {
|
||||
return oauth2.NewTransport(c.Transport, c, nil)
|
||||
}
|
||||
transport := &urlfetch.Transport{
|
||||
Context: c.context,
|
||||
Deadline: 0,
|
||||
AllowInvalidServerCertificate: false,
|
||||
}
|
||||
return oauth2.NewTransport(transport, c, nil)
|
||||
return oauth2.NewTransport(c.Transport, c, nil)
|
||||
}
|
||||
|
||||
// FetchToken fetches a new access token for the provided scopes.
|
||||
|
||||
@@ -3,29 +3,40 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/golang/oauth2"
|
||||
"google.golang.org/appengine"
|
||||
"google.golang.org/appengine/urlfetch"
|
||||
)
|
||||
|
||||
// AppEngineConfig represents a configuration for an
|
||||
// App Engine application's Google service account.
|
||||
type AppEngineConfig struct {
|
||||
context appengine.Context
|
||||
scopes []string
|
||||
// Transport represents the default transport to be used while constructing
|
||||
// oauth2.Transport instances from this configuration.
|
||||
Transport *urlfetch.Transport
|
||||
context appengine.Context
|
||||
|
||||
scopes []string
|
||||
}
|
||||
|
||||
// NewAppEngineConfig creates a new AppEngineConfig for the
|
||||
// provided auth scopes.
|
||||
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
|
||||
return &AppEngineConfig{context: context, scopes: scopes}
|
||||
return &AppEngineConfig{
|
||||
Transport: &urlfetch.Transport{
|
||||
Context: context,
|
||||
Deadline: 0,
|
||||
AllowInvalidServerCertificate: false,
|
||||
},
|
||||
context: context,
|
||||
scopes: scopes,
|
||||
}
|
||||
}
|
||||
|
||||
// NewTransport returns a transport that authorizes
|
||||
// the requests with the application's service account.
|
||||
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
|
||||
return oauth2.NewTransport(http.DefaultTransport, c, nil)
|
||||
return oauth2.NewTransport(c.Transport, c, nil)
|
||||
}
|
||||
|
||||
// FetchToken fetches a new access token for the provided scopes.
|
||||
|
||||
@@ -37,6 +37,15 @@ type metaTokenRespBody struct {
|
||||
// ComputeEngineConfig represents a OAuth 2.0 consumer client
|
||||
// running on Google Compute Engine.
|
||||
type ComputeEngineConfig struct {
|
||||
// Client is the default HTTP client to be used while retrieving
|
||||
// tokens from the OAuth 2.0 provider.
|
||||
Client *http.Client
|
||||
|
||||
// Transport represents the default round tripper to be used
|
||||
// while constructing new oauth2.Transport instances from
|
||||
// this configuration.
|
||||
Transport http.RoundTripper
|
||||
|
||||
account string
|
||||
}
|
||||
|
||||
@@ -56,12 +65,16 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
|
||||
// from Google Compute Engine instance's metaserver. If no account is
|
||||
// provided, default is used.
|
||||
func NewComputeEngineConfig(account string) *ComputeEngineConfig {
|
||||
return &ComputeEngineConfig{account: account}
|
||||
return &ComputeEngineConfig{
|
||||
Client: http.DefaultClient,
|
||||
Transport: http.DefaultTransport,
|
||||
account: account,
|
||||
}
|
||||
}
|
||||
|
||||
// NewTransport creates an authorized transport.
|
||||
func (c *ComputeEngineConfig) NewTransport() *oauth2.Transport {
|
||||
return oauth2.NewTransport(http.DefaultTransport, c, nil)
|
||||
return oauth2.NewTransport(c.Transport, c, nil)
|
||||
}
|
||||
|
||||
// FetchToken retrieves a new access token via metadata server.
|
||||
@@ -76,7 +89,7 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
|
||||
return
|
||||
}
|
||||
req.Header.Add("X-Google-Metadata-Request", "True")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user