Repository navigation
Expand file tree
/
Copy pathmetadata_test.go
More file actions
562 lines (550 loc) · 18.3 KB
/
Copy pathmetadata_test.go
File metadata and controls
562 lines (550 loc) · 18.3 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package oidfed
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/luci/go-render/render"
)
var metadataMarshalData = map[string]marshalData{
"nil": {
Data: []byte(`null`),
Object: nil,
},
"empty": {
Data: []byte(`{}`),
Object: Metadata{},
},
"only federation": {
Data: []byte(`{"federation_entity":{"contacts":["contact@federation.endpoint"],"federation_fetch_endpoint":"https://federation.endpoint/fetch","federation_list_endpoint":"https://federation.endpoint/list","federation_resolve_endpoint":"https://federation.endpoint/resolve","federation_trust_mark_status_endpoint":"https://federation.endpoint/trustmarks","organization_name":"Test Organization"}}`),
Object: Metadata{
FederationEntity: &FederationEntityMetadata{
FederationFetchEndpoint: "https://federation.endpoint/fetch",
FederationListEndpoint: "https://federation.endpoint/list",
FederationResolveEndpoint: "https://federation.endpoint/resolve",
FederationTrustMarkStatusEndpoint: "https://federation.endpoint/trustmarks",
OrganizationName: "Test Organization",
Contacts: []string{"contact@federation.endpoint"},
wasSet: map[string]bool{
"FederationFetchEndpoint": true,
"FederationListEndpoint": true,
"FederationResolveEndpoint": true,
"FederationTrustMarkStatusEndpoint": true,
"OrganizationName": true,
"Contacts": true,
},
},
},
},
"federation + op": {
Data: []byte(`{"federation_entity":{"contacts":["contact@op.example.com"],"organization_name":"Test OP Org"},"openid_provider":{"authorization_endpoint":"https://op.example.com/authorization","client_registration_types_supported":["automatic"],"code_challenge_methods_supported":["S256"],"grant_types_supported":["authorization_code","refresh_token"],"id_token_signing_alg_values_supported":["ES256","ES512"],"introspection_endpoint":"https://op.example.com/introspect","issuer":"https://op.example.com","jwks_uri":"https://op.example.com/jwks","organization_name":"Test OP Org","request_object_signing_alg_values_supported":["ES256","ES512"],"response_types_supported":["code"],"revocation_endpoint":"https://op.example.com/revoke","scopes_supported":["openid","profile","email","offline_access"],"token_endpoint":"https://op.example.com/token","userinfo_endpoint":"https://op.example.com/userinfo","userinfo_signed_response_alg_values_supported":["ES256","ES512"]}}`),
Object: Metadata{
OpenIDProvider: &OpenIDProviderMetadata{
Issuer: "https://op.example.com",
AuthorizationEndpoint: "https://op.example.com/authorization",
TokenEndpoint: "https://op.example.com/token",
UserinfoEndpoint: "https://op.example.com/userinfo",
ScopesSupported: []string{
"openid",
"profile",
"email",
"offline_access",
},
ResponseTypesSupported: []string{"code"},
GrantTypesSupported: []string{
"authorization_code",
"refresh_token",
},
IDTokenSigningAlgValuesSupported: []string{
"ES256",
"ES512",
},
UserinfoSignedResponseAlgValuesSupported: []string{
"ES256",
"ES512",
},
RequestObjectSigningAlgValuesSupported: []string{
"ES256",
"ES512",
},
RevocationEndpoint: "https://op.example.com/revoke",
IntrospectionEndpoint: "https://op.example.com/introspect",
CodeChallengeMethodsSupported: []string{"S256"},
ClientRegistrationTypesSupported: []string{"automatic"},
OrganizationName: "Test OP Org",
JWKSURI: "https://op.example.com/jwks",
wasSet: map[string]bool{
"Issuer": true,
"AuthorizationEndpoint": true,
"TokenEndpoint": true,
"UserinfoEndpoint": true,
"ScopesSupported": true,
"ResponseTypesSupported": true,
"GrantTypesSupported": true,
"IDTokenSigningAlgValuesSupported": true,
"UserinfoSignedResponseAlgValuesSupported": true,
"RequestObjectSigningAlgValuesSupported": true,
"RevocationEndpoint": true,
"IntrospectionEndpoint": true,
"CodeChallengeMethodsSupported": true,
"ClientRegistrationTypesSupported": true,
"OrganizationName": true,
"JWKSURI": true,
},
},
FederationEntity: &FederationEntityMetadata{
OrganizationName: "Test OP Org",
Contacts: []string{"contact@op.example.com"},
wasSet: map[string]bool{
"OrganizationName": true,
"Contacts": true,
},
},
},
},
"federation + rp": {
Data: []byte(`{"federation_entity":{"contacts":["contact@rp.example.org"],"organization_name":"test rp org"},"openid_relying_party":{"application_type":"web","client_name":"test rp","client_registration_types":["automatic"],"client_uri":"https://rp.example.org","contacts":["contact@rp.example.org"],"grant_types":["authorization_code"],"jwks_uri":"https://rp.example.org/jwks","organization_name":"test rp org","policy_uri":"https://rp.example.org/policy","redirect_uris":["https://rp.example.org/redirect"],"response_types":["code"],"scope":"openid profile email","tos_uri":"https://rp.example.org/tos"}}`),
Object: Metadata{
RelyingParty: &OpenIDRelyingPartyMetadata{
Scope: "openid profile email",
RedirectURIS: []string{"https://rp.example.org/redirect"},
ResponseTypes: []string{"code"},
GrantTypes: []string{"authorization_code"},
ApplicationType: "web",
Contacts: []string{"contact@rp.example.org"},
ClientName: "test rp",
ClientURI: "https://rp.example.org",
PolicyURI: "https://rp.example.org/policy",
TOSURI: "https://rp.example.org/tos",
JWKSURI: "https://rp.example.org/jwks",
OrganizationName: "test rp org",
ClientRegistrationTypes: []string{"automatic"},
wasSet: map[string]bool{
"Scope": true,
"RedirectURIS": true,
"ResponseTypes": true,
"GrantTypes": true,
"ApplicationType": true,
"Contacts": true,
"ClientName": true,
"ClientURI": true,
"PolicyURI": true,
"TOSURI": true,
"JWKSURI": true,
"OrganizationName": true,
"ClientRegistrationTypes": true,
},
},
FederationEntity: &FederationEntityMetadata{
OrganizationName: "test rp org",
Contacts: []string{"contact@rp.example.org"},
wasSet: map[string]bool{
"OrganizationName": true,
"Contacts": true,
},
},
},
},
"op extra fields": {
Data: []byte(`{"openid_provider":{"authorization_endpoint":"https://op.example.com/auth","foo":"bar","issuer":"https://op.example.com","slice":["two","values"],"token_endpoint":"https://op.example.com/token"}}`),
Object: Metadata{
OpenIDProvider: &OpenIDProviderMetadata{
Issuer: "https://op.example.com",
AuthorizationEndpoint: "https://op.example.com/auth",
TokenEndpoint: "https://op.example.com/token",
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Issuer": true,
"AuthorizationEndpoint": true,
"TokenEndpoint": true,
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"rp extra fields": {
Data: []byte(`{"openid_relying_party":{"foo":"bar","slice":["two","values"]}}`),
Object: Metadata{
RelyingParty: &OpenIDRelyingPartyMetadata{
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"as extra fields": {
Data: []byte(`{"oauth_authorization_server":{"authorization_endpoint":"https://as.example.com/auth","foo":"bar","issuer":"https://as.example.com","slice":["two","values"],"token_endpoint":"https://as.example.com/token"}}`),
Object: Metadata{
OAuthAuthorizationServer: &OAuthAuthorizationServerMetadata{
Issuer: "https://as.example.com",
AuthorizationEndpoint: "https://as.example.com/auth",
TokenEndpoint: "https://as.example.com/token",
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Issuer": true,
"AuthorizationEndpoint": true,
"TokenEndpoint": true,
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"client extra fields": {
Data: []byte(`{"oauth_client":{"foo":"bar","slice":["two","values"]}}`),
Object: Metadata{
OAuthClient: &OAuthClientMetadata{
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"pr extra fields": {
Data: []byte(`{"oauth_resource":{"foo":"bar","slice":["two","values"]}}`),
Object: Metadata{
OAuthProtectedResource: &OAuthProtectedResourceMetadata{
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"federation extra fields": {
Data: []byte(`{"federation_entity":{"foo":"bar","slice":["two","values"]}}`),
Object: Metadata{
FederationEntity: &FederationEntityMetadata{
Extra: map[string]any{
"foo": "bar",
"slice": []any{
"two",
"values",
},
},
wasSet: map[string]bool{
"Extra": true,
"foo": true,
"slice": true,
},
},
},
},
"extra metadata": {
Data: []byte(`{"another-entity":{"a-key":"a-value"},"federation_entity":{"federation_fetch_endpoint":"https://federation.endpoint/fetch"}}`),
Object: Metadata{
FederationEntity: &FederationEntityMetadata{
FederationFetchEndpoint: "https://federation.endpoint/fetch",
wasSet: map[string]bool{
"FederationFetchEndpoint": true,
},
},
Extra: map[string]any{
"another-entity": map[string]any{
"a-key": "a-value",
},
},
},
},
}
func TestMetadata_MarshalJSON(t *testing.T) {
for name, test := range metadataMarshalData {
t.Run(
name, func(t *testing.T) {
j, err := json.Marshal(test.Object)
if err != nil {
t.Error(err)
}
if !bytes.Equal(j, test.Data) {
t.Errorf("Marshal result not as expected.\nExpected: %s\n Got: %s", test.Data, j)
}
},
)
}
}
func TestMetadata_UnmarshalJSON(t *testing.T) {
for name, test := range metadataMarshalData {
t.Run(
name, func(t *testing.T) {
var result Metadata
err := json.Unmarshal(test.Data, &result)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(test.Object, result) {
if name == "nil" {
// the nil test is only for marshalling not unmarshalling into nil
return
}
t.Errorf(
"Unmarshal result not as expected.\nExpected: %s\n Got: %s", render.Render(test.Object),
render.Render(result),
)
}
},
)
}
}
func TestMetadata_FindEntityMetadata(t *testing.T) {
type AnotherEntityMetadata struct {
AKey string `json:"a-key"`
}
var metadata Metadata
if err := json.Unmarshal(metadataMarshalData["extra metadata"].Data, &metadata); err != nil {
t.Fatal(err)
}
testCases := map[string]struct {
metadataType string
deserializeInto string
shouldSucceed bool
expectedResult any
}{
"Metadata is present and in an explicit struct field": {
metadataType: "federation_entity",
deserializeInto: "federation_entity_metadata",
shouldSucceed: true,
expectedResult: FederationEntityMetadata{
FederationFetchEndpoint: "https://federation.endpoint/fetch",
wasSet: map[string]bool{
"FederationFetchEndpoint": true,
},
},
},
"Metadata handle nil pointer parameter": {
metadataType: "federation_entity",
deserializeInto: "",
shouldSucceed: false,
expectedResult: nil,
},
"Metadata present in Extra but incorrectly typed": {
metadataType: "another-entity",
deserializeInto: "struct name",
shouldSucceed: true,
expectedResult: struct{ Name string }{},
},
"Struct field present but empty": {
metadataType: "openid_provider",
deserializeInto: "openid_provider_metadata",
shouldSucceed: false,
expectedResult: OpenIDProviderMetadata{},
},
"Extra metadata with invalid entity type": {
metadataType: "invalid-extra",
deserializeInto: "struct",
shouldSucceed: false,
expectedResult: struct{}{},
},
"Metadata is present and in extra metadata": {
metadataType: "another-entity",
deserializeInto: "another_entity_metadata",
shouldSucceed: true,
expectedResult: AnotherEntityMetadata{
AKey: "a-value",
},
},
"Metadata is absent and would be in an explicit struct field": {
metadataType: "openid_provider",
deserializeInto: "openid_provider_metadata",
shouldSucceed: false,
expectedResult: OpenIDProviderMetadata{},
},
"Metadata is absent and would be in extra metadata": {
metadataType: "no-such-metadata",
deserializeInto: "struct",
shouldSucceed: false,
expectedResult: struct{}{},
},
}
for name, testCase := range testCases {
t.Run(
name, func(t *testing.T) {
var err error
var result any
switch testCase.deserializeInto {
case "federation_entity_metadata":
var deserialize FederationEntityMetadata
err = metadata.FindEntityMetadata(testCase.metadataType, &deserialize)
result = deserialize
case "openid_provider_metadata":
var deserialize OpenIDProviderMetadata
err = metadata.FindEntityMetadata(testCase.metadataType, &deserialize)
result = deserialize
case "another_entity_metadata":
var deserialize AnotherEntityMetadata
err = metadata.FindEntityMetadata(testCase.metadataType, &deserialize)
result = deserialize
case "struct":
var deserialize struct{}
err = metadata.FindEntityMetadata(testCase.metadataType, &deserialize)
result = deserialize
case "struct name":
var deserialize struct{ Name string }
err = metadata.FindEntityMetadata(testCase.metadataType, &deserialize)
result = deserialize
default:
err = metadata.FindEntityMetadata(testCase.metadataType, nil)
}
fmt.Printf("Result: %T %+v\n", result, result)
if testCase.shouldSucceed && err != nil {
t.Error(err)
} else if !testCase.shouldSucceed && err == nil {
t.Errorf("finding %s metadata should fail", testCase.metadataType)
if result != nil {
// t.Logf("%+v", reflect.ValueOf(testValue).Elem().Interface())
t.Logf("%+v", result)
}
}
if testCase.expectedResult != nil && result != nil {
if !reflect.DeepEqual(result, testCase.expectedResult) {
t.Errorf(
"Result not as expected.\nExpected: %T: %+v\n Got: %T: %+v",
testCase.expectedResult,
testCase.expectedResult,
result,
result,
)
}
}
},
)
}
}
func TestMetadata_ApplyInformationalClaimsToFederationEntity(t *testing.T) {
testCases := map[string]struct {
input Metadata
expectedOutput Metadata
}{
"Empty metadata": {
input: Metadata{},
expectedOutput: Metadata{},
},
"Only federation entity with no claims": {
input: Metadata{
FederationEntity: &FederationEntityMetadata{OrganizationName: "Federation Only"},
},
expectedOutput: Metadata{
FederationEntity: &FederationEntityMetadata{OrganizationName: "Federation Only"},
},
},
"RelyingParty provides non-conflicting claims": {
input: Metadata{
RelyingParty: &OpenIDRelyingPartyMetadata{
OrganizationName: "RP Org",
DisplayName: "Display Name",
Contacts: []string{"contact@rp.org"},
PolicyURI: "https://rp.org/policy",
},
},
expectedOutput: Metadata{
FederationEntity: &FederationEntityMetadata{
OrganizationName: "RP Org",
DisplayName: "Display Name",
Contacts: []string{"contact@rp.org"},
PolicyURI: "https://rp.org/policy",
},
RelyingParty: &OpenIDRelyingPartyMetadata{
OrganizationName: "RP Org",
DisplayName: "Display Name",
Contacts: []string{"contact@rp.org"},
PolicyURI: "https://rp.org/policy",
},
},
},
"Conflicting claims (ignored)": {
input: Metadata{
OpenIDProvider: &OpenIDProviderMetadata{
OrganizationName: "OP Org",
Contacts: []string{"contact@op.org"},
},
RelyingParty: &OpenIDRelyingPartyMetadata{
OrganizationName: "RP Org",
Contacts: []string{"contact@rp.org"},
},
},
expectedOutput: Metadata{
OpenIDProvider: &OpenIDProviderMetadata{
OrganizationName: "OP Org",
Contacts: []string{"contact@op.org"},
},
RelyingParty: &OpenIDRelyingPartyMetadata{
OrganizationName: "RP Org",
Contacts: []string{"contact@rp.org"},
},
},
},
"Federation entity already defined, no conflicting claims": {
input: Metadata{
FederationEntity: &FederationEntityMetadata{
OrganizationName: "Existing Federation",
},
OpenIDProvider: &OpenIDProviderMetadata{
OrganizationName: "OP Org",
},
},
expectedOutput: Metadata{
FederationEntity: &FederationEntityMetadata{
OrganizationName: "Existing Federation",
},
OpenIDProvider: &OpenIDProviderMetadata{
OrganizationName: "OP Org",
},
},
},
}
for name, testCase := range testCases {
t.Run(
name, func(t *testing.T) {
testCase.input.ApplyInformationalClaimsToFederationEntity()
if !reflect.DeepEqual(testCase.input, testCase.expectedOutput) {
t.Errorf(
"Output not as expected.\nExpected: %+v\n Got: %+v",
render.Render(testCase.expectedOutput),
render.Render(testCase.input),
)
}
},
)
}
}