Skip to content

Commit

Permalink
fix(auth/oauth2adapt): adapt Token Types to be translated (#9801)
Browse files Browse the repository at this point in the history
Related: #9800
  • Loading branch information
codyoss committed Apr 18, 2024
1 parent acd3e2d commit 70f4115
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion auth/oauth2adapt/go.mod
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions 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=
Expand Down
2 changes: 2 additions & 0 deletions auth/oauth2adapt/oauth2adapt.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -77,6 +78,7 @@ func (ts *tokenSourceAdapter) Token() (*oauth2.Token, error) {
}
return &oauth2.Token{
AccessToken: tok.Value,
TokenType: tok.Type,
Expiry: tok.Expiry,
}, nil
}
Expand Down
43 changes: 27 additions & 16 deletions auth/oauth2adapt/oauth2adapt_test.go
Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -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)
}
})
}
Expand All @@ -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",
Expand Down Expand Up @@ -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)
}
})
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -244,7 +253,7 @@ func TestOauth2CredentialsFromAuthCredentials(t *testing.T) {
}

type tokenSource struct {
token string
token *oauth2.Token
err error
}

Expand All @@ -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
}

Expand All @@ -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
}

0 comments on commit 70f4115

Please sign in to comment.