Skip to content

Commit

Permalink
google: add CredentialsParams.EarlyTokenRefresh
Browse files Browse the repository at this point in the history
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 <codyoss@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
  • Loading branch information
codyoss authored and rolandshoemaker committed Mar 29, 2023
1 parent 1e7f329 commit 4abfd87
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion google/default.go
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"runtime"
"time"

"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -68,6 +69,14 @@ type CredentialsParams struct {
// The OAuth2 TokenURL default override. This value overrides the default TokenURL,
// unless explicitly specified by the credentials config file. Optional.
TokenURL string

// EarlyTokenRefresh is the amount of time before a token expires that a new
// token will be preemptively fetched. If unset the default value is 10
// seconds.
//
// Note: This option is currently only respected when using credentials
// fetched from the GCE metadata server.
EarlyTokenRefresh time.Duration
}

func (params CredentialsParams) deepCopy() CredentialsParams {
Expand Down Expand Up @@ -155,7 +164,7 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
id, _ := metadata.ProjectID()
return &Credentials{
ProjectID: id,
TokenSource: ComputeTokenSource("", params.Scopes...),
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
}, nil
}

Expand Down
6 changes: 5 additions & 1 deletion google/google.go
Expand Up @@ -231,7 +231,11 @@ func (f *credentialsFile) tokenSource(ctx context.Context, params CredentialsPar
// Further information about retrieving access tokens from the GCE metadata
// server can be found at https://cloud.google.com/compute/docs/authentication.
func ComputeTokenSource(account string, scope ...string) oauth2.TokenSource {
return oauth2.ReuseTokenSource(nil, computeSource{account: account, scopes: scope})
return computeTokenSource(account, 0, scope...)
}

func computeTokenSource(account string, earlyExpiry time.Duration, scope ...string) oauth2.TokenSource {
return oauth2.ReuseTokenSourceWithExpiry(nil, computeSource{account: account, scopes: scope}, earlyExpiry)
}

type computeSource struct {
Expand Down

0 comments on commit 4abfd87

Please sign in to comment.