-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsp.go
391 lines (332 loc) · 9.93 KB
/
sp.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package saml
import (
_ "embed"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
"github.com/hashicorp/cap/saml/models/core"
"github.com/hashicorp/cap/saml/models/metadata"
"github.com/jonboulle/clockwork"
dsig "github.com/russellhaering/goxmldsig/types"
)
//go:embed authn_request.gohtml
var postBindingTempl string
type metadataOptions struct {
wantAssertionsSigned bool
nameIDFormats []core.NameIDFormat
acsServiceBinding core.ServiceBinding
additionalACSs []metadata.Endpoint
}
func metadataOptionsDefault() metadataOptions {
return metadataOptions{
wantAssertionsSigned: true,
acsServiceBinding: core.ServiceBindingHTTPPost,
}
}
func getMetadataOptions(opt ...Option) metadataOptions {
opts := metadataOptionsDefault()
ApplyOpts(&opts, opt...)
return opts
}
// InsecureWantAssertionsUnsigned provides a way to optionally request that you
// want insecure/unsigned assertions.
func InsecureWantAssertionsUnsigned() Option {
return func(o interface{}) {
if o, ok := o.(*metadataOptions); ok {
o.wantAssertionsSigned = false
}
}
}
// WithMetadataNameIDFormat provides an optional name ID formats, which are
// added to the existing set.
func WithMetadataNameIDFormat(format ...core.NameIDFormat) Option {
return func(o interface{}) {
if o, ok := o.(*metadataOptions); ok {
o.nameIDFormats = append(o.nameIDFormats, format...)
}
}
}
// WithACSServiceBinding provides an optional service binding.
func WithACSServiceBinding(b core.ServiceBinding) Option {
return func(o interface{}) {
if o, ok := o.(*metadataOptions); ok {
o.acsServiceBinding = b
}
}
}
// WithAdditionalACSEndpoint provides an optional additional ACS endpoint
func WithAdditionalACSEndpoint(b core.ServiceBinding, location url.URL) Option {
return func(o interface{}) {
if o, ok := o.(*metadataOptions); ok {
o.additionalACSs = append(o.additionalACSs, metadata.Endpoint{
Binding: b,
Location: location.String(),
})
}
}
}
// ServiceProvider defines a type for service providers
type ServiceProvider struct {
cfg *Config
metadata *metadata.EntityDescriptorIDPSSO
metadataCachedUntil *time.Time
metadataLock sync.Mutex
}
// NewServiceProvider creates a new ServiceProvider.
func NewServiceProvider(cfg *Config) (*ServiceProvider, error) {
const op = "saml.NewServiceProvider"
if cfg == nil {
return nil, fmt.Errorf(
"%s: no provider config provided",
op,
)
}
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf(
"%s: insufficient provider config: %w",
op, err,
)
}
return &ServiceProvider{
cfg: cfg,
}, nil
}
// Config returns the service provider config.
func (sp *ServiceProvider) Config() *Config {
return sp.cfg
}
// CreateMetadata creates the metadata XML for the service provider.
//
// Options:
// - InsecureWantAssertionsUnsigned
// - WithNameIDFormats
// - WithACSServiceBinding
// - WithAdditonalACSEndpoint
func (sp *ServiceProvider) CreateMetadata(opt ...Option) *metadata.EntityDescriptorSPSSO {
validUntil := sp.cfg.ValidUntil()
opts := getMetadataOptions(opt...)
spsso := metadata.EntityDescriptorSPSSO{}
spsso.EntityID = sp.cfg.EntityID
spsso.ValidUntil = &validUntil
spssoDescriptor := &metadata.SPSSODescriptor{}
spssoDescriptor.ProtocolSupportEnumeration = metadata.ProtocolSupportEnumerationProtocol
spssoDescriptor.NameIDFormat = opts.nameIDFormats
spssoDescriptor.AuthnRequestsSigned = false // always false for now until request signing is supported.
spssoDescriptor.WantAssertionsSigned = opts.wantAssertionsSigned
spssoDescriptor.AssertionConsumerService = []metadata.IndexedEndpoint{
{
Endpoint: metadata.Endpoint{
Binding: opts.acsServiceBinding,
Location: sp.cfg.AssertionConsumerServiceURL,
},
Index: 1,
},
}
for i, a := range opts.additionalACSs {
spssoDescriptor.AssertionConsumerService = append(
spssoDescriptor.AssertionConsumerService,
metadata.IndexedEndpoint{
Endpoint: a,
Index: i + 2, // The first index is already taken.
},
)
}
spsso.SPSSODescriptor = []*metadata.SPSSODescriptor{spssoDescriptor}
return &spsso
}
type idpMetadataOptions struct {
cache bool
useStale bool
clock clockwork.Clock
}
func idpMetadataOptionsDefault() idpMetadataOptions {
return idpMetadataOptions{
cache: true,
useStale: false,
clock: clockwork.NewRealClock(),
}
}
func getIDPMetadataOptions(opt ...Option) idpMetadataOptions {
opts := idpMetadataOptionsDefault()
ApplyOpts(&opts, opt...)
return opts
}
// WithCache control whether we should cache IDP Metadata.
func WithCache(cache bool) Option {
return func(o interface{}) {
if o, ok := o.(*idpMetadataOptions); ok {
o.cache = cache
}
}
}
// WithStale control whether we should use a stale IDP Metadata document if
// refreshing it fails.
func WithStale(stale bool) Option {
return func(o interface{}) {
if o, ok := o.(*idpMetadataOptions); ok {
o.useStale = stale
}
}
}
// IDPMetadata fetches the metadata XML document from the configured identity provider.
// Options:
// - WithClock
// - WithCache
// - WithStale
func (sp *ServiceProvider) IDPMetadata(opt ...Option) (*metadata.EntityDescriptorIDPSSO, error) {
const op = "saml.ServiceProvider.FetchIDPMetadata"
opts := getIDPMetadataOptions(opt...)
var err error
var ed *metadata.EntityDescriptorIDPSSO
isValid := func(md *metadata.EntityDescriptorIDPSSO) bool {
if md == nil {
return false
}
if md.ValidUntil == nil {
return true
}
return opts.clock.Now().Before(*md.ValidUntil)
}
isAlive := func(md *metadata.EntityDescriptorIDPSSO, expireAt *time.Time) bool {
if md == nil || !opts.cache || expireAt == nil {
return false
}
return opts.clock.Now().Before(*expireAt)
}
if opts.cache {
// We only take the lock when caching is enabled so that requests can be
// done concurrently when it is not
sp.metadataLock.Lock()
defer sp.metadataLock.Unlock()
switch {
case !isValid(sp.metadata):
sp.metadata = nil
sp.metadataCachedUntil = nil
case isValid(sp.metadata) && isAlive(sp.metadata, sp.metadataCachedUntil):
return sp.metadata, nil
}
}
// Order of switch case determines IDP metadata config precedence
switch {
case sp.cfg.MetadataURL != "":
ed, err = fetchIDPMetadata(sp.cfg.MetadataURL)
switch {
case err != nil && opts.useStale && isValid(sp.metadata):
// An error occurred but we have a cached metadata document that
// we can use
return sp.metadata, nil
case err != nil:
return nil, fmt.Errorf("%s: %w", op, err)
}
case sp.cfg.MetadataXML != "":
ed, err = parseIDPMetadata([]byte(sp.cfg.MetadataXML))
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
case sp.cfg.MetadataParameters != nil:
ed, err = constructIDPMetadata(sp.cfg.MetadataParameters)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
default:
return nil, fmt.Errorf("%s: no IDP metadata configuration set: %w", op, ErrInvalidParameter)
}
if !isValid(ed) {
return nil, fmt.Errorf("the IDP configuration was only valid until %s", ed.ValidUntil.Format(time.RFC3339))
}
sp.metadata = ed
sp.metadataCachedUntil = nil
if sp.metadata.CacheDuration != nil {
cachedUntil := opts.clock.Now().Add(time.Duration(*sp.metadata.CacheDuration))
sp.metadataCachedUntil = &cachedUntil
}
return ed, err
}
func (sp *ServiceProvider) destination(binding core.ServiceBinding) (string, error) {
const op = "saml.ServiceProvider.destination"
meta, err := sp.IDPMetadata()
if err != nil {
return "", fmt.Errorf("%s: failed to fetch metadata: %w", op, err)
}
destination, ok := meta.GetLocationForBinding(binding)
if !ok {
return "", fmt.Errorf(
"%s: no location for provided binding (%s) found: %w",
op, binding, ErrBindingUnsupported,
)
}
return destination, nil
}
func fetchIDPMetadata(metadataURL string) (*metadata.EntityDescriptorIDPSSO, error) {
res, err := http.Get(metadataURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch identity provider metadata: %w", err)
}
raw, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read http body: %w", err)
}
meta, err := parseIDPMetadata(raw)
if err != nil {
return nil, err
}
return meta, err
}
func parseIDPMetadata(rawXML []byte) (*metadata.EntityDescriptorIDPSSO, error) {
var ed metadata.EntityDescriptorIDPSSO
if err := xml.Unmarshal(rawXML, &ed); err != nil {
return nil, fmt.Errorf("failed to parse identity provider XML metadata: %w", err)
}
// [SDP-MD03] https://kantarainitiative.github.io/SAMLprofiles/saml2int.html#_metadata_and_trust_management
// IDPMetadata without a validUntil attribute on its root element MUST be rejected. IDPMetadata whose root element’s validUntil
// attribute extends beyond a deployer- or community-imposed threshold MUST be rejected.
// TODO: VALIDATE
return &ed, nil
}
func constructIDPMetadata(params *MetadataParameters) (*metadata.EntityDescriptorIDPSSO, error) {
cert, err := parsePEMCertificate([]byte(params.IDPCertificate))
if err != nil {
return nil, fmt.Errorf("failed to parse certificate: %w", err)
}
keyDescriptor := metadata.KeyDescriptor{
Use: metadata.KeyTypeSigning,
KeyInfo: metadata.KeyInfo{
KeyInfo: dsig.KeyInfo{
X509Data: dsig.X509Data{
X509Certificates: []dsig.X509Certificate{
{
Data: base64.StdEncoding.EncodeToString(cert.Raw),
},
},
},
},
},
}
idpSSODescriptor := &metadata.IDPSSODescriptor{
SSODescriptor: metadata.SSODescriptor{
RoleDescriptor: metadata.RoleDescriptor{
KeyDescriptor: []metadata.KeyDescriptor{keyDescriptor},
},
},
WantAuthnRequestsSigned: false,
SingleSignOnService: []metadata.Endpoint{
{
Binding: params.Binding,
Location: params.SingleSignOnURL,
},
},
}
return &metadata.EntityDescriptorIDPSSO{
EntityDescriptor: metadata.EntityDescriptor{
EntityID: params.Issuer,
},
IDPSSODescriptor: []*metadata.IDPSSODescriptor{idpSSODescriptor},
}, nil
}