Merge branch 'httpclient'

This commit is contained in:
Burcu Dogan
2014-09-03 18:04:35 -07:00
5 changed files with 79 additions and 33 deletions

30
jwt.go
View File

@@ -58,11 +58,9 @@ func NewJWTConfig(opts *JWTOptions, aud string) (*JWTConfig, error) {
return nil, err
}
return &JWTConfig{
Client: http.DefaultClient,
Transport: http.DefaultTransport,
opts: opts,
aud: audURL,
key: parsedKey,
opts: opts,
aud: audURL,
key: parsedKey,
}, nil
}
@@ -73,7 +71,7 @@ type JWTConfig struct {
// tokens from the OAuth 2.0 provider.
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
// this configuration.
Transport http.RoundTripper
@@ -86,13 +84,13 @@ type JWTConfig struct {
// NewTransport creates a transport that is authorize with the
// parent JWT configuration.
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
// the client and impersonates the specified user.
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
@@ -124,7 +122,7 @@ func (c *JWTConfig) FetchToken(existing *Token) (*Token, error) {
v.Set("assertion", payload)
// 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 {
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
}
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
// 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