@@ -7,7 +7,6 @@ package google
7
7
import (
8
8
"context"
9
9
"fmt"
10
- "strconv"
11
10
"sync"
12
11
"time"
13
12
@@ -17,47 +16,49 @@ import (
17
16
18
17
// DelegateTokenSource allows a TokenSource issued to a user or
19
18
// service account to impersonate another. The target service account
20
- // must grant the orginating credential principal the
19
+ // must grant the orginating principal the
21
20
// "Service Account Token Creator" IAM role:
22
21
// https://cloud.google.com/iam/docs/service-accounts#the_service_account_token_creator_role
23
22
//
24
23
// rootSource (TokenSource): The root TokenSource
25
- // used as to acquire the delegated identity TokenSource.
26
- // rootSource *must* include scopes that includes
24
+ // used as to acquire the target identity TokenSource.
25
+ // rootSource *must* include scopes that contains
27
26
// "https://www.googleapis.com/auth/iam"
27
+ // or
28
+ // "https://www.googleapis.com/auth/cloud-platform"
28
29
// principal (string): The service account to impersonate.
29
30
// new_scopes ([]string): Scopes to request during the
30
31
// authorization grant.
31
32
// delegates ([]string): The chained list of delegates required
32
33
// to grant the final access_token.
33
- // lifetime (int ): Number of seconds the delegated credential should
34
+ // lifetime (time.Duration ): Number of seconds the delegated credential should
34
35
// be valid for (upto 3600).
35
36
//
36
37
// Usage:
37
38
// principal := "[email protected] "
38
- // lifetime := 30
39
+ // lifetime := 30 * time.Second
39
40
// delegates := []string{}
40
41
// newScopes := []string{storage.ScopeReadOnly}
41
42
// rootTokenSource, err := google.DefaultTokenSource(ctx,
42
43
// "https://www.googleapis.com/auth/iam")
43
44
// delegatetokenSource, err := google.DelegateTokenSource(ctx,
44
45
// rootTokenSource,
45
- // principal, lifetime, delegates, newScopes)
46
+ // principal, lifetime, delegates, newScopes)
46
47
// storeageClient, _ = storage.NewClient(ctx,
47
48
// option.WithTokenSource(delegatetokenSource))
48
-
49
+ //
49
50
// Note that this is not a standard OAuth flow, but rather uses Google Cloud
50
51
// IAMCredentials API to exchange one oauth token for an impersonated account
51
52
// see: https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/generateAccessToken
52
53
func DelegateTokenSource (ctx context.Context , rootSource oauth2.TokenSource ,
53
- principal string , lifetime int , delegates []string ,
54
+ principal string , lifetime time. Duration , delegates []string ,
54
55
newScopes []string ) (oauth2.TokenSource , error ) {
55
56
56
57
return & delegateTokenSource {
57
58
ctx : ctx ,
58
59
rootSource : rootSource ,
59
60
principal : principal ,
60
- lifetime : strconv . Itoa ( lifetime ) + "s" ,
61
+ lifetime : lifetime ,
61
62
delegates : delegates ,
62
63
newScopes : newScopes ,
63
64
}, nil
@@ -67,7 +68,7 @@ type delegateTokenSource struct {
67
68
ctx context.Context
68
69
rootSource oauth2.TokenSource
69
70
principal string
70
- lifetime string
71
+ lifetime time. Duration
71
72
delegates []string
72
73
newScopes []string
73
74
}
@@ -86,26 +87,26 @@ func (ts *delegateTokenSource) Token() (*oauth2.Token, error) {
86
87
return tok , nil
87
88
}
88
89
89
- client := oauth2 .NewClient (context . Background () , ts .rootSource )
90
+ client := oauth2 .NewClient (ts . ctx , ts .rootSource )
90
91
91
92
service , err := iamcredentials .New (client )
92
93
if err != nil {
93
- return nil , fmt .Errorf ("Error creating IAMCredentials: %v" , err )
94
+ return nil , fmt .Errorf ("google: Error creating IAMCredentials: %v" , err )
94
95
}
95
96
name := "projects/-/serviceAccounts/" + ts .principal
96
97
tokenRequest := & iamcredentials.GenerateAccessTokenRequest {
97
- Lifetime : ts .lifetime ,
98
+ Lifetime : ts .lifetime . String () ,
98
99
Delegates : ts .delegates ,
99
100
Scope : ts .newScopes ,
100
101
}
101
102
at , err := service .Projects .ServiceAccounts .GenerateAccessToken (name , tokenRequest ).Do ()
102
103
if err != nil {
103
- return nil , fmt .Errorf ("Error calling GenerateAccessToken: %v" , err )
104
+ return nil , fmt .Errorf ("google: Error calling iamcredentials. GenerateAccessToken: %v" , err )
104
105
}
105
106
106
107
expireAt , err := time .Parse (time .RFC3339 , at .ExpireTime )
107
108
if err != nil {
108
- return nil , fmt .Errorf ("Error parsing ExpireTime: %v" , err )
109
+ return nil , fmt .Errorf ("google: Error parsing ExpireTime from iamcredentials : %v" , err )
109
110
}
110
111
111
112
tok = & oauth2.Token {
0 commit comments