@@ -35,17 +35,17 @@ var testConfig = Config{
35
35
}
36
36
37
37
var (
38
- baseCredsRequestBody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=street123&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
39
- baseCredsResponseBody = `{"access_token":"Sample.Access.Token","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
40
- correctAT = "Sample.Access.Token"
41
- expiry int64 = 234852
38
+ baseCredsRequestBody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=street123&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
39
+ baseCredsResponseBody = `{"access_token":"Sample.Access.Token","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
40
+ workforcePoolRequestBody = "audience=%2F%2Fiam.googleapis.com%2Flocations%2Feu%2FworkforcePools%2Fpool-id%2Fproviders%2Fprovider-id&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&options=%7B%22userProject%22%3A%22myProject%22%7D&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=street123&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
41
+ correctAT = "Sample.Access.Token"
42
+ expiry int64 = 234852
42
43
)
43
44
var (
44
45
testNow = func () time.Time { return time .Unix (expiry , 0 ) }
45
46
)
46
47
47
48
func TestToken (t * testing.T ) {
48
-
49
49
targetServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
50
50
if got , want := r .URL .String (), "/" ; got != want {
51
51
t .Errorf ("URL.String(): got %v but want %v" , got , want )
@@ -94,7 +94,59 @@ func TestToken(t *testing.T) {
94
94
if got , want := tok .Expiry , now ().Add (time .Duration (3600 )* time .Second ); got != want {
95
95
t .Errorf ("Unexpected Expiry: got %v, but wanted %v" , got , want )
96
96
}
97
+ }
98
+
99
+ func TestWorkforcePoolToken (t * testing.T ) {
100
+ targetServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
101
+ if got , want := r .URL .String (), "/" ; got != want {
102
+ t .Errorf ("URL.String(): got %v but want %v" , got , want )
103
+ }
104
+ headerAuth := r .Header .Get ("Authorization" )
105
+ if got , want := headerAuth , "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" ; got != want {
106
+ t .Errorf ("got %v but want %v" , got , want )
107
+ }
108
+ headerContentType := r .Header .Get ("Content-Type" )
109
+ if got , want := headerContentType , "application/x-www-form-urlencoded" ; got != want {
110
+ t .Errorf ("got %v but want %v" , got , want )
111
+ }
112
+ body , err := ioutil .ReadAll (r .Body )
113
+ if err != nil {
114
+ t .Fatalf ("Failed reading request body: %s." , err )
115
+ }
116
+ if got , want := string (body ), workforcePoolRequestBody ; got != want {
117
+ t .Errorf ("Unexpected exchange payload: got %v but want %v" , got , want )
118
+ }
119
+ w .Header ().Set ("Content-Type" , "application/json" )
120
+ w .Write ([]byte (baseCredsResponseBody ))
121
+ }))
122
+ defer targetServer .Close ()
97
123
124
+ testConfig .TokenURL = targetServer .URL
125
+ testConfig .WorkforcePoolUserProject = "myProject"
126
+ testConfig .Audience = "//iam.googleapis.com/locations/eu/workforcePools/pool-id/providers/provider-id"
127
+ ourTS := tokenSource {
128
+ ctx : context .Background (),
129
+ conf : & testConfig ,
130
+ }
131
+
132
+ oldNow := now
133
+ defer func () { now = oldNow }()
134
+ now = testNow
135
+
136
+ tok , err := ourTS .Token ()
137
+ if err != nil {
138
+ t .Fatalf ("Unexpected error: %e" , err )
139
+ }
140
+ if got , want := tok .AccessToken , correctAT ; got != want {
141
+ t .Errorf ("Unexpected access token: got %v, but wanted %v" , got , want )
142
+ }
143
+ if got , want := tok .TokenType , "Bearer" ; got != want {
144
+ t .Errorf ("Unexpected TokenType: got %v, but wanted %v" , got , want )
145
+ }
146
+
147
+ if got , want := tok .Expiry , now ().Add (time .Duration (3600 )* time .Second ); got != want {
148
+ t .Errorf ("Unexpected Expiry: got %v, but wanted %v" , got , want )
149
+ }
98
150
}
99
151
100
152
func TestValidateURLTokenURL (t * testing.T ) {
0 commit comments