@@ -6,14 +6,20 @@ package clientcredentials
6
6
7
7
import (
8
8
"context"
9
+ "encoding/base64"
10
+ "encoding/json"
9
11
"io"
10
12
"io/ioutil"
11
13
"net/http"
12
14
"net/http/httptest"
13
15
"net/url"
16
+ "strings"
14
17
"testing"
18
+ "time"
15
19
20
+ "golang.org/x/oauth2"
16
21
"golang.org/x/oauth2/internal"
22
+ "golang.org/x/oauth2/jws"
17
23
)
18
24
19
25
func newConf (serverURL string ) * Config {
@@ -113,6 +119,136 @@ func TestTokenRequest(t *testing.T) {
113
119
}
114
120
}
115
121
122
+ var dummyPrivateKey = []byte (`-----BEGIN RSA PRIVATE KEY-----
123
+ MIIEpAIBAAKCAQEAx4fm7dngEmOULNmAs1IGZ9Apfzh+BkaQ1dzkmbUgpcoghucE
124
+ DZRnAGd2aPyB6skGMXUytWQvNYav0WTR00wFtX1ohWTfv68HGXJ8QXCpyoSKSSFY
125
+ fuP9X36wBSkSX9J5DVgiuzD5VBdzUISSmapjKm+DcbRALjz6OUIPEWi1Tjl6p5RK
126
+ 1w41qdbmt7E5/kGhKLDuT7+M83g4VWhgIvaAXtnhklDAggilPPa8ZJ1IFe31lNlr
127
+ k4DRk38nc6sEutdf3RL7QoH7FBusI7uXV03DC6dwN1kP4GE7bjJhcRb/7jYt7CQ9
128
+ /E9Exz3c0yAp0yrTg0Fwh+qxfH9dKwN52S7SBwIDAQABAoIBAQCaCs26K07WY5Jt
129
+ 3a2Cw3y2gPrIgTCqX6hJs7O5ByEhXZ8nBwsWANBUe4vrGaajQHdLj5OKfsIDrOvn
130
+ 2NI1MqflqeAbu/kR32q3tq8/Rl+PPiwUsW3E6Pcf1orGMSNCXxeducF2iySySzh3
131
+ nSIhCG5uwJDWI7a4+9KiieFgK1pt/Iv30q1SQS8IEntTfXYwANQrfKUVMmVF9aIK
132
+ 6/WZE2yd5+q3wVVIJ6jsmTzoDCX6QQkkJICIYwCkglmVy5AeTckOVwcXL0jqw5Kf
133
+ 5/soZJQwLEyBoQq7Kbpa26QHq+CJONetPP8Ssy8MJJXBT+u/bSseMb3Zsr5cr43e
134
+ DJOhwsThAoGBAPY6rPKl2NT/K7XfRCGm1sbWjUQyDShscwuWJ5+kD0yudnT/ZEJ1
135
+ M3+KS/iOOAoHDdEDi9crRvMl0UfNa8MAcDKHflzxg2jg/QI+fTBjPP5GOX0lkZ9g
136
+ z6VePoVoQw2gpPFVNPPTxKfk27tEzbaffvOLGBEih0Kb7HTINkW8rIlzAoGBAM9y
137
+ 1yr+jvfS1cGFtNU+Gotoihw2eMKtIqR03Yn3n0PK1nVCDKqwdUqCypz4+ml6cxRK
138
+ J8+Pfdh7D+ZJd4LEG6Y4QRDLuv5OA700tUoSHxMSNn3q9As4+T3MUyYxWKvTeu3U
139
+ f2NWP9ePU0lV8ttk7YlpVRaPQmc1qwooBA/z/8AdAoGAW9x0HWqmRICWTBnpjyxx
140
+ QGlW9rQ9mHEtUotIaRSJ6K/F3cxSGUEkX1a3FRnp6kPLcckC6NlqdNgNBd6rb2rA
141
+ cPl/uSkZP42Als+9YMoFPU/xrrDPbUhu72EDrj3Bllnyb168jKLa4VBOccUvggxr
142
+ Dm08I1hgYgdN5huzs7y6GeUCgYEAj+AZJSOJ6o1aXS6rfV3mMRve9bQ9yt8jcKXw
143
+ 5HhOCEmMtaSKfnOF1Ziih34Sxsb7O2428DiX0mV/YHtBnPsAJidL0SdLWIapBzeg
144
+ KHArByIRkwE6IvJvwpGMdaex1PIGhx5i/3VZL9qiq/ElT05PhIb+UXgoWMabCp84
145
+ OgxDK20CgYAeaFo8BdQ7FmVX2+EEejF+8xSge6WVLtkaon8bqcn6P0O8lLypoOhd
146
+ mJAYH8WU+UAy9pecUnDZj14LAGNVmYcse8HFX71MoshnvCTFEPVo4rZxIAGwMpeJ
147
+ 5jgQ3slYLpqrGlcbLgUXBUgzEO684Wk/UV9DFPlHALVqCfXQ9dpJPg==
148
+ -----END RSA PRIVATE KEY-----` )
149
+
150
+ func TestTokenJWTRequest (t * testing.T ) {
151
+ var assertion string
152
+ audience := "audience1"
153
+ scopes := "scope1 scope2"
154
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
155
+ if r .URL .String () != "/token" {
156
+ t .Errorf ("authenticate client request URL = %q; want %q" , r .URL , "/token" )
157
+ }
158
+ if got , want := r .Header .Get ("Content-Type" ), "application/x-www-form-urlencoded" ; got != want {
159
+ t .Errorf ("Content-Type header = %q; want %q" , got , want )
160
+ }
161
+ err := r .ParseForm ()
162
+ if err != nil {
163
+ t .Fatal (err )
164
+ }
165
+
166
+ if got , want := r .Form .Get ("scope" ), scopes ; got != want {
167
+ t .Errorf ("scope = %q; want %q" , got , want )
168
+ }
169
+ if got , want := r .Form .Get ("audience" ), audience ; got != want {
170
+ t .Errorf ("audience = %q; want %q" , got , want )
171
+ }
172
+ if got , want := r .Form .Get ("grant_type" ), "client_credentials" ; got != want {
173
+ t .Errorf ("grant_type = %q; want %q" , got , want )
174
+ }
175
+ expectedAssertionType := "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
176
+ if got , want := r .Form .Get ("client_assertion_type" ), expectedAssertionType ; got != want {
177
+ t .Errorf ("client_assertion_type = %q; want %q" , got , want )
178
+ }
179
+
180
+ assertion = r .Form .Get ("client_assertion" )
181
+
182
+ w .Header ().Set ("Content-Type" , "application/json" )
183
+ w .Write ([]byte (`{
184
+ "access_token": "90d64460d14870c08c81352a05dedd3465940a7c",
185
+ "token_type": "bearer",
186
+ "expires_in": 3600
187
+ }` ))
188
+ }))
189
+ defer ts .Close ()
190
+
191
+ for _ , conf := range []* Config {
192
+ {
193
+ ClientID : "CLIENT_ID" ,
194
+ Scopes : strings .Split (scopes , " " ),
195
+ TokenURL : ts .URL + "/token" ,
196
+ EndpointParams : url.Values {"audience" : {audience }},
197
+ AuthStyle : oauth2 .AuthStylePrivateKeyJWT ,
198
+ PrivateKey : dummyPrivateKey ,
199
+ KeyID : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
200
+ },
201
+ {
202
+ ClientID : "CLIENT_ID_set_jwt_expiration_time" ,
203
+ Scopes : strings .Split (scopes , " " ),
204
+ TokenURL : ts .URL + "/token" ,
205
+ EndpointParams : url.Values {"audience" : {audience }},
206
+ AuthStyle : oauth2 .AuthStylePrivateKeyJWT ,
207
+ PrivateKey : dummyPrivateKey ,
208
+ KeyID : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
209
+ JWTExpires : time .Minute ,
210
+ },
211
+ } {
212
+ t .Run (conf .ClientID , func (t * testing.T ) {
213
+ _ , err := conf .TokenSource (context .Background ()).Token ()
214
+ if err != nil {
215
+ t .Fatalf ("Failed to fetch token: %v" , err )
216
+ }
217
+ parts := strings .Split (assertion , "." )
218
+ if len (parts ) != 3 {
219
+ t .Fatalf ("assertion = %q; want 3 parts" , assertion )
220
+ }
221
+ gotJson , err := base64 .RawURLEncoding .DecodeString (parts [1 ])
222
+ if err != nil {
223
+ t .Fatalf ("invalid token payload; err = %v" , err )
224
+ }
225
+ claimSet := jws.ClaimSet {}
226
+ if err := json .Unmarshal (gotJson , & claimSet ); err != nil {
227
+ t .Errorf ("failed to unmarshal json token payload = %q; err = %v" , gotJson , err )
228
+ }
229
+ if got , want := claimSet .Iss , conf .ClientID ; got != want {
230
+ t .Errorf ("payload iss = %q; want %q" , got , want )
231
+ }
232
+ if claimSet .Jti == "" {
233
+ t .Errorf ("payload jti is empty" )
234
+ }
235
+ expectedDuration := time .Hour
236
+ if conf .JWTExpires > 0 {
237
+ expectedDuration = conf .JWTExpires
238
+ }
239
+ if got , want := claimSet .Exp , time .Now ().Add (expectedDuration ).Unix (); got != want {
240
+ t .Errorf ("payload exp = %q; want %q" , got , want )
241
+ }
242
+ if got , want := claimSet .Aud , conf .TokenURL ; got != want {
243
+ t .Errorf ("payload aud = %q; want %q" , got , want )
244
+ }
245
+ if got , want := claimSet .Sub , conf .ClientID ; got != want {
246
+ t .Errorf ("payload sub = %q; want %q" , got , want )
247
+ }
248
+ })
249
+ }
250
+ }
251
+
116
252
func TestTokenRefreshRequest (t * testing.T ) {
117
253
internal .ResetAuthCache ()
118
254
ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
0 commit comments