forked from supabase/auth
/
samlassertion.go
177 lines (144 loc) · 4.72 KB
/
samlassertion.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
package api
import (
"strings"
"time"
"github.com/clanwyse/halo/internal/models"
"github.com/crewjam/saml"
)
type SAMLAssertion struct {
*saml.Assertion
}
const (
SAMLSubjectIDAttributeName = "urn:oasis:names:tc:SAML:attribute:subject-id"
)
// Attribute returns the first matching attribute value in the attribute
// statements where name equals the official SAML attribute Name or
// FriendlyName. Returns nil if such an attribute can't be found.
func (a *SAMLAssertion) Attribute(name string) []saml.AttributeValue {
var values []saml.AttributeValue
for _, stmt := range a.AttributeStatements {
for _, attr := range stmt.Attributes {
// TODO: maybe this should be case-insentivite equality?
if attr.Name == name || attr.FriendlyName == name {
values = append(values, attr.Values...)
}
}
}
return values
}
// UserID returns the best choice for a persistent user identifier on the
// Identity Provider side. Don't assume the format of the string returned, as
// it's Identity Provider specific.
func (a *SAMLAssertion) UserID() string {
// First we look up the SAMLSubjectIDAttributeName in the attribute
// section of the assertion, as this is the preferred way to
// persistently identify users in SAML 2.0.
// See: https://docs.oasis-open.org/security/saml-subject-id-attr/v1.0/cs01/saml-subject-id-attr-v1.0-cs01.html#_Toc536097226
values := a.Attribute(SAMLSubjectIDAttributeName)
if len(values) > 0 {
return values[0].Value
}
// Otherwise, fall back to the SubjectID value.
subjectID, isPersistent := a.SubjectID()
if !isPersistent {
return ""
}
return subjectID
}
// SubjectID returns the user identifier in present in the Subject section of
// the SAML assertion. Note that this way of identifying the Subject is
// generally superseded by the SAMLSubjectIDAttributeName assertion attribute;
// tho must be present in all assertions. It can have a few formats, of which
// the most important are: saml.EmailAddressNameIDFormat (meaning the user ID
// is an email address), saml.PersistentNameIDFormat (the user ID is an opaque
// string that does not change with each assertion, e.g. UUID),
// saml.TransientNameIDFormat (the user ID changes with each assertion -- can't
// be used to identify a user). The boolean returned identifies if the user ID
// is persistent. If it's an email address, it's lowercased just in case.
func (a *SAMLAssertion) SubjectID() (string, bool) {
if a.Subject == nil {
return "", false
}
if a.Subject.NameID == nil {
return "", false
}
if a.Subject.NameID.Value == "" {
return "", false
}
if a.Subject.NameID.Format == string(saml.EmailAddressNameIDFormat) {
return strings.ToLower(strings.TrimSpace(a.Subject.NameID.Value)), true
}
// all other NameID formats are regarded as persistent
isPersistent := a.Subject.NameID.Format != string(saml.TransientNameIDFormat)
return a.Subject.NameID.Value, isPersistent
}
// Email returns the best guess for an email address.
func (a *SAMLAssertion) Email() string {
attributeNames := []string{
"urn:oid:0.9.2342.19200300.100.1.3",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
"http://schemas.xmlsoap.org/claims/EmailAddress",
"mail",
"Mail",
"email",
}
for _, name := range attributeNames {
for _, attr := range a.Attribute(name) {
if attr.Value != "" {
return attr.Value
}
}
}
if a.Subject.NameID.Format == string(saml.EmailAddressNameIDFormat) {
return a.Subject.NameID.Value
}
return ""
}
// Process processes this assertion according to the SAMLAttributeMapping. Never returns nil.
func (a *SAMLAssertion) Process(mapping models.SAMLAttributeMapping) map[string]interface{} {
ret := make(map[string]interface{})
for key, mapper := range mapping.Keys {
names := []string{mapper.Name}
names = append(names, mapper.Names...)
setKey := false
for _, name := range names {
for _, attr := range a.Attribute(name) {
if attr.Value != "" {
ret[key] = attr.Value
setKey = true
break
}
}
if setKey {
break
}
}
if !setKey && mapper.Default != nil {
ret[key] = mapper.Default
}
}
return ret
}
// NotBefore extracts the time before which this assertion should not be
// considered.
func (a *SAMLAssertion) NotBefore() time.Time {
if a.Conditions != nil && !a.Conditions.NotBefore.IsZero() {
return a.Conditions.NotBefore.UTC()
}
return time.Time{}
}
// NotAfter extracts the time at which or after this assertion should not be
// considered.
func (a *SAMLAssertion) NotAfter() time.Time {
var notOnOrAfter time.Time
for _, statement := range a.AuthnStatements {
if statement.SessionNotOnOrAfter == nil {
continue
}
notOnOrAfter = *statement.SessionNotOnOrAfter
if !notOnOrAfter.IsZero() {
break
}
}
return notOnOrAfter
}