This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
forked from gogatekeeper/gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_test.go
474 lines (408 loc) · 15.1 KB
/
oauth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"golang.org/x/oauth2"
oidc3 "github.com/coreos/go-oidc/v3/oidc"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jochasinga/relay"
"github.com/stretchr/testify/assert"
jose2 "gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
type fakeAuthServer struct {
location *url.URL
proxyLocation string
key jose2.JSONWebKey
server *httptest.Server
expiration time.Duration
}
const fakePrivateKey = `
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC0E2cxe1nDLCE7
U4k3Zvd4nMiAHqKZBxCPuADbzR15IcOlcLTqBBPNCpwgXSZwobgeEl1aDi9fAsEK
XZNPU2GbIPmw5nBHfE/RZ5JmU2GXdEm2R2Irnwpi8kk8hHWPK4ETN8+6yk4qKF1P
xkoHIilm37T8zWoqzYtIuj3/Obqi3Io67pvKYCsA1qMR3RRlI9IegUAc3WHMrKIL
YQShEo66pg8cTb7Q/LdgaSxR3KLi9eox0vTE72AmVQoBZlZ/ej7sJpwPKKmkszXU
AMPGf1s3Hx/lgDtM3MxtHk0pxLBHgP+P5i77dF9edW9hc/fMirdOmCpYZboox3Lr
IGoVcJodAgMBAAECggEAahX4OEV0BzArT7kR4GqvpgWv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FzbuHDAfBgNVHSMEGDAWgBQ5Fw8voBMO1GoQ
l1Qqm5UdFzbuHDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQDU
msNwnbl8uI4VcFsiyrNS7Np2xLmcs6LgCfP9WzsA4h6Ag2K15d9eh+CgaL59oza8
q0pxRcasLFtuk0egRc+HwNR5ynwt4W2al4zB1dRpTWgrnNaoOBdhsb3ifNEjFcYD
di+dPoKLST6xqKGh0zl+W4FLevUDg7KzJVcttaQ8tFh5KafcmSHZ7PfNbFsfbx/R
wthh/acHnCkOndcTBEoHdIv283bONr1Zpe9Sok2mM3uVsCvv6fRYnG+mRqcZ3C9d
hHbOowWOqA2rxWxSHrkBTQju/uYQKG5GMnXWgZokUgwRDMaNMpdp03GG4Bgeg/06
8cad+/Bp0tBaKmnCtxOC
-----END CERTIFICATE-----
`
type fakeOidcDiscoveryResponse struct {
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
UserInfoURL string `json:"userinfo_endpoint"`
Algorithms []string `json:"id_token_signing_alg_values_supported"`
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
type fakeAuthConfig struct {
EnableTLS bool
EnableProxy bool
Expiration time.Duration
}
// newFakeAuthServer simulates a oauth service
func newFakeAuthServer(config *fakeAuthConfig) *fakeAuthServer {
certBlock, _ := pem.Decode([]byte(fakeCert))
var cert *x509.Certificate
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
panic("failed to parse certificate from block, error: " + err.Error())
}
x5tSHA1 := sha1.Sum(cert.Raw)
x5tSHA256 := sha256.Sum256(cert.Raw)
service := &fakeAuthServer{
key: jose2.JSONWebKey{
Key: cert.PublicKey,
KeyID: "test-kid",
Algorithm: "RS256",
Certificates: []*x509.Certificate{cert},
CertificateThumbprintSHA1: x5tSHA1[:],
CertificateThumbprintSHA256: x5tSHA256[:],
},
}
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Get("/auth/realms/hod-test/.well-known/openid-configuration", service.discoveryHandler)
r.Get("/auth/realms/hod-test/protocol/openid-connect/certs", service.keysHandler)
r.Get("/auth/realms/hod-test/protocol/openid-connect/token", service.tokenHandler)
r.Get("/auth/realms/hod-test/protocol/openid-connect/auth", service.authHandler)
r.Get("/auth/realms/hod-test/protocol/openid-connect/userinfo", service.userInfoHandler)
r.Post("/auth/realms/hod-test/protocol/openid-connect/logout", service.logoutHandler)
r.Post("/auth/realms/hod-test/protocol/openid-connect/revoke", service.revocationHandler)
r.Post("/auth/realms/hod-test/protocol/openid-connect/token", service.tokenHandler)
if config.EnableTLS {
service.server = httptest.NewTLSServer(r)
} else {
service.server = httptest.NewServer(r)
}
if config.EnableProxy {
delay := time.Duration(0) * time.Second
proxy := relay.NewProxy(delay, service.server)
service.proxyLocation = proxy.URL
}
location, err := url.Parse(service.server.URL)
if err != nil {
panic("unable to create fake oauth service, error: " + err.Error())
}
service.location = location
service.expiration = time.Duration(1) * time.Hour
if config.Expiration.Seconds() > 0 {
service.expiration = config.Expiration
}
return service
}
func (r *fakeAuthServer) Close() {
r.server.Close()
}
func (r *fakeAuthServer) getProxyURL() string {
return r.proxyLocation
}
func (r *fakeAuthServer) getLocation() string {
return fmt.Sprintf("%s://%s/auth/realms/hod-test", r.location.Scheme, r.location.Host)
}
func (r *fakeAuthServer) getRevocationURL() string {
return fmt.Sprintf("%s://%s/auth/realms/hod-test/protocol/openid-connect/revoke", r.location.Scheme, r.location.Host)
}
func (r *fakeAuthServer) setTokenExpiration(tm time.Duration) *fakeAuthServer {
r.expiration = tm
return r
}
func (r *fakeAuthServer) discoveryHandler(w http.ResponseWriter, req *http.Request) {
renderJSON(http.StatusOK, w, req, fakeOidcDiscoveryResponse{
Issuer: fmt.Sprintf("%s://%s/auth/realms/hod-test", r.location.Scheme, r.location.Host),
AuthURL: fmt.Sprintf("%s://%s/auth/realms/hod-test/protocol/openid-connect/auth", r.location.Scheme, r.location.Host),
TokenURL: fmt.Sprintf("%s://%s/auth/realms/hod-test/protocol/openid-connect/token", r.location.Scheme, r.location.Host),
JWKSURL: fmt.Sprintf("%s://%s/auth/realms/hod-test/protocol/openid-connect/certs", r.location.Scheme, r.location.Host),
UserInfoURL: fmt.Sprintf("%s://%s/auth/realms/hod-test/protocol/openid-connect/userinfo", r.location.Scheme, r.location.Host),
Algorithms: []string{"RS256"},
})
}
func (r *fakeAuthServer) keysHandler(w http.ResponseWriter, req *http.Request) {
renderJSON(http.StatusOK, w, req, jose2.JSONWebKeySet{Keys: []jose2.JSONWebKey{r.key}})
}
func (r *fakeAuthServer) authHandler(w http.ResponseWriter, req *http.Request) {
state := req.URL.Query().Get("state")
redirect := req.URL.Query().Get("redirect_uri")
if redirect == "" {
w.WriteHeader(http.StatusInternalServerError)
return
}
if state == "" {
state = "/"
}
randString, err := getRandomString(32)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
redirectionURL := fmt.Sprintf("%s?state=%s&code=%s", redirect, state, randString)
http.Redirect(w, req, redirectionURL, http.StatusSeeOther)
}
func (r *fakeAuthServer) logoutHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
}
func (r *fakeAuthServer) revocationHandler(w http.ResponseWriter, req *http.Request) {
// according RFC revocation endpoint can be access/refresh token, keycloak
// implementation https://github.com/keycloak/keycloak/pull/6704, accepts
// refresh/offline tokens
if token := req.FormValue("token"); token == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func (r *fakeAuthServer) userInfoHandler(w http.ResponseWriter, req *http.Request) {
items := strings.Split(req.Header.Get("Authorization"), " ")
if len(items) != 2 {
w.WriteHeader(http.StatusUnauthorized)
return
}
token, err := jwt.ParseSigned(items[1])
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
user, err := extractIdentity(token)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
renderJSON(http.StatusOK, w, req, map[string]interface{}{
"sub": user.claims["sub"],
"name": user.claims["name"],
"given_name": user.claims["given_name"],
"family_name": user.claims["familty_name"],
"preferred_username": user.claims["preferred_username"],
"email": user.claims["email"],
"picture": user.claims["picture"],
})
}
func (r *fakeAuthServer) tokenHandler(w http.ResponseWriter, req *http.Request) {
expires := time.Now().Add(r.expiration)
token := newTestToken(r.getLocation())
token.setExpiration(expires)
// sign the token with the private key
jwt, err := token.getToken()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
switch req.FormValue("grant_type") {
case GrantTypeUserCreds:
username := req.FormValue("username")
password := req.FormValue("password")
if username == "" || password == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
if username == validUsername && password == validPassword {
renderJSON(http.StatusOK, w, req, tokenResponse{
IDToken: jwt,
AccessToken: jwt,
RefreshToken: jwt,
ExpiresIn: float64(expires.UTC().Second()),
})
return
}
renderJSON(http.StatusUnauthorized, w, req, map[string]string{
"error": "invalid_grant",
"error_description": "invalid user credentials",
})
case GrantTypeClientCreds:
clientID := req.FormValue("client_id")
clientSecret := req.FormValue("client_secret")
if clientID == "" || clientSecret == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
if clientID == validUsername && clientSecret == validPassword {
renderJSON(http.StatusOK, w, req, tokenResponse{
IDToken: jwt,
AccessToken: jwt,
RefreshToken: jwt,
ExpiresIn: float64(expires.UTC().Second()),
})
return
}
renderJSON(http.StatusUnauthorized, w, req, map[string]string{
"error": "invalid_grant",
"error_description": "invalid user credentials",
})
case GrantTypeRefreshToken:
fallthrough
case GrantTypeAuthCode:
renderJSON(http.StatusOK, w, req, tokenResponse{
IDToken: jwt,
AccessToken: jwt,
RefreshToken: jwt,
ExpiresIn: float64(expires.Second()),
})
default:
w.WriteHeader(http.StatusBadRequest)
}
}
func TestGetUserinfo(t *testing.T) {
px, idp, _ := newTestProxyService(nil)
token, err := newTestToken(idp.getLocation()).getToken()
assert.NoError(t, err)
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
ctx, cancel := context.WithTimeout(context.Background(), px.config.OpenIDProviderTimeout)
defer cancel()
userInfo, err := px.provider.UserInfo(ctx, tokenSource)
assert.NoError(t, err)
claims := DefaultTestTokenClaims{}
err = userInfo.Claims(&claims)
assert.NoError(t, err)
assert.NotEqual(t, (DefaultTestTokenClaims{}), claims)
}
func TestTokenExpired(t *testing.T) {
px, idp, _ := newTestProxyService(nil)
token := newTestToken(idp.getLocation())
cs := []struct {
Expire time.Duration
OK bool
}{
{
Expire: 1 * time.Hour,
OK: true,
},
{
Expire: -5 * time.Hour,
},
}
for i, x := range cs {
token.setExpiration(time.Now().Add(x.Expire))
jwt, err := token.getToken()
if err != nil {
t.Errorf("case %d unable to sign the token, error: %s", i, err)
continue
}
verifier := px.provider.Verifier(
&oidc3.Config{
ClientID: px.config.ClientID,
SkipClientIDCheck: true,
},
)
_, err = verifier.Verify(context.Background(), jwt)
if x.OK && err != nil {
t.Errorf("case %d, expected: %t got error: %s", i, x.OK, err)
}
if !x.OK && err == nil {
t.Errorf("case %d, expected: %t got no error", i, x.OK)
}
}
}
func getRandomString(n int) (string, error) {
b := make([]rune, n)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
if err != nil {
return "", err
}
b[i] = letterRunes[num.Int64()]
}
return string(b), nil
}
func renderJSON(code int, w http.ResponseWriter, req *http.Request, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}