Skip to content

Commit 4abfd87

Browse files
codyossrolandshoemaker
authored andcommitted
google: add CredentialsParams.EarlyTokenRefresh
This option is a followup to to cl/479676 where an option was added to configure the preemptive token refresh. Currently the option in this package is only being used by compute credentials. In the future we can support more/all auth flows but that would require a lot of new surfaces to be added. Compute credentials are currently the only case where we are expirencing the need to configure this setting. Change-Id: Ib78ca4beec44d0fe030ae81e84c8fcc4924793ba Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/479956 Run-TryBot: Cody Oss <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent 1e7f329 commit 4abfd87

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

google/default.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path/filepath"
1515
"runtime"
16+
"time"
1617

1718
"cloud.google.com/go/compute/metadata"
1819
"golang.org/x/oauth2"
@@ -68,6 +69,14 @@ type CredentialsParams struct {
6869
// The OAuth2 TokenURL default override. This value overrides the default TokenURL,
6970
// unless explicitly specified by the credentials config file. Optional.
7071
TokenURL string
72+
73+
// EarlyTokenRefresh is the amount of time before a token expires that a new
74+
// token will be preemptively fetched. If unset the default value is 10
75+
// seconds.
76+
//
77+
// Note: This option is currently only respected when using credentials
78+
// fetched from the GCE metadata server.
79+
EarlyTokenRefresh time.Duration
7180
}
7281

7382
func (params CredentialsParams) deepCopy() CredentialsParams {
@@ -155,7 +164,7 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
155164
id, _ := metadata.ProjectID()
156165
return &Credentials{
157166
ProjectID: id,
158-
TokenSource: ComputeTokenSource("", params.Scopes...),
167+
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
159168
}, nil
160169
}
161170

google/google.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ func (f *credentialsFile) tokenSource(ctx context.Context, params CredentialsPar
231231
// Further information about retrieving access tokens from the GCE metadata
232232
// server can be found at https://cloud.google.com/compute/docs/authentication.
233233
func ComputeTokenSource(account string, scope ...string) oauth2.TokenSource {
234-
return oauth2.ReuseTokenSource(nil, computeSource{account: account, scopes: scope})
234+
return computeTokenSource(account, 0, scope...)
235+
}
236+
237+
func computeTokenSource(account string, earlyExpiry time.Duration, scope ...string) oauth2.TokenSource {
238+
return oauth2.ReuseTokenSourceWithExpiry(nil, computeSource{account: account, scopes: scope}, earlyExpiry)
235239
}
236240

237241
type computeSource struct {

0 commit comments

Comments
 (0)