forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add support for providing custom certificate URL (googlea…
…pis#11006) fixes: googleapis#11005
- Loading branch information
Showing
2 changed files
with
127 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,51 +49,70 @@ var ( | |
func TestValidateRS256(t *testing.T) { | ||
idToken, pk := createRS256JWT(t) | ||
tests := []struct { | ||
name string | ||
keyID string | ||
n *big.Int | ||
e int | ||
nowFunc func() time.Time | ||
wantErr bool | ||
name string | ||
keyID string | ||
certsURL string | ||
n *big.Int | ||
e int | ||
nowFunc func() time.Time | ||
wantErr bool | ||
wantCertsURL string | ||
}{ | ||
{ | ||
name: "works", | ||
keyID: keyID, | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
name: "works", | ||
keyID: keyID, | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
wantCertsURL: googleSACertsURL, | ||
}, | ||
{ | ||
name: "no matching key", | ||
keyID: "5678", | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
name: "works with custom certs url", | ||
keyID: keyID, | ||
certsURL: "https://www.googleapis.com/service_accounts/v1/jwk/[email protected]", | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
wantCertsURL: "https://www.googleapis.com/service_accounts/v1/jwk/[email protected]", | ||
}, | ||
{ | ||
name: "sig does not match", | ||
keyID: keyID, | ||
n: new(big.Int).SetBytes([]byte("42")), | ||
e: 42, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
name: "no matching key", | ||
keyID: "5678", | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
wantCertsURL: googleSACertsURL, | ||
}, | ||
{ | ||
name: "token expired", | ||
keyID: keyID, | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: afterExp, | ||
wantErr: true, | ||
name: "sig does not match", | ||
keyID: keyID, | ||
n: new(big.Int).SetBytes([]byte("42")), | ||
e: 42, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
wantCertsURL: googleSACertsURL, | ||
}, | ||
{ | ||
name: "token expired", | ||
keyID: keyID, | ||
n: pk.N, | ||
e: pk.E, | ||
nowFunc: afterExp, | ||
wantErr: true, | ||
wantCertsURL: googleSACertsURL, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client := &http.Client{ | ||
Transport: RoundTripFn(func(req *http.Request) *http.Response { | ||
if req.URL.String() != tt.wantCertsURL { | ||
t.Fatalf("Invalid request uri, want %v got %v", tt.wantCertsURL, req.URL.String()) | ||
} | ||
cr := certResponse{ | ||
Keys: []jwk{ | ||
{ | ||
|
@@ -119,7 +138,8 @@ func TestValidateRS256(t *testing.T) { | |
now = tt.nowFunc | ||
|
||
v, err := NewValidator(&ValidatorOptions{ | ||
Client: client, | ||
Client: client, | ||
RSA256CertsURL: tt.certsURL, | ||
}) | ||
if err != nil { | ||
t.Fatalf("NewValidator(...) = %q, want nil", err) | ||
|
@@ -162,50 +182,69 @@ func TestValidateRS256(t *testing.T) { | |
func TestValidateES256(t *testing.T) { | ||
idToken, pk := createES256JWT(t) | ||
tests := []struct { | ||
name string | ||
keyID string | ||
x *big.Int | ||
y *big.Int | ||
nowFunc func() time.Time | ||
wantErr bool | ||
name string | ||
keyID string | ||
certsURL string | ||
x *big.Int | ||
y *big.Int | ||
nowFunc func() time.Time | ||
wantErr bool | ||
wantCertsURL string | ||
}{ | ||
{ | ||
name: "works", | ||
keyID: keyID, | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
name: "works", | ||
keyID: keyID, | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
wantCertsURL: googleIAPCertsURL, | ||
}, | ||
{ | ||
name: "no matching key", | ||
keyID: "5678", | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
name: "works with custom certs url", | ||
keyID: keyID, | ||
certsURL: "http://example.com", | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: beforeExp, | ||
wantErr: false, | ||
wantCertsURL: "http://example.com", | ||
}, | ||
{ | ||
name: "sig does not match", | ||
keyID: keyID, | ||
x: new(big.Int), | ||
y: new(big.Int), | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
name: "no matching key", | ||
keyID: "5678", | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
wantCertsURL: googleIAPCertsURL, | ||
}, | ||
{ | ||
name: "token expired", | ||
keyID: keyID, | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: afterExp, | ||
wantErr: true, | ||
name: "sig does not match", | ||
keyID: keyID, | ||
x: new(big.Int), | ||
y: new(big.Int), | ||
nowFunc: beforeExp, | ||
wantErr: true, | ||
wantCertsURL: googleIAPCertsURL, | ||
}, | ||
{ | ||
name: "token expired", | ||
keyID: keyID, | ||
x: pk.X, | ||
y: pk.Y, | ||
nowFunc: afterExp, | ||
wantErr: true, | ||
wantCertsURL: googleIAPCertsURL, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client := &http.Client{ | ||
Transport: RoundTripFn(func(req *http.Request) *http.Response { | ||
if req.URL.String() != tt.wantCertsURL { | ||
t.Fatalf("Invalid request uri, want %v got %v", tt.wantCertsURL, req.URL.String()) | ||
} | ||
cr := certResponse{ | ||
Keys: []jwk{ | ||
{ | ||
|
@@ -231,7 +270,8 @@ func TestValidateES256(t *testing.T) { | |
now = tt.nowFunc | ||
|
||
v, err := NewValidator(&ValidatorOptions{ | ||
Client: client, | ||
Client: client, | ||
ES256CertsURL: tt.certsURL, | ||
}) | ||
if err != nil { | ||
t.Fatalf("NewValidator(...) = %q, want nil", err) | ||
|