From 70f411555ebbf2b71e6d425cc8d2030644c6b438 Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:22:22 -0500 Subject: [PATCH] fix(auth/oauth2adapt): adapt Token Types to be translated (#9801) Related: #9800 --- auth/oauth2adapt/go.mod | 2 +- auth/oauth2adapt/go.sum | 4 +-- auth/oauth2adapt/oauth2adapt.go | 2 ++ auth/oauth2adapt/oauth2adapt_test.go | 43 +++++++++++++++++----------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/auth/oauth2adapt/go.mod b/auth/oauth2adapt/go.mod index f30b79f5bcd..517077c3ce4 100644 --- a/auth/oauth2adapt/go.mod +++ b/auth/oauth2adapt/go.mod @@ -3,7 +3,7 @@ module cloud.google.com/go/auth/oauth2adapt go 1.19 require ( - cloud.google.com/go/auth v0.2.0 + cloud.google.com/go/auth v0.2.1 github.com/google/go-cmp v0.6.0 golang.org/x/oauth2 v0.19.0 ) diff --git a/auth/oauth2adapt/go.sum b/auth/oauth2adapt/go.sum index 8b29eb267e3..9d157014f67 100644 --- a/auth/oauth2adapt/go.sum +++ b/auth/oauth2adapt/go.sum @@ -1,5 +1,5 @@ -cloud.google.com/go/auth v0.2.0 h1:y6oTcpMSbOcXbwYgUUrvI+mrQ2xbrcdpPgtVbCGTLTk= -cloud.google.com/go/auth v0.2.0/go.mod h1:+yb+oy3/P0geX6DLKlqiGHARGR6EX2GRtYCzWOCQSbU= +cloud.google.com/go/auth v0.2.1 h1:RMl6PI2MH1Qc3CM7XNJJHGwbC4WHQppSAjL0Cvu/M/g= +cloud.google.com/go/auth v0.2.1/go.mod h1:khQRBNrvNoHiHhV1iu2x8fSnlNbCaVHilznW5MAI5GY= cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= diff --git a/auth/oauth2adapt/oauth2adapt.go b/auth/oauth2adapt/oauth2adapt.go index 897b59f343b..9835ac571cf 100644 --- a/auth/oauth2adapt/oauth2adapt.go +++ b/auth/oauth2adapt/oauth2adapt.go @@ -49,6 +49,7 @@ func (tp *tokenProviderAdapter) Token(context.Context) (*auth.Token, error) { } return &auth.Token{ Value: tok.AccessToken, + Type: tok.Type(), Expiry: tok.Expiry, }, nil } @@ -77,6 +78,7 @@ func (ts *tokenSourceAdapter) Token() (*oauth2.Token, error) { } return &oauth2.Token{ AccessToken: tok.Value, + TokenType: tok.Type, Expiry: tok.Expiry, }, nil } diff --git a/auth/oauth2adapt/oauth2adapt_test.go b/auth/oauth2adapt/oauth2adapt_test.go index 5ea923bc8fb..d408c90c568 100644 --- a/auth/oauth2adapt/oauth2adapt_test.go +++ b/auth/oauth2adapt/oauth2adapt_test.go @@ -29,12 +29,12 @@ import ( func TestTokenProviderFromTokenSource(t *testing.T) { tests := []struct { name string - token string + token *oauth2.Token err error }{ { name: "working token", - token: "fakeToken", + token: &oauth2.Token{AccessToken: "fakeToken", TokenType: "Basic"}, err: nil, }, { @@ -72,8 +72,11 @@ func TestTokenProviderFromTokenSource(t *testing.T) { } return } - if tok.Value != tt.token { - t.Errorf("got %q, want %q", tok.Value, tt.token) + if tok.Value != tt.token.AccessToken { + t.Errorf("got %q, want %q", tok.Value, tt.token.AccessToken) + } + if tok.Type != tt.token.TokenType { + t.Errorf("got %q, want %q", tok.Type, tt.token.TokenType) } }) } @@ -82,13 +85,16 @@ func TestTokenProviderFromTokenSource(t *testing.T) { func TestTokenSourceFromTokenProvider(t *testing.T) { tests := []struct { name string - token string + token *auth.Token err error }{ { - name: "working token", - token: "fakeToken", - err: nil, + name: "working token", + token: &auth.Token{ + Value: "fakeToken", + Type: "Basic", + }, + err: nil, }, { name: "coverts err", @@ -134,8 +140,11 @@ func TestTokenSourceFromTokenProvider(t *testing.T) { } return } - if tok.AccessToken != tt.token { - t.Errorf("got %q, want %q", tok.AccessToken, tt.token) + if tok.AccessToken != tt.token.Value { + t.Errorf("got %q, want %q", tok.AccessToken, tt.token.Value) + } + if tok.TokenType != tt.token.Type { + t.Errorf("got %q, want %q", tok.TokenType, tt.token.Type) } }) } @@ -145,7 +154,7 @@ func TestAuthCredentialsFromOauth2Credentials(t *testing.T) { ctx := context.Background() inputCreds := &google.Credentials{ ProjectID: "test_project", - TokenSource: tokenSource{token: "token"}, + TokenSource: tokenSource{token: &oauth2.Token{AccessToken: "token"}}, JSON: []byte("json"), UniverseDomainProvider: func() (string, error) { return "domain", nil @@ -197,7 +206,7 @@ func TestOauth2CredentialsFromAuthCredentials(t *testing.T) { ProjectIDProvider: auth.CredentialsPropertyFunc(func(ctx context.Context) (string, error) { return "project", nil }), - TokenProvider: tokenProvider{token: "token"}, + TokenProvider: tokenProvider{token: &auth.Token{Value: "token"}}, JSON: []byte("json"), UniverseDomainProvider: auth.CredentialsPropertyFunc(func(ctx context.Context) (string, error) { return "domain", nil @@ -244,7 +253,7 @@ func TestOauth2CredentialsFromAuthCredentials(t *testing.T) { } type tokenSource struct { - token string + token *oauth2.Token err error } @@ -253,12 +262,13 @@ func (ts tokenSource) Token() (*oauth2.Token, error) { return nil, ts.err } return &oauth2.Token{ - AccessToken: ts.token, + AccessToken: ts.token.AccessToken, + TokenType: ts.token.TokenType, }, nil } type tokenProvider struct { - token string + token *auth.Token err error } @@ -267,6 +277,7 @@ func (tp tokenProvider) Token(context.Context) (*auth.Token, error) { return nil, tp.err } return &auth.Token{ - Value: tp.token, + Value: tp.token.Value, + Type: tp.token.Type, }, nil }