-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertion_test.go
More file actions
367 lines (307 loc) · 8.97 KB
/
assertion_test.go
File metadata and controls
367 lines (307 loc) · 8.97 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
package sctx
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"math/big"
"testing"
"time"
)
// Helper function to create test certificate and key.
func createTestCertAndKey(t *testing.T) (*x509.Certificate, ed25519.PrivateKey) {
// Generate key pair
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
// Create certificate
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: "test-client",
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, pub, priv)
if err != nil {
t.Fatalf("Failed to create certificate: %v", err)
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
t.Fatalf("Failed to parse certificate: %v", err)
}
return cert, priv
}
func TestCreateAssertion(t *testing.T) {
cert, priv := createTestCertAndKey(t)
t.Run("valid assertion", func(t *testing.T) {
assertion, err := CreateAssertion(priv, cert)
if err != nil {
t.Fatalf("Failed to create assertion: %v", err)
}
// Check claims
if assertion.Claims.Purpose != "token-generation" {
t.Errorf("Expected purpose 'token-generation', got %s", assertion.Claims.Purpose)
}
if assertion.Claims.Fingerprint != getFingerprint(cert) {
t.Errorf("Fingerprint mismatch")
}
// Check timing
if assertion.Claims.IssuedAt.After(time.Now()) {
t.Error("IssuedAt is in the future")
}
if assertion.Claims.ExpiresAt.Before(time.Now()) {
t.Error("Assertion already expired")
}
// Check nonce
if assertion.Claims.Nonce == "" {
t.Error("Nonce is empty")
}
// Decode and check nonce length
decoded, err := base64.RawURLEncoding.DecodeString(assertion.Claims.Nonce)
if err != nil {
t.Errorf("Failed to decode nonce: %v", err)
}
if len(decoded) != 32 {
t.Errorf("Expected nonce length 32, got %d", len(decoded))
}
})
t.Run("nil private key", func(t *testing.T) {
_, err := CreateAssertion(nil, cert)
if err == nil {
t.Error("Expected error for nil private key")
}
})
t.Run("nil certificate", func(t *testing.T) {
_, err := CreateAssertion(priv, nil)
if err == nil {
t.Error("Expected error for nil certificate")
}
})
t.Run("mismatched key and certificate", func(t *testing.T) {
// Create a different key
_, otherPriv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
_, err = CreateAssertion(otherPriv, cert)
if err == nil {
t.Error("Expected error for mismatched key and certificate")
}
})
}
func TestVerifyAssertion(t *testing.T) {
cert, priv := createTestCertAndKey(t)
t.Run("valid signature", func(t *testing.T) {
assertion, err := CreateAssertion(priv, cert)
if err != nil {
t.Fatalf("Failed to create assertion: %v", err)
}
err = verifyAssertion(assertion, cert)
if err != nil {
t.Errorf("Failed to verify valid assertion: %v", err)
}
})
t.Run("invalid signature", func(t *testing.T) {
assertion, err := CreateAssertion(priv, cert)
if err != nil {
t.Fatalf("Failed to create assertion: %v", err)
}
// Corrupt the signature
assertion.Signature[0] ^= 0xFF
err = verifyAssertion(assertion, cert)
if err == nil {
t.Error("Expected error for invalid signature")
}
})
t.Run("wrong certificate", func(t *testing.T) {
assertion, err := CreateAssertion(priv, cert)
if err != nil {
t.Fatalf("Failed to create assertion: %v", err)
}
// Create a different certificate
otherCert, _ := createTestCertAndKey(t)
err = verifyAssertion(assertion, otherCert)
if err == nil {
t.Error("Expected error for wrong certificate")
}
})
}
func TestAssertionValidationSteps(t *testing.T) {
cert, priv := createTestCertAndKey(t)
ctx := context.Background()
t.Run("verifySignatureStep", func(t *testing.T) {
t.Run("valid signature", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := verifySignatureStep(ctx, ac)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
t.Run("invalid signature", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
assertion.Signature[0] ^= 0xFF // Corrupt signature
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := verifySignatureStep(ctx, ac)
if err == nil {
t.Error("Expected error for invalid signature")
}
})
})
t.Run("checkExpirationStep", func(t *testing.T) {
t.Run("valid assertion", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := checkExpirationStep(ctx, ac)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
t.Run("expired assertion", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
// Set expiration to past
assertion.Claims.ExpiresAt = time.Now().Add(-1 * time.Hour)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := checkExpirationStep(ctx, ac)
if err == nil {
t.Error("Expected error for expired assertion")
}
})
t.Run("future issued assertion", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
// Set issued time to future (beyond clock skew)
assertion.Claims.IssuedAt = time.Now().Add(1 * time.Hour)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := checkExpirationStep(ctx, ac)
if err == nil {
t.Error("Expected error for future issued assertion")
}
})
t.Run("lifetime too long", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
// Set lifetime to 10 minutes
assertion.Claims.ExpiresAt = assertion.Claims.IssuedAt.Add(10 * time.Minute)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := checkExpirationStep(ctx, ac)
if err == nil {
t.Error("Expected error for assertion with long lifetime")
}
})
})
t.Run("matchFingerprintStep", func(t *testing.T) {
t.Run("matching fingerprint", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := matchFingerprintStep(ctx, ac)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
t.Run("mismatched fingerprint", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
// Change fingerprint
assertion.Claims.Fingerprint = "wrong-fingerprint"
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := matchFingerprintStep(ctx, ac)
if err == nil {
t.Error("Expected error for mismatched fingerprint")
}
})
})
t.Run("validateClaimsStep", func(t *testing.T) {
t.Run("valid claims", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := validateClaimsStep(ctx, ac)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
t.Run("invalid purpose", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
assertion.Claims.Purpose = "wrong-purpose"
ac := &AssertionContext{
Assertion: assertion,
Certificate: cert,
}
err := validateClaimsStep(ctx, ac)
if err == nil {
t.Error("Expected error for invalid purpose")
}
})
})
}
func TestValidateAssertionIntegration(t *testing.T) {
resetAdminForTesting()
cert, priv := createTestCertAndKey(t)
ctx := context.Background()
// Create admin service to test with nonce checking
_, adminKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
certPool := x509.NewCertPool()
certPool.AddCert(cert)
admin, err := NewAdminService[any](adminKey, certPool)
if err != nil {
t.Fatalf("Failed to create admin service: %v", err)
}
adminSvc := admin.(*adminService[any])
t.Run("valid assertion passes all checks", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
err := ValidateAssertion(ctx, assertion, cert, adminSvc)
if err != nil {
t.Errorf("Expected valid assertion to pass, got: %v", err)
}
})
t.Run("invalid signature fails", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
assertion.Signature[0] ^= 0xFF
err := ValidateAssertion(ctx, assertion, cert, adminSvc)
if err == nil {
t.Error("Expected invalid signature to fail")
}
})
t.Run("expired assertion fails", func(t *testing.T) {
assertion, _ := CreateAssertion(priv, cert)
assertion.Claims.ExpiresAt = time.Now().Add(-time.Hour)
err := ValidateAssertion(ctx, assertion, cert, adminSvc)
if err == nil {
t.Error("Expected expired assertion to fail")
}
})
}