| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package internal | |
| 6 | |
| 7 import ( | |
| 8 "io/ioutil" | |
| 9 | |
| 10 "golang.org/x/net/context" | |
| 11 "golang.org/x/oauth2/google" | |
| 12 "golang.org/x/oauth2/jwt" | |
| 13 ) | |
| 14 | |
| 15 type serviceAccountTokenProvider struct { | |
| 16 oauthTokenProvider | |
| 17 | |
| 18 ctx context.Context | |
| 19 config *jwt.Config | |
| 20 } | |
| 21 | |
| 22 // NewServiceAccountTokenProvider returns TokenProvider that supports service ac
counts. | |
| 23 func NewServiceAccountTokenProvider(ctx context.Context, credsPath string, scope
s []string) (TokenProvider, error) { | |
| 24 buf, err := ioutil.ReadFile(credsPath) | |
| 25 if err != nil { | |
| 26 return nil, err | |
| 27 } | |
| 28 config, err := google.JWTConfigFromJSON(buf, scopes...) | |
| 29 if err != nil { | |
| 30 return nil, err | |
| 31 } | |
| 32 return &serviceAccountTokenProvider{ | |
| 33 oauthTokenProvider: oauthTokenProvider{ | |
| 34 interactive: false, | |
| 35 tokenFlavor: "service_account", | |
| 36 }, | |
| 37 ctx: ctx, | |
| 38 config: config, | |
| 39 }, nil | |
| 40 } | |
| 41 | |
| 42 func (p *serviceAccountTokenProvider) MintToken() (Token, error) { | |
| 43 src := p.config.TokenSource(p.ctx) | |
| 44 tok, err := src.Token() | |
| 45 if err != nil { | |
| 46 return nil, err | |
| 47 } | |
| 48 return makeToken(tok), nil | |
| 49 } | |
| 50 | |
| 51 func (p *serviceAccountTokenProvider) RefreshToken(Token) (Token, error) { | |
| 52 // JWT tokens are self sufficient, there's no need for refresh_token. Mi
nting | |
| 53 // a token and "refreshing" it is a same thing. | |
| 54 return p.MintToken() | |
| 55 } | |
| OLD | NEW |