-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoidc_test.go
More file actions
527 lines (418 loc) · 12.9 KB
/
oidc_test.go
File metadata and controls
527 lines (418 loc) · 12.9 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"errors"
"maps"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
"github.com/goccy/go-json"
fiber "github.com/gofiber/fiber/v3"
"github.com/hyp3rd/hypercache/pkg/httpauth"
)
// Test-fixture constants. Hoisted to satisfy goconst — these
// strings repeat across every table-driven test in this file with
// no semantic significance per occurrence.
const (
testAudience = "hypercache-test"
claimSub = "sub"
claimAud = "aud"
claimScope = "scope"
claimEmail = "email"
scopeRead = "read"
scopeWrite = "write"
scopeAdmin = "admin"
identityOps = "ops"
identityOperator = "operator@example.com"
)
// idpStub is an in-process OIDC IdP just rich enough for go-oidc's
// verifier to validate JWTs. Two endpoints:
//
// - /.well-known/openid-configuration: the discovery doc
// - /jwks: the public-key set
//
// No /authorize, no /token — those are only needed for the OAuth
// dance, not for IdToken verification (which validates a JWT
// already in hand).
type idpStub struct {
server *httptest.Server
signer jose.Signer
issuer string
keyID string
}
// newIdPStub mints an RSA key pair, builds a JWKS document from
// the public key, and starts a tiny HTTP server that serves it.
// The caller derives JWTs via stub.sign() and points the OIDC
// verifier at stub.issuer().
func newIdPStub(t *testing.T) *idpStub {
t.Helper()
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generate key: %v", err)
}
const keyID = "test-key-1"
signer, err := jose.NewSigner(
jose.SigningKey{Algorithm: jose.RS256, Key: priv},
(&jose.SignerOptions{}).WithType("JWT").WithHeader(jose.HeaderKey("kid"), keyID),
)
if err != nil {
t.Fatalf("new signer: %v", err)
}
jwks := jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
{
Key: &priv.PublicKey,
KeyID: keyID,
Algorithm: "RS256",
Use: "sig",
},
},
}
stub := &idpStub{signer: signer, keyID: keyID}
mux := http.NewServeMux()
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"issuer": stub.issuer,
"jwks_uri": stub.issuer + "/jwks",
"authorization_endpoint": stub.issuer + "/authorize",
"token_endpoint": stub.issuer + "/token",
"id_token_signing_alg_values_supported": []string{"RS256"},
"response_types_supported": []string{"code"},
"subject_types_supported": []string{"public"},
})
})
mux.HandleFunc("/jwks", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(jwks)
})
stub.server = httptest.NewServer(mux)
stub.issuer = stub.server.URL
t.Cleanup(stub.server.Close)
return stub
}
// sign builds a signed JWT with the given claim map. Always
// stamps `iss` (matches the stub's URL) and the standard timing
// claims unless caller overrides them.
func (s *idpStub) sign(t *testing.T, claims map[string]any) string {
t.Helper()
merged := map[string]any{
"iss": s.issuer,
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour).Unix(),
}
maps.Copy(merged, claims)
token, err := jwt.Signed(s.signer).Claims(merged).Serialize()
if err != nil {
t.Fatalf("sign jwt: %v", err)
}
return token
}
// newVerifier builds a real `*oidc.IDTokenVerifier` against the
// stub with `testAudience` as the expected client ID. Hoisting
// the audience to a package constant keeps unparam happy (no
// always-same parameter); tests that need a different audience
// build the verifier inline.
func (s *idpStub) newVerifier(t *testing.T) *oidc.IDTokenVerifier {
t.Helper()
provider, err := oidc.NewProvider(t.Context(), s.issuer)
if err != nil {
t.Fatalf("oidc discovery: %v", err)
}
return provider.Verifier(&oidc.Config{ClientID: testAudience})
}
// driveVerify constructs a fiber app that runs `verify` on
// `/probe` and returns the resulting status + Identity. Lets the
// test body assert just the post-verify state without rebuilding
// a fiber stack inline.
func driveVerify(
t *testing.T,
verify func(fiber.Ctx) (httpauth.Identity, error),
authHeader string,
) (httpauth.Identity, error) {
t.Helper()
app := fiber.New()
var (
got httpauth.Identity
probeErr error
)
app.Get("/probe", func(c fiber.Ctx) error {
id, err := verify(c)
got, probeErr = id, err
return c.SendStatus(http.StatusOK)
})
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/probe", http.NoBody)
if authHeader != "" {
req.Header.Set("Authorization", authHeader)
}
resp, err := app.Test(req)
if err != nil {
t.Fatalf("app.Test: %v", err)
}
_ = resp.Body.Close()
return got, probeErr
}
func TestOIDC_VerifyValidToken(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
claimAud: audience,
claimSub: identityOperator,
claimScope: scopeRead + " " + scopeWrite,
})
id, err := driveVerify(t, verifier, "Bearer "+token)
if err != nil {
t.Fatalf("verify failed: %v", err)
}
if id.ID != "operator@example.com" {
t.Errorf("identity ID: got %q, want operator@example.com", id.ID)
}
if len(id.Scopes) != 2 || id.Scopes[0] != httpauth.ScopeRead || id.Scopes[1] != httpauth.ScopeWrite {
t.Errorf("scopes: got %v, want [read write]", id.Scopes)
}
}
func TestOIDC_RejectsExpiredToken(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
claimAud: audience,
claimSub: identityOps,
claimScope: scopeRead,
"exp": time.Now().Add(-time.Hour).Unix(), // expired
})
_, err := driveVerify(t, verifier, "Bearer "+token)
if err == nil {
t.Fatal("expected error for expired token, got nil")
}
}
func TestOIDC_RejectsWrongAudience(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
"aud": "different-service", // wrong audience
claimSub: identityOps,
claimScope: scopeRead,
})
_, err := driveVerify(t, verifier, "Bearer "+token)
if err == nil {
t.Fatal("expected error for wrong audience, got nil")
}
}
func TestOIDC_RejectsBadSignature(t *testing.T) {
t.Parallel()
// Two independent stubs — one signs the JWT, the verifier
// fetches JWKS from the OTHER. The signature lookup fails
// because the kid doesn't match any key in the verifier's
// JWKS.
signerStub := newIdPStub(t)
verifierStub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: verifierStub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
// Sign with signerStub but issued by verifierStub so the
// `iss` check passes; only the signature is wrong.
token := signerStub.sign(t, map[string]any{
"iss": verifierStub.issuer,
claimAud: audience,
claimSub: identityOps,
claimScope: scopeRead,
})
_, err := driveVerify(t, verifier, "Bearer "+token)
if err == nil {
t.Fatal("expected error for bad signature, got nil")
}
}
func TestOIDC_NoBearerHeader(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
// No Authorization header — must return errOIDCMissingBearer
// so the resolve chain falls through to AllowAnonymous (or
// 401). Returning a generic JWT-validation error here would
// mask the empty-header case from operators reading logs.
_, err := driveVerify(t, verifier, "")
if !errors.Is(err, errOIDCMissingBearer) {
t.Fatalf("got %v, want errOIDCMissingBearer", err)
}
}
func TestOIDC_IdentityClaim_Email(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimEmail, // not the default "sub"
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
claimAud: audience,
claimSub: "ignored-because-email-is-the-claim",
claimEmail: identityOperator,
claimScope: scopeRead,
})
id, err := driveVerify(t, verifier, "Bearer "+token)
if err != nil {
t.Fatalf("verify: %v", err)
}
if id.ID != identityOperator {
t.Errorf("identity ID: got %q, want %s", id.ID, identityOperator)
}
}
func TestOIDC_IdentityClaim_Missing(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: "preferred_username",
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
claimAud: audience,
claimSub: identityOps,
claimScope: scopeRead,
// no preferred_username
})
_, err := driveVerify(t, verifier, "Bearer "+token)
if err == nil {
t.Fatal("expected error for missing identity claim, got nil")
}
}
func TestOIDC_ScopeClaim_ArrayShape(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: "cache_scopes",
})
token := stub.sign(t, map[string]any{
"aud": audience,
claimSub: identityOps,
"cache_scopes": []string{"read", "admin"},
})
id, err := driveVerify(t, verifier, "Bearer "+token)
if err != nil {
t.Fatalf("verify: %v", err)
}
if len(id.Scopes) != 2 || id.Scopes[0] != httpauth.ScopeRead || id.Scopes[1] != httpauth.ScopeAdmin {
t.Errorf("scopes: got %v, want [read admin]", id.Scopes)
}
}
func TestOIDC_ScopeClaim_DropsUnknownScopes(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
claimAud: audience,
claimSub: identityOps,
"scope": "read trace openid email", // only read maps to a known scope
})
id, err := driveVerify(t, verifier, "Bearer "+token)
if err != nil {
t.Fatalf("verify: %v", err)
}
if len(id.Scopes) != 1 || id.Scopes[0] != httpauth.ScopeRead {
t.Errorf("scopes: got %v, want [read]", id.Scopes)
}
}
func TestOIDC_ScopeClaim_MissingYieldsEmpty(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
const audience = "hypercache-test"
verifier := makeOIDCServerVerify(oidcVerifierOptions{
IDTokenVerifier: stub.newVerifier(t),
IdentityClaim: claimSub,
ScopeClaim: claimScope,
})
token := stub.sign(t, map[string]any{
"aud": audience,
claimSub: identityOps,
// no scope claim at all
})
id, err := driveVerify(t, verifier, "Bearer "+token)
if err != nil {
t.Fatalf("verify: %v", err)
}
if len(id.Scopes) != 0 {
t.Errorf("scopes: got %v, want empty", id.Scopes)
}
if id.ID != "ops" {
t.Errorf("identity ID: got %q, want ops", id.ID)
}
}
// TestBuildOIDCVerifier_DiscoveryRPC pins the boot-time discovery
// behavior: a successful discovery returns a usable verifier; a
// bad issuer URL fails fast (rather than silently producing a
// verifier that 401s every JWT).
func TestBuildOIDCVerifier_DiscoveryRPC(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
stub := newIdPStub(t)
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
verify, err := buildOIDCVerifier(ctx, envConfig{
OIDCIssuer: stub.issuer,
OIDCAudience: "hypercache-test",
OIDCIdentityClaim: claimSub,
OIDCScopeClaim: "scope",
})
if err != nil {
t.Fatalf("buildOIDCVerifier: %v", err)
}
if verify == nil {
t.Fatal("verifier nil on success")
}
})
t.Run("unreachable issuer fails fast", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
_, err := buildOIDCVerifier(ctx, envConfig{
OIDCIssuer: "http://127.0.0.1:1", // no listener
OIDCAudience: "anything",
OIDCIdentityClaim: claimSub,
OIDCScopeClaim: "scope",
})
if err == nil {
t.Fatal("expected error for unreachable issuer")
}
})
}