diff options
author | Niall Sheridan <nsheridan@gmail.com> | 2016-10-08 16:02:50 -0500 |
---|---|---|
committer | Niall Sheridan <nsheridan@gmail.com> | 2016-10-08 16:02:50 -0500 |
commit | baf7141d1dd0f99d561a2197a909c66dd389809d (patch) | |
tree | 92c176f713b0b28893344261b1e567db5e30ba79 /vendor/google.golang.org/api/transport | |
parent | 696aebffe56853345d679d4d2b3051236423c6db (diff) |
Update dependencies
Diffstat (limited to 'vendor/google.golang.org/api/transport')
-rw-r--r-- | vendor/google.golang.org/api/transport/dial.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/transport/dial.go b/vendor/google.golang.org/api/transport/dial.go index a7d058b..c054460 100644 --- a/vendor/google.golang.org/api/transport/dial.go +++ b/vendor/google.golang.org/api/transport/dial.go @@ -20,6 +20,7 @@ package transport import ( "errors" "fmt" + "io/ioutil" "net/http" "golang.org/x/net/context" @@ -48,6 +49,13 @@ func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*http.Clie if o.HTTPClient != nil { return o.HTTPClient, o.Endpoint, nil } + if o.ServiceAccountJSONFilename != "" { + ts, err := serviceAcctTokenSource(ctx, o.ServiceAccountJSONFilename, o.Scopes...) + if err != nil { + return nil, "", err + } + o.TokenSource = ts + } if o.TokenSource == nil { var err error o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...) @@ -74,6 +82,13 @@ func DialGRPC(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientCon if o.GRPCConn != nil { return o.GRPCConn, nil } + if o.ServiceAccountJSONFilename != "" { + ts, err := serviceAcctTokenSource(ctx, o.ServiceAccountJSONFilename, o.Scopes...) + if err != nil { + return nil, err + } + o.TokenSource = ts + } if o.TokenSource == nil { var err error o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...) @@ -95,3 +110,15 @@ func DialGRPC(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientCon } return grpc.DialContext(ctx, o.Endpoint, grpcOpts...) } + +func serviceAcctTokenSource(ctx context.Context, filename string, scope ...string) (oauth2.TokenSource, error) { + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, fmt.Errorf("cannot read service account file: %v", err) + } + cfg, err := google.JWTConfigFromJSON(data, scope...) + if err != nil { + return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err) + } + return cfg.TokenSource(ctx), nil +} |