forked from crewjam/saml
-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.go
1322 lines (1190 loc) · 43.3 KB
/
schema.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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package saml
import (
"bytes"
"compress/flate"
"encoding/xml"
"strconv"
"time"
"github.com/beevik/etree"
"github.com/russellhaering/goxmldsig/etreeutils"
)
// RequestedAuthnContext represents the SAML object of the same name, an indication of the
// requirements on the authentication process.
type RequestedAuthnContext struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol RequestedAuthnContext"`
Comparison string `xml:",attr"`
AuthnContextClassRef string `xml:"urn:oasis:names:tc:SAML:2.0:assertion AuthnContextClassRef"`
}
// Element returns an etree.Element representing the object in XML form.
func (r *RequestedAuthnContext) Element() *etree.Element {
el := etree.NewElement("samlp:RequestedAuthnContext")
el.CreateAttr("Comparison", r.Comparison)
elContext := etree.NewElement("saml:AuthnContextClassRef")
elContext.SetText(r.AuthnContextClassRef)
el.AddChild(elContext)
return el
}
// AuthnRequest represents the SAML object of the same name, a request from a service provider
// to authenticate a user.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type AuthnRequest struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol AuthnRequest"`
ID string `xml:",attr"`
Version string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
Destination string `xml:",attr"`
Consent string `xml:",attr"`
Issuer *Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
Signature *etree.Element
Subject *Subject
NameIDPolicy *NameIDPolicy `xml:"urn:oasis:names:tc:SAML:2.0:protocol NameIDPolicy"`
Conditions *Conditions
RequestedAuthnContext *RequestedAuthnContext
// Scoping *Scoping // TODO
ForceAuthn *bool `xml:",attr"`
IsPassive *bool `xml:",attr"`
AssertionConsumerServiceIndex string `xml:",attr"`
AssertionConsumerServiceURL string `xml:",attr"`
ProtocolBinding string `xml:",attr"`
AttributeConsumingServiceIndex string `xml:",attr"`
ProviderName string `xml:",attr"`
}
// LogoutRequest represents the SAML object of the same name, a request from an IDP
// to destroy a user's session.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type LogoutRequest struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol LogoutRequest"`
ID string `xml:",attr"`
Version string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
NotOnOrAfter *time.Time `xml:",attr"`
Destination string `xml:",attr"`
Issuer *Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
NameID *NameID
Signature *etree.Element
SessionIndex *SessionIndex `xml:"SessionIndex"`
}
// Element returns an etree.Element representing the object in XML form.
func (r *LogoutRequest) Element() *etree.Element {
el := etree.NewElement("samlp:LogoutRequest")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
el.CreateAttr("ID", r.ID)
el.CreateAttr("Version", r.Version)
el.CreateAttr("IssueInstant", r.IssueInstant.Format(timeFormat))
if r.NotOnOrAfter != nil {
el.CreateAttr("NotOnOrAfter", r.NotOnOrAfter.Format(timeFormat))
}
if r.Destination != "" {
el.CreateAttr("Destination", r.Destination)
}
if r.Issuer != nil {
el.AddChild(r.Issuer.Element())
}
if r.Signature != nil {
el.AddChild(r.Signature)
}
if r.NameID != nil {
el.AddChild(r.NameID.Element())
}
if r.SessionIndex != nil {
el.AddChild(r.SessionIndex.Element())
}
return el
}
// MarshalXML implements xml.Marshaler
func (r *LogoutRequest) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
type Alias LogoutRequest
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
NotOnOrAfter *RelaxedTime `xml:",attr"`
*Alias
}{
IssueInstant: RelaxedTime(r.IssueInstant),
NotOnOrAfter: (*RelaxedTime)(r.NotOnOrAfter),
Alias: (*Alias)(r),
}
return e.Encode(aux)
}
// UnmarshalXML implements xml.Unmarshaler
func (r *LogoutRequest) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias LogoutRequest
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
NotOnOrAfter *RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
r.IssueInstant = time.Time(aux.IssueInstant)
r.NotOnOrAfter = (*time.Time)(aux.NotOnOrAfter)
return nil
}
// Bytes returns a byte array representation of the LogoutRequest
func (r *LogoutRequest) Bytes() ([]byte, error) {
doc := etree.NewDocument()
doc.SetRoot(r.Element())
buf, err := doc.WriteToBytes()
if err != nil {
return nil, err
}
return buf, nil
}
// Deflate returns a compressed byte array of the LogoutRequest
func (r *LogoutRequest) Deflate() ([]byte, error) {
buf, err := r.Bytes()
if err != nil {
return nil, err
}
var b bytes.Buffer
writer, err := flate.NewWriter(&b, flate.DefaultCompression)
if err != nil {
return nil, err
}
if _, err := writer.Write(buf); err != nil {
return nil, err
}
if err := writer.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// Element returns an etree.Element representing the object in XML form.
func (r *AuthnRequest) Element() *etree.Element {
el := etree.NewElement("samlp:AuthnRequest")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
el.CreateAttr("ID", r.ID)
el.CreateAttr("Version", r.Version)
el.CreateAttr("IssueInstant", r.IssueInstant.Format(timeFormat))
if r.Destination != "" {
el.CreateAttr("Destination", r.Destination)
}
if r.Consent != "" {
el.CreateAttr("Consent", r.Consent)
}
if r.Issuer != nil {
el.AddChild(r.Issuer.Element())
}
if r.Signature != nil {
el.AddChild(r.Signature)
}
if r.Subject != nil {
el.AddChild(r.Subject.Element())
}
if r.NameIDPolicy != nil {
el.AddChild(r.NameIDPolicy.Element())
}
if r.Conditions != nil {
el.AddChild(r.Conditions.Element())
}
if r.RequestedAuthnContext != nil {
el.AddChild(r.RequestedAuthnContext.Element())
}
// if r.Scoping != nil {
// el.AddChild(r.Scoping.Element())
// }
if r.ForceAuthn != nil {
el.CreateAttr("ForceAuthn", strconv.FormatBool(*r.ForceAuthn))
}
if r.IsPassive != nil {
el.CreateAttr("IsPassive", strconv.FormatBool(*r.IsPassive))
}
if r.AssertionConsumerServiceIndex != "" {
el.CreateAttr("AssertionConsumerServiceIndex", r.AssertionConsumerServiceIndex)
}
if r.AssertionConsumerServiceURL != "" {
el.CreateAttr("AssertionConsumerServiceURL", r.AssertionConsumerServiceURL)
}
if r.ProtocolBinding != "" {
el.CreateAttr("ProtocolBinding", r.ProtocolBinding)
}
if r.AttributeConsumingServiceIndex != "" {
el.CreateAttr("AttributeConsumingServiceIndex", r.AttributeConsumingServiceIndex)
}
if r.ProviderName != "" {
el.CreateAttr("ProviderName", r.ProviderName)
}
return el
}
// MarshalXML implements xml.Marshaler
func (r *AuthnRequest) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
type Alias AuthnRequest
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
IssueInstant: RelaxedTime(r.IssueInstant),
Alias: (*Alias)(r),
}
return e.Encode(aux)
}
// UnmarshalXML implements xml.Unmarshaler
func (r *AuthnRequest) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias AuthnRequest
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
r.IssueInstant = time.Time(aux.IssueInstant)
return nil
}
// Issuer represents the SAML object of the same name.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type Issuer struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
NameQualifier string `xml:",attr"`
SPNameQualifier string `xml:",attr"`
Format string `xml:",attr"`
SPProvidedID string `xml:",attr"`
Value string `xml:",chardata"`
}
// Element returns an etree.Element representing the object in XML form.
func (a *Issuer) Element() *etree.Element {
el := etree.NewElement("saml:Issuer")
if a.NameQualifier != "" {
el.CreateAttr("NameQualifier", a.NameQualifier)
}
if a.SPNameQualifier != "" {
el.CreateAttr("SPNameQualifier", a.SPNameQualifier)
}
if a.Format != "" {
el.CreateAttr("Format", a.Format)
}
if a.SPProvidedID != "" {
el.CreateAttr("SPProvidedID", a.SPProvidedID)
}
el.SetText(a.Value)
return el
}
// NameIDPolicy represents the SAML object of the same name.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type NameIDPolicy struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol NameIDPolicy"`
Format *string `xml:",attr"`
SPNameQualifier *string `xml:",attr"`
AllowCreate *bool `xml:",attr"`
}
// Element returns an etree.Element representing the object in XML form.
func (a *NameIDPolicy) Element() *etree.Element {
el := etree.NewElement("samlp:NameIDPolicy")
if a.Format != nil && *a.Format != "" {
el.CreateAttr("Format", *a.Format)
}
if a.SPNameQualifier != nil {
el.CreateAttr("SPNameQualifier", *a.SPNameQualifier)
}
if a.AllowCreate != nil {
el.CreateAttr("AllowCreate", strconv.FormatBool(*a.AllowCreate))
}
return el
}
// ArtifactResolve represents the SAML object of the same name.
type ArtifactResolve struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol ArtifactResolve"`
ID string `xml:",attr"`
Version string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
Issuer *Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
Signature *etree.Element
Artifact string `xml:"urn:oasis:names:tc:SAML:2.0:protocol Artifact"`
}
// Element returns an etree.Element representing the object in XML form.
func (r *ArtifactResolve) Element() *etree.Element {
el := etree.NewElement("samlp:ArtifactResolve")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
// Note: This namespace is not used by any element or attribute name, but
// is required so that the AttributeValue type element can have a value like
// "xs:string". If we don't declare it here, then it will be stripped by the
// cannonicalizer. This could be avoided by providing a prefix list to the
// cannonicalizer, but prefix lists do not appear to be implemented correctly
// in some libraries, so the safest action is to always produce XML that is
// (a) in canonical form and (b) does not require prefix lists.
el.CreateAttr("xmlns:xs", "http://www.w3.org/2001/XMLSchema")
el.CreateAttr("ID", r.ID)
el.CreateAttr("Version", r.Version)
el.CreateAttr("IssueInstant", r.IssueInstant.Format(timeFormat))
if r.Issuer != nil {
el.AddChild(r.Issuer.Element())
}
artifact := etree.NewElement("samlp:Artifact")
artifact.SetText(r.Artifact)
el.AddChild(artifact)
if r.Signature != nil {
el.AddChild(r.Signature)
}
return el
}
// SoapRequest returns a SOAP Envelope contining the ArtifactResolve request
func (r *ArtifactResolve) SoapRequest() *etree.Element {
envelope := etree.NewElement("soapenv:Envelope")
envelope.CreateAttr("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
envelope.CreateAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
body := etree.NewElement("soapenv:Body")
envelope.AddChild(body)
body.AddChild(r.Element())
return envelope
}
// MarshalXML implements xml.Marshaler
func (r *ArtifactResolve) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
type Alias ArtifactResolve
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
IssueInstant: RelaxedTime(r.IssueInstant),
Alias: (*Alias)(r),
}
return e.Encode(aux)
}
// UnmarshalXML implements xml.Unmarshaler
func (r *ArtifactResolve) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias ArtifactResolve
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
r.IssueInstant = time.Time(aux.IssueInstant)
return nil
}
// ArtifactResponse represents the SAML object of the same name.
type ArtifactResponse struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol ArtifactResponse"`
ID string `xml:",attr"`
InResponseTo string `xml:",attr"`
Version string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
Issuer *Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
Signature *etree.Element
Status Status `xml:"urn:oasis:names:tc:SAML:2.0:protocol Status"`
Response Response `xml:"urn:oasis:names:tc:SAML:2.0:protocol Response"`
}
// Element returns an etree.Element representing the object in XML form.
func (r *ArtifactResponse) Element() *etree.Element {
el := etree.NewElement("samlp:ArtifactResponse")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
// Note: This namespace is not used by any element or attribute name, but
// is required so that the AttributeValue type element can have a value like
// "xs:string". If we don't declare it here, then it will be stripped by the
// cannonicalizer. This could be avoided by providing a prefix list to the
// cannonicalizer, but prefix lists do not appear to be implemented correctly
// in some libraries, so the safest action is to always produce XML that is
// (a) in canonical form and (b) does not require prefix lists.
el.CreateAttr("xmlns:xs", "http://www.w3.org/2001/XMLSchema")
el.CreateAttr("ID", r.ID)
if r.InResponseTo != "" {
el.CreateAttr("InResponseTo", r.InResponseTo)
}
el.CreateAttr("Version", r.Version)
el.CreateAttr("IssueInstant", r.IssueInstant.Format(timeFormat))
if r.Issuer != nil {
el.AddChild(r.Issuer.Element())
}
if r.Signature != nil {
el.AddChild(r.Signature)
}
el.AddChild(r.Status.Element())
el.AddChild(r.Response.Element())
return el
}
// MarshalXML implements xml.Marshaler
func (r *ArtifactResponse) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
type Alias ArtifactResponse
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
IssueInstant: RelaxedTime(r.IssueInstant),
Alias: (*Alias)(r),
}
return e.Encode(aux)
}
// UnmarshalXML implements xml.Unmarshaler
func (r *ArtifactResponse) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias ArtifactResponse
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
r.IssueInstant = time.Time(aux.IssueInstant)
return nil
}
// Response represents the SAML object of the same name.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type Response struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol Response"`
ID string `xml:",attr"`
InResponseTo string `xml:",attr"`
Version string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
Destination string `xml:",attr"`
Consent string `xml:",attr"`
Issuer *Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
Signature *etree.Element
Status Status `xml:"urn:oasis:names:tc:SAML:2.0:protocol Status"`
// TODO(ross): more than one EncryptedAssertion is allowed
EncryptedAssertion *etree.Element `xml:"urn:oasis:names:tc:SAML:2.0:assertion EncryptedAssertion"`
// TODO(ross): more than one Assertion is allowed
Assertion *Assertion `xml:"urn:oasis:names:tc:SAML:2.0:assertion Assertion"`
}
// Element returns an etree.Element representing the object in XML form.
func (r *Response) Element() *etree.Element {
el := etree.NewElement("samlp:Response")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
// Note: This namespace is not used by any element or attribute name, but
// is required so that the AttributeValue type element can have a value like
// "xs:string". If we don't declare it here, then it will be stripped by the
// cannonicalizer. This could be avoided by providing a prefix list to the
// cannonicalizer, but prefix lists do not appear to be implemented correctly
// in some libraries, so the safest action is to always produce XML that is
// (a) in canonical form and (b) does not require prefix lists.
el.CreateAttr("xmlns:xs", "http://www.w3.org/2001/XMLSchema")
el.CreateAttr("ID", r.ID)
if r.InResponseTo != "" {
el.CreateAttr("InResponseTo", r.InResponseTo)
}
el.CreateAttr("Version", r.Version)
el.CreateAttr("IssueInstant", r.IssueInstant.Format(timeFormat))
if r.Destination != "" {
el.CreateAttr("Destination", r.Destination)
}
if r.Consent != "" {
el.CreateAttr("Consent", r.Consent)
}
if r.Issuer != nil {
el.AddChild(r.Issuer.Element())
}
if r.Signature != nil {
el.AddChild(r.Signature)
}
el.AddChild(r.Status.Element())
if r.EncryptedAssertion != nil {
el.AddChild(r.EncryptedAssertion)
}
if r.Assertion != nil {
el.AddChild(r.Assertion.Element())
}
return el
}
// MarshalXML implements xml.Marshaler
func (r *Response) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
type Alias Response
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
IssueInstant: RelaxedTime(r.IssueInstant),
Alias: (*Alias)(r),
}
return e.Encode(aux)
}
// UnmarshalXML implements xml.Unmarshaler
func (r *Response) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias Response
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
r.IssueInstant = time.Time(aux.IssueInstant)
return nil
}
// Status represents the SAML object of the same name.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type Status struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol Status"`
StatusCode StatusCode
StatusMessage *StatusMessage
StatusDetail *StatusDetail
}
// Element returns an etree.Element representing the object in XML form.
func (s *Status) Element() *etree.Element {
el := etree.NewElement("samlp:Status")
el.AddChild(s.StatusCode.Element())
if s.StatusMessage != nil {
el.AddChild(s.StatusMessage.Element())
}
if s.StatusDetail != nil {
el.AddChild(s.StatusDetail.Element())
}
return el
}
// StatusCode represents the SAML object of the same name.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
type StatusCode struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol StatusCode"`
Value string `xml:",attr"`
StatusCode *StatusCode
}
// Element returns an etree.Element representing the object in XML form.
func (s *StatusCode) Element() *etree.Element {
el := etree.NewElement("samlp:StatusCode")
el.CreateAttr("Value", s.Value)
if s.StatusCode != nil {
el.AddChild(s.StatusCode.Element())
}
return el
}
// StatusSuccess means the request succeeded. Additional information MAY be returned in the <StatusMessage> and/or <StatusDetail> elements.
//
// TODO(ross): this value is mostly constant, but is mutated in tests. Fix the hacky test so this can be const.
var StatusSuccess = "urn:oasis:names:tc:SAML:2.0:status:Success"
const (
// The permissible top-level <StatusCode> values are as follows:
// StatusRequester means the request could not be performed due to an error on the part of the requester.
StatusRequester = "urn:oasis:names:tc:SAML:2.0:status:Requester"
// StatusResponder means the request could not be performed due to an error on the part of the SAML responder or SAML authority.
StatusResponder = "urn:oasis:names:tc:SAML:2.0:status:Responder"
// StatusVersionMismatch means the SAML responder could not process the request because the version of the request message was incorrect.
StatusVersionMismatch = "urn:oasis:names:tc:SAML:2.0:status:VersionMismatch"
// The following second-level status codes are referenced at various places in this specification. Additional
// second-level status codes MAY be defined in future versions of the SAML specification. System entities
// are free to define more specific status codes by defining appropriate URI references.
// StatusAuthnFailed means the responding provider was unable to successfully authenticate the principal.
StatusAuthnFailed = "urn:oasis:names:tc:SAML:2.0:status:AuthnFailed"
// StatusInvalidAttrNameOrValue means Unexpected or invalid content was encountered within a <saml:Attribute> or <saml:AttributeValue> element.
StatusInvalidAttrNameOrValue = "urn:oasis:names:tc:SAML:2.0:status:InvalidAttrNameOrValue"
// StatusInvalidNameIDPolicy means the responding provider cannot or will not support the requested name identifier policy.
StatusInvalidNameIDPolicy = "urn:oasis:names:tc:SAML:2.0:status:InvalidNameIDPolicy"
// StatusNoAuthnContext means the specified authentication context requirements cannot be met by the responder.
StatusNoAuthnContext = "urn:oasis:names:tc:SAML:2.0:status:NoAuthnContext"
// StatusNoAvailableIDP is used by an intermediary to indicate that none of the supported identity provider <Loc> elements in an <IDPList> can be resolved or that none of the supported identity providers are available.
StatusNoAvailableIDP = "urn:oasis:names:tc:SAML:2.0:status:NoAvailableIDP"
// StatusNoPassive means Indicates the responding provider cannot authenticate the principal passively, as has been requested.
StatusNoPassive = "urn:oasis:names:tc:SAML:2.0:status:NoPassive" //nolint:gosec
// StatusNoSupportedIDP is used by an intermediary to indicate that none of the identity providers in an <IDPList> are supported by the intermediary.
StatusNoSupportedIDP = "urn:oasis:names:tc:SAML:2.0:status:NoSupportedIDP"
// StatusPartialLogout is used by a session authority to indicate to a session participant that it was not able to propagate logout to all other session participants.
StatusPartialLogout = "urn:oasis:names:tc:SAML:2.0:status:PartialLogout"
// StatusProxyCountExceeded means Indicates that a responding provider cannot authenticate the principal directly and is not permitted to proxy the request further.
StatusProxyCountExceeded = "urn:oasis:names:tc:SAML:2.0:status:ProxyCountExceeded"
// StatusRequestDenied means the SAML responder or SAML authority is able to process the request but has chosen not to respond. This status code MAY be used when there is concern about the security context of the request message or the sequence of request messages received from a particular requester.
StatusRequestDenied = "urn:oasis:names:tc:SAML:2.0:status:RequestDenied"
// StatusRequestUnsupported means the SAML responder or SAML authority does not support the request.
StatusRequestUnsupported = "urn:oasis:names:tc:SAML:2.0:status:RequestUnsupported"
// StatusRequestVersionDeprecated means the SAML responder cannot process any requests with the protocol version specified in the request.
StatusRequestVersionDeprecated = "urn:oasis:names:tc:SAML:2.0:status:RequestVersionDeprecated"
// StatusRequestVersionTooHigh means the SAML responder cannot process the request because the protocol version specified in the request message is a major upgrade from the highest protocol version supported by the responder.
StatusRequestVersionTooHigh = "urn:oasis:names:tc:SAML:2.0:status:RequestVersionTooHigh"
// StatusRequestVersionTooLow means the SAML responder cannot process the request because the protocol version specified in the request message is too low.
StatusRequestVersionTooLow = "urn:oasis:names:tc:SAML:2.0:status:RequestVersionTooLow"
// StatusResourceNotRecognized means the resource value provided in the request message is invalid or unrecognized.
StatusResourceNotRecognized = "urn:oasis:names:tc:SAML:2.0:status:ResourceNotRecognized"
// StatusTooManyResponses means the response message would contain more elements than the SAML responder is able to return.
StatusTooManyResponses = "urn:oasis:names:tc:SAML:2.0:status:TooManyResponses"
// StatusUnknownAttrProfile means an entity that has no knowledge of a particular attribute profile has been presented with an attribute means drawn from that profile.
StatusUnknownAttrProfile = "urn:oasis:names:tc:SAML:2.0:status:UnknownAttrProfile"
// StatusUnknownPrincipal means the responding provider does not recognize the principal specified or implied by the request.
StatusUnknownPrincipal = "urn:oasis:names:tc:SAML:2.0:status:UnknownPrincipal"
// StatusUnsupportedBinding means the SAML responder cannot properly fulfill the request using the protocol binding specified in the request.
StatusUnsupportedBinding = "urn:oasis:names:tc:SAML:2.0:status:UnsupportedBinding"
)
// StatusMessage represents the SAML element StatusMessage.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §3.2.2.3
type StatusMessage struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol StatusMessage"`
Value string `xml:",chardata"`
}
// Element returns an etree.Element representing the object in XML form.
func (sm StatusMessage) Element() *etree.Element {
el := etree.NewElement("samlp:StatusMessage")
el.SetText(sm.Value)
return el
}
// StatusDetail represents the SAML element StatusDetail.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §3.2.2.4
type StatusDetail struct {
Children []*etree.Element
}
// Element returns an etree.Element representing the object in XML form.
func (sm StatusDetail) Element() *etree.Element {
el := etree.NewElement("samlp:StatusDetail")
for _, child := range sm.Children {
el.AddChild(child)
}
return el
}
// Assertion represents the SAML element Assertion.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.3.3
type Assertion struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:assertion Assertion"`
ID string `xml:",attr"`
IssueInstant time.Time `xml:",attr"`
Version string `xml:",attr"`
Issuer Issuer `xml:"urn:oasis:names:tc:SAML:2.0:assertion Issuer"`
Signature *etree.Element
Subject *Subject
Conditions *Conditions
// Advice *Advice
// Statements []Statement
AuthnStatements []AuthnStatement `xml:"AuthnStatement"`
// AuthzDecisionStatements []AuthzDecisionStatement
AttributeStatements []AttributeStatement `xml:"AttributeStatement"`
}
// Element returns an etree.Element representing the object in XML form.
func (a *Assertion) Element() *etree.Element {
el := etree.NewElement("saml:Assertion")
el.CreateAttr("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion")
el.CreateAttr("Version", "2.0")
el.CreateAttr("ID", a.ID)
el.CreateAttr("IssueInstant", a.IssueInstant.Format(timeFormat))
el.AddChild(a.Issuer.Element())
if a.Signature != nil {
el.AddChild(a.Signature)
}
if a.Subject != nil {
el.AddChild(a.Subject.Element())
}
if a.Conditions != nil {
el.AddChild(a.Conditions.Element())
}
for _, authnStatement := range a.AuthnStatements {
el.AddChild(authnStatement.Element())
}
for _, attributeStatement := range a.AttributeStatements {
el.AddChild(attributeStatement.Element())
}
err := etreeutils.TransformExcC14n(el, canonicalizerPrefixList, false)
if err != nil {
panic(err)
}
return el
}
// UnmarshalXML implements xml.Unmarshaler
func (a *Assertion) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias Assertion
aux := &struct {
IssueInstant RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(a),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
a.IssueInstant = time.Time(aux.IssueInstant)
return nil
}
// Subject represents the SAML element Subject.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.4.1
type Subject struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:assertion Subject"`
// BaseID *BaseID ... TODO
NameID *NameID
// EncryptedID *EncryptedID ... TODO
SubjectConfirmations []SubjectConfirmation `xml:"SubjectConfirmation"`
}
// Element returns an etree.Element representing the object in XML form.
func (a *Subject) Element() *etree.Element {
el := etree.NewElement("saml:Subject")
if a.NameID != nil {
el.AddChild(a.NameID.Element())
}
for _, v := range a.SubjectConfirmations {
el.AddChild(v.Element())
}
return el
}
// NameID represents the SAML element NameID.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.2.3
type NameID struct {
NameQualifier string `xml:",attr"`
SPNameQualifier string `xml:",attr"`
Format string `xml:",attr"`
SPProvidedID string `xml:",attr"`
Value string `xml:",chardata"`
}
// Element returns an etree.Element representing the object in XML form.
func (a *NameID) Element() *etree.Element {
el := etree.NewElement("saml:NameID")
if a.NameQualifier != "" {
el.CreateAttr("NameQualifier", a.NameQualifier)
}
if a.SPNameQualifier != "" {
el.CreateAttr("SPNameQualifier", a.SPNameQualifier)
}
if a.Format != "" {
el.CreateAttr("Format", a.Format)
}
if a.SPProvidedID != "" {
el.CreateAttr("SPProvidedID", a.SPProvidedID)
}
if a.Value != "" {
el.SetText(a.Value)
}
return el
}
// SessionIndex represents the SAML element SessionIndex.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §3.7.1
type SessionIndex struct {
Value string `xml:",chardata"`
}
// Element returns an etree.Element representing the object in XML form.
func (s *SessionIndex) Element() *etree.Element {
el := etree.NewElement("samlp:SessionIndex")
el.CreateAttr("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol")
if s.Value != "" {
el.SetText(s.Value)
}
return el
}
// SubjectConfirmation represents the SAML element SubjectConfirmation.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.4.1.1
type SubjectConfirmation struct {
Method string `xml:",attr"`
// BaseID *BaseID ... TODO
NameID *NameID
// EncryptedID *EncryptedID ... TODO
SubjectConfirmationData *SubjectConfirmationData
}
// Element returns an etree.Element representing the object in XML form.
func (a *SubjectConfirmation) Element() *etree.Element {
el := etree.NewElement("saml:SubjectConfirmation")
el.CreateAttr("Method", a.Method)
if a.NameID != nil {
el.AddChild(a.NameID.Element())
}
if a.SubjectConfirmationData != nil {
el.AddChild(a.SubjectConfirmationData.Element())
}
return el
}
// SubjectConfirmationData represents the SAML element SubjectConfirmationData.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.4.1.2
type SubjectConfirmationData struct {
NotBefore time.Time `xml:",attr"`
NotOnOrAfter time.Time `xml:",attr"`
Recipient string `xml:",attr"`
InResponseTo string `xml:",attr"`
Address string `xml:",attr"`
}
// Element returns an etree.Element representing the object in XML form.
func (s *SubjectConfirmationData) Element() *etree.Element {
el := etree.NewElement("saml:SubjectConfirmationData")
if !s.NotBefore.IsZero() {
el.CreateAttr("NotBefore", s.NotBefore.Format(timeFormat))
}
if !s.NotOnOrAfter.IsZero() {
el.CreateAttr("NotOnOrAfter", s.NotOnOrAfter.Format(timeFormat))
}
if s.Recipient != "" {
el.CreateAttr("Recipient", s.Recipient)
}
if s.InResponseTo != "" {
el.CreateAttr("InResponseTo", s.InResponseTo)
}
if s.Address != "" {
el.CreateAttr("Address", s.Address)
}
return el
}
// MarshalXML implements xml.Marshaler
func (s *SubjectConfirmationData) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type Alias SubjectConfirmationData
aux := &struct {
NotOnOrAfter RelaxedTime `xml:",attr"`
*Alias
}{
NotOnOrAfter: RelaxedTime(s.NotOnOrAfter),
Alias: (*Alias)(s),
}
return e.EncodeElement(aux, start)
}
// UnmarshalXML implements xml.Unmarshaler
func (s *SubjectConfirmationData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias SubjectConfirmationData
aux := &struct {
NotOnOrAfter RelaxedTime `xml:",attr"`
*Alias
}{
Alias: (*Alias)(s),
}
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
s.NotOnOrAfter = time.Time(aux.NotOnOrAfter)
return nil
}
// Conditions represents the SAML element Conditions.
//
// See http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf §2.5.1
type Conditions struct {
NotBefore time.Time `xml:",attr"`
NotOnOrAfter time.Time `xml:",attr"`
AudienceRestrictions []AudienceRestriction `xml:"AudienceRestriction"`
OneTimeUse *OneTimeUse
ProxyRestriction *ProxyRestriction
}
// Element returns an etree.Element representing the object in XML form.
func (c *Conditions) Element() *etree.Element {
el := etree.NewElement("saml:Conditions")
if !c.NotBefore.IsZero() {
el.CreateAttr("NotBefore", c.NotBefore.Format(timeFormat))
}
if !c.NotOnOrAfter.IsZero() {
el.CreateAttr("NotOnOrAfter", c.NotOnOrAfter.Format(timeFormat))
}
for _, v := range c.AudienceRestrictions {
el.AddChild(v.Element())
}
if c.OneTimeUse != nil {
el.AddChild(c.OneTimeUse.Element())
}
if c.ProxyRestriction != nil {
el.AddChild(c.ProxyRestriction.Element())
}
return el
}
// MarshalXML implements xml.Marshaler
func (c *Conditions) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type Alias Conditions
aux := &struct {
NotBefore RelaxedTime `xml:",attr"`
NotOnOrAfter RelaxedTime `xml:",attr"`
*Alias
}{
NotBefore: RelaxedTime(c.NotBefore),
NotOnOrAfter: RelaxedTime(c.NotOnOrAfter),
Alias: (*Alias)(c),
}
return e.EncodeElement(aux, start)
}
// UnmarshalXML implements xml.Unmarshaler
func (c *Conditions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type Alias Conditions
aux := &struct {
NotBefore RelaxedTime `xml:",attr"`
NotOnOrAfter RelaxedTime `xml:",attr"`