Export a credentials.TokenSource type that wraps an oauth2.TokenSource as a credentials.Credentials.

Fixes #153.
This commit is contained in:
David Symonds
2015-04-08 10:56:46 +10:00
parent 343e505904
commit f2936c474c

View File

@ -181,15 +181,13 @@ func NewServerTLSFromFile(certFile, keyFile string) (TransportAuthenticator, err
}, nil }, nil
} }
// computeEngine represents credentials for the built-in service account for // TokenSource supplies credentials from an oauth2.TokenSource.
// the currently running Google Compute Engine (GCE) instance. It uses the type TokenSource struct {
// metadata server to get access tokens. oauth2.TokenSource
type computeEngine struct {
ts oauth2.TokenSource
} }
func (c computeEngine) GetRequestMetadata(ctx context.Context) (map[string]string, error) { func (ts TokenSource) GetRequestMetadata(ctx context.Context) (map[string]string, error) {
token, err := c.ts.Token() token, err := ts.Token()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -201,10 +199,9 @@ func (c computeEngine) GetRequestMetadata(ctx context.Context) (map[string]strin
// NewComputeEngine constructs the credentials that fetches access tokens from // NewComputeEngine constructs the credentials that fetches access tokens from
// Google Compute Engine (GCE)'s metadata server. It is only valid to use this // Google Compute Engine (GCE)'s metadata server. It is only valid to use this
// if your program is running on a GCE instance. // if your program is running on a GCE instance.
// TODO(dsymonds): Deprecate and remove this.
func NewComputeEngine() Credentials { func NewComputeEngine() Credentials {
return computeEngine{ return TokenSource{google.ComputeTokenSource("")}
ts: google.ComputeTokenSource(""),
}
} }
// serviceAccount represents credentials via JWT signing key. // serviceAccount represents credentials via JWT signing key.