forked from hashicorp/cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp_test.go
More file actions
430 lines (372 loc) · 10.5 KB
/
sp_test.go
File metadata and controls
430 lines (372 loc) · 10.5 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package saml_test
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
"github.com/hashicorp/cap/saml"
"github.com/hashicorp/cap/saml/models/core"
"github.com/hashicorp/cap/saml/models/metadata"
)
func Test_NewServiceProvider(t *testing.T) {
t.Parallel()
r := require.New(t)
exampleURL := "http://test.me"
validConfig, err := saml.NewConfig(
exampleURL,
exampleURL,
exampleURL,
)
r.NoError(err)
cases := []struct {
name string
cfg *saml.Config
err string
}{
{
name: "When a valid config is provided",
cfg: validConfig,
err: "",
},
{
name: "When an invalid config is provided",
cfg: &saml.Config{},
err: "saml.NewServiceProvider: insufficient provider config:",
},
{
name: "When no config is provided",
cfg: nil,
err: "saml.NewServiceProvider: no provider config provided",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := require.New(t)
got, err := saml.NewServiceProvider(c.cfg)
if c.err != "" {
r.Error(err)
r.ErrorContains(err, c.err)
return
}
r.NoError(err)
r.NotNil(got)
r.NotNil(got.Config())
})
}
}
func Test_ServiceProvider_FetchMetadata_ErrorCases(t *testing.T) {
t.Parallel()
r := require.New(t)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("<invalidXML//>"))
}))
defer s.Close()
fakeURL := "http://cap.saml.fake"
metaURL := fmt.Sprintf("%s/saml/metadata", s.URL)
cfg, err := saml.NewConfig(
fakeURL,
fakeURL,
fakeURL,
)
r.NoError(err)
cases := []struct {
name string
metadata string
wantErr string
}{
{
name: "When the metadata can't be fetched",
metadata: fakeURL,
wantErr: "saml.ServiceProvider.FetchIDPMetadata: failed to fetch identity provider metadata:",
},
{
name: "When the metadata XML can't be parsed",
metadata: metaURL,
wantErr: "saml.ServiceProvider.FetchIDPMetadata: failed to parse identity provider XML metadata:",
},
}
for _, c := range cases {
cfg.MetadataURL = c.metadata
provider, err := saml.NewServiceProvider(cfg)
r.NoError(err)
t.Run(c.name, func(t *testing.T) {
r := require.New(t)
got, err := provider.IDPMetadata()
r.Nil(got)
r.Error(err)
r.ErrorContains(err, c.wantErr)
})
}
}
func Test_ServiceProvider_FetchMetadata_Cache(t *testing.T) {
type testServer struct {
fail bool
failOnRefresh bool
}
newTestServer := func(t *testing.T, failOnRefresh bool) string {
t.Helper()
ts := &testServer{false, failOnRefresh}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if !ts.fail {
w.Write([]byte(exampleIDPSSODescriptorX))
}
ts.fail = ts.fail || ts.failOnRefresh
}))
t.Cleanup(s.Close)
return s.URL
}
cases := []struct {
name string
newTime string
shouldBeCached bool
opts []saml.Option
failOnRefresh bool
expectErrorOnRefresh bool
}{
{
name: "is cached",
shouldBeCached: true,
},
{
name: "cache is disabled",
opts: []saml.Option{saml.WithCache(false)},
shouldBeCached: false,
},
{
name: "stale cached document should not be used",
newTime: "2017-07-26",
shouldBeCached: false,
},
{
name: "is not cached once validUntil is reached",
newTime: "2018-07-25",
expectErrorOnRefresh: true,
},
{
name: "a stale document should not be used if refreshing fails",
newTime: "2017-07-26",
failOnRefresh: true,
expectErrorOnRefresh: true,
},
{
name: "use stale document",
opts: []saml.Option{saml.WithStale(true)},
newTime: "2017-07-26",
failOnRefresh: true,
shouldBeCached: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
r := require.New(t)
url := newTestServer(t, tt.failOnRefresh)
metaURL := fmt.Sprintf("%s/saml/metadata", url)
cfg, err := saml.NewConfig(
metaURL,
metaURL,
metaURL,
)
r.NoError(err)
provider, err := saml.NewServiceProvider(cfg)
r.NoError(err)
newTime, err := time.Parse("2006-01-02", "2017-07-25")
r.NoError(err)
opts := append([]saml.Option{saml.WithClock(clockwork.NewFakeClockAt(newTime))}, tt.opts...)
got1, err := provider.IDPMetadata(opts...)
r.NoError(err)
r.NotNil(got1)
if tt.newTime != "" {
newTime, err = time.Parse("2006-01-02", tt.newTime)
r.NoError(err)
opts = append(opts, saml.WithClock(clockwork.NewFakeClockAt(newTime)))
}
got2, err := provider.IDPMetadata(opts...)
if tt.expectErrorOnRefresh {
r.Error(err)
return
}
r.NoError(err)
r.NotNil(got2)
if tt.shouldBeCached {
r.True(got1 == got2)
} else {
r.False(got1 == got2)
}
})
}
}
func Test_ServiceProvider_CreateMetadata(t *testing.T) {
t.Parallel()
r := require.New(t)
entityID := "http://test.me/entity"
acs := "http://test.me/saml/acs"
meta := "http://test.me/sso/metadata"
now := time.Now()
validUntil := func() time.Time {
return now
}
cfg, err := saml.NewConfig(
entityID,
acs,
meta,
)
r.NoError(err)
cfg.ValidUntil = validUntil
provider, err := saml.NewServiceProvider(cfg)
r.NoError(err)
cases := []struct {
name string
nameIDFormats []core.NameIDFormat
}{
{
name: "",
},
{
name: "email",
nameIDFormats: []core.NameIDFormat{core.NameIDFormatEmail},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := require.New(t)
opts := []saml.Option{}
if c.nameIDFormats != nil {
opts = append(opts, saml.WithMetadataNameIDFormat(c.nameIDFormats...))
}
got := provider.CreateMetadata(opts...)
r.Equal(&now, got.ValidUntil)
r.Equal("http://test.me/entity", got.EntityID)
r.Len(got.SPSSODescriptor, 1)
r.True(got.SPSSODescriptor[0].WantAssertionsSigned)
r.False(got.SPSSODescriptor[0].AuthnRequestsSigned)
r.Equal(
metadata.ProtocolSupportEnumerationProtocol,
got.SPSSODescriptor[0].ProtocolSupportEnumeration,
)
r.Equal(
core.ServiceBindingHTTPPost,
got.SPSSODescriptor[0].AssertionConsumerService[0].Binding,
)
r.Equal(1, got.SPSSODescriptor[0].AssertionConsumerService[0].Index)
r.Equal(
"http://test.me/saml/acs",
got.SPSSODescriptor[0].AssertionConsumerService[0].Location,
)
r.Equal(got.SPSSODescriptor[0].NameIDFormat, c.nameIDFormats)
})
}
}
func Test_CreateMetadata_Options(t *testing.T) {
t.Parallel()
r := require.New(t)
fakeURL := "http://fake.test.url"
cfg, err := saml.NewConfig(
fakeURL,
fakeURL,
fakeURL,
)
provider, err := saml.NewServiceProvider(cfg)
r.NoError(err)
t.Run("When option InsecureWantAssertionsUnsigned is set", func(t *testing.T) {
r := require.New(t)
got := provider.CreateMetadata(
saml.InsecureWantAssertionsUnsigned(),
)
r.False(got.SPSSODescriptor[0].WantAssertionsSigned)
})
t.Run("When option WithAdditionalNameIDFormat is set", func(t *testing.T) {
r := require.New(t)
got := provider.CreateMetadata(
saml.WithMetadataNameIDFormat(core.NameIDFormatTransient),
)
r.Equal(got.SPSSODescriptor[0].NameIDFormat, []core.NameIDFormat{core.NameIDFormatTransient})
})
t.Run("When option WithNameIDFormats is set", func(t *testing.T) {
r := require.New(t)
got := provider.CreateMetadata(
saml.WithMetadataNameIDFormat(core.NameIDFormatEntity, core.NameIDFormatUnspecified),
)
r.Len(got.SPSSODescriptor[0].NameIDFormat, 2)
r.Equal(got.SPSSODescriptor[0].NameIDFormat, []core.NameIDFormat{
core.NameIDFormatEntity,
core.NameIDFormatUnspecified,
})
})
t.Run("When option WithACSServiceBinding is set", func(t *testing.T) {
r := require.New(t)
got := provider.CreateMetadata(
saml.WithACSServiceBinding(core.ServiceBindingHTTPRedirect),
)
r.Len(got.SPSSODescriptor[0].AssertionConsumerService, 1)
r.Equal(
got.SPSSODescriptor[0].AssertionConsumerService[0].Binding,
core.ServiceBindingHTTPRedirect,
)
})
t.Run("When option WithAdditionalACSEndpoint is set", func(t *testing.T) {
r := require.New(t)
redirectEndpoint, err := url.Parse("http://cap.saml.test/acs/redirect")
r.NoError(err)
got := provider.CreateMetadata(
saml.WithAdditionalACSEndpoint(
core.ServiceBindingHTTPRedirect,
*redirectEndpoint,
),
)
r.Len(got.SPSSODescriptor[0].AssertionConsumerService, 2)
r.Equal(
got.SPSSODescriptor[0].AssertionConsumerService[0],
metadata.IndexedEndpoint{
Endpoint: metadata.Endpoint{
Binding: core.ServiceBindingHTTPPost,
Location: fakeURL,
},
Index: 1,
},
)
r.Equal(
got.SPSSODescriptor[0].AssertionConsumerService[1],
metadata.IndexedEndpoint{
Endpoint: metadata.Endpoint{
Binding: core.ServiceBindingHTTPRedirect,
Location: redirectEndpoint.String(),
},
Index: 2,
},
)
})
}
var exampleIDPSSODescriptorX = `<md:EntityDescriptor entityID="https://sso.example.info/entity" validUntil="2017-08-30T19:10:29Z" cacheDuration="PT15M"
xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns:mdrpi="urn:oasis:names:tc:SAML:metadata:rpi"
xmlns:mdattr="urn:oasis:names:tc:SAML:metadata:attribute"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<!-- insert ds:Signature element (omitted) -->
<md:Extensions>
<mdrpi:RegistrationInfo registrationAuthority="https://registrar.example.net"/>
<mdrpi:PublicationInfo creationInstant="2017-08-16T19:10:29Z" publisher="https://registrar.example.net"/>
<mdattr:EntityAttributes>
<saml:Attribute Name="http://registrar.example.net/entity-category" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
<saml:AttributeValue>https://registrar.example.net/category/self-certified</saml:AttributeValue>
</saml:Attribute>
</mdattr:EntityAttributes>
</md:Extensions>
<!-- insert one or more concrete instances of the md:RoleDescriptor abstract type (see below) -->
<md:Organization>
<md:OrganizationName xml:lang="en">...</md:OrganizationName>
<md:OrganizationDisplayName xml:lang="en">...</md:OrganizationDisplayName>
<md:OrganizationURL xml:lang="en">https://www.example.info/</md:OrganizationURL>
</md:Organization>
<md:ContactPerson contactType="technical">
<md:SurName>SAML Technical Support</md:SurName>
<md:EmailAddress>mailto:technical-support@example.info</md:EmailAddress>
</md:ContactPerson>
</md:EntityDescriptor>
`