Skip to content

Commit e8ef4a0

Browse files
committed
update ctx; duration
1 parent e19e35f commit e8ef4a0

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

google/delegate.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package google
77
import (
88
"context"
99
"fmt"
10-
"strconv"
1110
"sync"
1211
"time"
1312

@@ -17,47 +16,49 @@ import (
1716

1817
// DelegateTokenSource allows a TokenSource issued to a user or
1918
// service account to impersonate another. The target service account
20-
// must grant the orginating credential principal the
19+
// must grant the orginating principal the
2120
// "Service Account Token Creator" IAM role:
2221
// https://cloud.google.com/iam/docs/service-accounts#the_service_account_token_creator_role
2322
//
2423
// 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
2726
// "https://www.googleapis.com/auth/iam"
27+
// or
28+
// "https://www.googleapis.com/auth/cloud-platform"
2829
// principal (string): The service account to impersonate.
2930
// new_scopes ([]string): Scopes to request during the
3031
// authorization grant.
3132
// delegates ([]string): The chained list of delegates required
3233
// 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
3435
// be valid for (upto 3600).
3536
//
3637
// Usage:
3738
// principal := "[email protected]"
38-
// lifetime := 30
39+
// lifetime := 30 * time.Second
3940
// delegates := []string{}
4041
// newScopes := []string{storage.ScopeReadOnly}
4142
// rootTokenSource, err := google.DefaultTokenSource(ctx,
4243
// "https://www.googleapis.com/auth/iam")
4344
// delegatetokenSource, err := google.DelegateTokenSource(ctx,
4445
// rootTokenSource,
45-
// principal, lifetime, delegates, newScopes)
46+
// principal, lifetime, delegates, newScopes)
4647
// storeageClient, _ = storage.NewClient(ctx,
4748
// option.WithTokenSource(delegatetokenSource))
48-
49+
//
4950
// Note that this is not a standard OAuth flow, but rather uses Google Cloud
5051
// IAMCredentials API to exchange one oauth token for an impersonated account
5152
// see: https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/generateAccessToken
5253
func DelegateTokenSource(ctx context.Context, rootSource oauth2.TokenSource,
53-
principal string, lifetime int, delegates []string,
54+
principal string, lifetime time.Duration, delegates []string,
5455
newScopes []string) (oauth2.TokenSource, error) {
5556

5657
return &delegateTokenSource{
5758
ctx: ctx,
5859
rootSource: rootSource,
5960
principal: principal,
60-
lifetime: strconv.Itoa(lifetime) + "s",
61+
lifetime: lifetime,
6162
delegates: delegates,
6263
newScopes: newScopes,
6364
}, nil
@@ -67,7 +68,7 @@ type delegateTokenSource struct {
6768
ctx context.Context
6869
rootSource oauth2.TokenSource
6970
principal string
70-
lifetime string
71+
lifetime time.Duration
7172
delegates []string
7273
newScopes []string
7374
}
@@ -86,26 +87,26 @@ func (ts *delegateTokenSource) Token() (*oauth2.Token, error) {
8687
return tok, nil
8788
}
8889

89-
client := oauth2.NewClient(context.Background(), ts.rootSource)
90+
client := oauth2.NewClient(ts.ctx, ts.rootSource)
9091

9192
service, err := iamcredentials.New(client)
9293
if err != nil {
93-
return nil, fmt.Errorf("Error creating IAMCredentials: %v", err)
94+
return nil, fmt.Errorf("google: Error creating IAMCredentials: %v", err)
9495
}
9596
name := "projects/-/serviceAccounts/" + ts.principal
9697
tokenRequest := &iamcredentials.GenerateAccessTokenRequest{
97-
Lifetime: ts.lifetime,
98+
Lifetime: ts.lifetime.String(),
9899
Delegates: ts.delegates,
99100
Scope: ts.newScopes,
100101
}
101102
at, err := service.Projects.ServiceAccounts.GenerateAccessToken(name, tokenRequest).Do()
102103
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)
104105
}
105106

106107
expireAt, err := time.Parse(time.RFC3339, at.ExpireTime)
107108
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)
109110
}
110111

111112
tok = &oauth2.Token{

0 commit comments

Comments
 (0)