/
hybrid.go
executable file
·471 lines (413 loc) · 16.7 KB
/
hybrid.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
/*
- support for RelayState
- does not seem to work: incl. Capitalization content-security-policy: referrer no-referrer;
- redo no-referer - current version does not work !!!
- MDQ lookup by location also for hub md
- Trusted proxy
- wayf:wayf i hub_ops metadata
- AttributeNameFormat for Krib -> WAYF SPS - ie if none -> repeat wayf error both formats but error
- schacHomeOrganization
- schacHomeOrganizationType
- collect schema errors
- illegal attributes from IdP - ignore or provoke error
*/
package gohybrid
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gorilla/securecookie"
"github.com/wayf-dk/go-libxml2/types"
"github.com/wayf-dk/gosaml"
"github.com/wayf-dk/goxml"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type (
formdata struct {
Acs string
Samlresponse string
RelayState string
Ard template.JS
}
AttributeReleaseData struct {
Values map[string][]string
IdPDisplayName map[string]string
IdPLogo string
SPDisplayName map[string]string
SPDescription map[string]string
SPLogo string
SPEntityID string
Key string
Hash string
NoConsent bool
ConsentAsAService string
}
Conf struct {
DiscoveryService string
Domain string
HubEntityID string
EptidSalt string
HubRequestedAttributes *goxml.Xp
Internal, ExternalIdP, ExternalSP, Hub gosaml.Md
SecureCookieHashKey string
PostFormTemplate string
AttributeReleaseTemplate string
Basic2uri map[string]string
StdTiming gosaml.IdAndTiming
ElementsToSign []string
Certpath string
SSOServiceHandler func(*goxml.Xp, *goxml.Xp, *goxml.Xp, *goxml.Xp) (string, string, string, error)
BirkHandler func(*goxml.Xp, *goxml.Xp, *goxml.Xp) (*goxml.Xp, *goxml.Xp, error)
ACSServiceHandler func(*goxml.Xp, *goxml.Xp, *goxml.Xp, *goxml.Xp, *goxml.Xp) (AttributeReleaseData, error)
KribServiceHandler func(*goxml.Xp, *goxml.Xp, *goxml.Xp) (string, error)
DeKribify func(string) string
SLOStore gosaml.SLOInfoMap
Session HybridSession
}
HybridSession interface {
Set(http.ResponseWriter, *http.Request, string, []byte) error
Get(http.ResponseWriter, *http.Request, string) ([]byte, error)
Del(http.ResponseWriter, *http.Request, string) error
GetDel(http.ResponseWriter, *http.Request, string) ([]byte, error)
}
)
const (
spCertQuery = `./md:SPSSODescriptor/md:KeyDescriptor[@use="signing" or not(@use)]/ds:KeyInfo/ds:X509Data/ds:X509Certificate`
)
var (
_ = log.Printf // For debugging; delete when done.
_ = fmt.Printf
postForm, attributeReleaseForm *template.Template
hashKey []byte
seccookie *securecookie.SecureCookie
config = Conf{}
)
func Config(configuration Conf) {
config = configuration
hashKey, _ := hex.DecodeString(config.SecureCookieHashKey)
seccookie = securecookie.New(hashKey, nil)
postForm = template.Must(template.New("post").Parse(config.PostFormTemplate))
attributeReleaseForm = template.Must(template.New("post").Parse(config.AttributeReleaseTemplate))
}
func SSOService(w http.ResponseWriter, r *http.Request) (err error) {
defer r.Body.Close()
request, spmd, hubmd, relayState, err := gosaml.ReceiveAuthnRequest(r, config.Internal, config.Hub)
if err != nil {
return
}
entityID := spmd.Query1(nil, "@entityID")
idp := spmd.Query1(nil, "./md:Extensions/wayf:wayf/wayf:IDPList")
// how to fix this - in metadata ???
if idp != "" && !strings.HasPrefix(idp, "https://birk.wayf.dk/birk.php/") {
bify := regexp.MustCompile("^(https?://)(.*)$")
idp = bify.ReplaceAllString(idp, "${1}birk.wayf.dk/birk.php/$2")
}
if idp == "" {
idp = request.Query1(nil, "./samlp:Scoping/samlp:IDPList/samlp:IDPEntry/@ProviderID")
}
if idp == "" {
idp = r.URL.Query().Get("idpentityid")
}
if idp == "" {
data := url.Values{}
data.Set("return", "https://"+r.Host+r.RequestURI)
data.Set("returnIDParam", "idpentityid")
data.Set("entityID", entityID)
http.Redirect(w, r, config.DiscoveryService+data.Encode(), http.StatusFound)
} else {
idpmd, err := config.ExternalIdP.MDQ(idp)
if err != nil {
return err
}
kribID, acsurl, ssourl, err := config.SSOServiceHandler(request, spmd, hubmd, idpmd)
if err != nil {
return err
}
request.QueryDashP(nil, "/saml:Issuer", kribID, nil)
request.QueryDashP(nil, "@AssertionConsumerServiceURL", acsurl, nil)
request.QueryDashP(nil, "@Destination", ssourl, nil)
u, _ := gosaml.SAMLRequest2Url(request, relayState, "", "", "")
http.Redirect(w, r, u.String(), http.StatusFound)
}
return
}
func BirkService(w http.ResponseWriter, r *http.Request) (err error) {
// use incoming request for crafting the new one
// remember to add the Scoping element to inform the IdP of requesterID - if stated in metadata for the IdP
// check ad-hoc feds overlap
defer r.Body.Close()
var directToSP bool
request, mdsp, mdbirkidp, relayState, err := gosaml.ReceiveAuthnRequest(r, config.ExternalSP, config.ExternalIdP)
// is this a request from KRIB?
if err != nil {
e, ok := err.(goxml.Werror)
if ok && e.Cause == gosaml.ACSError {
// or is it coming directly from a SP
request, mdsp, mdbirkidp, relayState, err = gosaml.ReceiveAuthnRequest(r, config.Internal, config.ExternalIdP)
}
if err != nil {
return
}
// If we get here we need to tag the request as a direct BIRK to SP - otherwise we will end up sending the response to KRIB
directToSP = true
}
request.QueryDashP(nil, "./@DirectToSP", strconv.FormatBool(directToSP), nil)
// Save the request in a session for when the response comes back
config.Session.Set(w, r, "BIRK", []byte(request.Doc.Dump(true)))
mdhub, mdidp, err := config.BirkHandler(request, mdsp, mdbirkidp)
if err != nil {
return
}
// why not use orig request?
newrequest, err := gosaml.NewAuthnRequest(config.StdTiming.Refresh(), request, mdhub, mdidp, "")
if err != nil {
return
}
var privatekey []byte
passwd := "-"
wars := mdidp.Query1(nil, `./md:IDPSSODescriptor/@WantAuthnRequestsSigned`)
switch wars {
case "true", "1":
cert := mdhub.Query1(nil, spCertQuery) // actual signing key is always first
var keyname string
keyname, _, err = gosaml.PublicKeyInfo(cert)
if err != nil {
return err
}
privatekey, err = ioutil.ReadFile(config.Certpath + keyname + ".key")
if err != nil {
return
}
}
u, _ := gosaml.SAMLRequest2Url(newrequest, relayState, string(privatekey), passwd, "sha256")
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
func ACSService(w http.ResponseWriter, r *http.Request) (err error) {
defer r.Body.Close()
value, err := config.Session.GetDel(w, r, "BIRK")
if err != nil {
return
}
// we checked the request when we received in birkService - we can use it without fear ie. we just parse it
request := goxml.NewXp(string(value))
directToSP := request.Query1(nil, "./@DirectToSP") == "true"
spMetadataSet := config.ExternalSP
if directToSP {
spMetadataSet = config.Internal
}
sp_md, err := spMetadataSet.MDQ(request.Query1(nil, "/samlp:AuthnRequest/saml:Issuer"))
if err != nil {
return
}
var birkmd *goxml.Xp
if directToSP {
birkmd, err = config.Hub.MDQ(config.HubEntityID)
} else {
birkmd, err = config.ExternalIdP.MDQ(request.Query1(nil, "/samlp:AuthnRequest/@Destination"))
}
if err != nil {
return
}
response, idp_md, hub_md, relayState, err := gosaml.ReceiveSAMLResponse(r, config.Internal, config.Hub)
if err != nil {
return
}
var newresponse *goxml.Xp
var ard AttributeReleaseData
if response.Query1(nil, `samlp:Status/samlp:StatusCode/@Value`) == "urn:oasis:names:tc:SAML:2.0:status:Success" {
ard, err = config.ACSServiceHandler(idp_md, config.HubRequestedAttributes, sp_md, request, response)
if err != nil {
return err
}
newresponse = gosaml.NewResponse(config.StdTiming.Refresh(), birkmd, sp_md, request, response)
nameid := newresponse.Query(nil, "./saml:Assertion/saml:Subject/saml:NameID")[0]
// respect nameID in req, give persistent id + all computed attributes + nameformat conversion
// The reponse at this time contains a full attribute set
nameidformat := request.Query1(nil, "./samlp:NameIDPolicy/@Format")
if nameidformat == gosaml.Persistent {
newresponse.QueryDashP(nameid, "@Format", gosaml.Persistent, nil)
eptid := newresponse.Query1(nil, `./saml:Assertion/saml:AttributeStatement/saml:Attribute[@FriendlyName="eduPersonTargetedID"]/saml:AttributeValue`)
newresponse.QueryDashP(nameid, ".", eptid, nil)
} else if nameidformat == gosaml.Transient {
newresponse.QueryDashP(nameid, ".", gosaml.Id(), nil)
}
handleAttributeNameFormat(newresponse, sp_md)
for _, q := range config.ElementsToSign {
err = gosaml.SignResponse(newresponse, q, birkmd)
if err != nil {
return err
}
}
if _, err = SLOInfoHandler(w, r, response, newresponse, hub_md, "BIRK-SLO"); err != nil {
return
}
} else {
newresponse = gosaml.NewErrorResponse(config.StdTiming.Refresh(), birkmd, sp_md, request, response)
err = gosaml.SignResponse(newresponse, "/samlp:Response", birkmd)
if err != nil {
return
}
ard = AttributeReleaseData{NoConsent: true}
}
// when consent as a service is ready - we will post to that
acs := newresponse.Query1(nil, "@Destination")
ardjson, err := json.Marshal(ard)
data := formdata{Acs: acs, Samlresponse: base64.StdEncoding.EncodeToString([]byte(newresponse.Doc.Dump(false))), RelayState: relayState, Ard: template.JS(ardjson)}
attributeReleaseForm.Execute(w, data)
return
}
func KribService(w http.ResponseWriter, r *http.Request) (err error) {
// check ad-hoc feds overlap
defer r.Body.Close()
response, birkmd, kribmd, relayState, err := gosaml.ReceiveSAMLResponse(r, config.ExternalIdP, config.ExternalSP)
if err != nil {
return
}
destination, err := config.KribServiceHandler(response, birkmd, kribmd)
if err != nil {
return
}
response.QueryDashP(nil, "@Destination", destination, nil)
issuer := config.HubEntityID
response.QueryDashP(nil, "./saml:Issuer", issuer, nil)
mdhub, err := config.Hub.MDQ(config.HubEntityID)
if err != nil {
return err
}
if response.Query1(nil, `samlp:Status/samlp:StatusCode/@Value`) == "urn:oasis:names:tc:SAML:2.0:status:Success" {
if _, err = SLOInfoHandler(w, r, response, response, kribmd, "KRIB-SLO"); err != nil {
return err
}
response.QueryDashP(nil, "./saml:Assertion/saml:Issuer", issuer, nil)
// Krib always receives attributes with nameformat=urn. Before sending to the real SP we need to look into
// the metadata for SP to determine the actual nameformat - as WAYF supports both for internal SPs.
response.QueryDashP(nil, "./saml:Assertion/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData/@Recipient", destination, nil)
mdsp, err := config.Internal.MDQ(destination)
if err != nil {
return err
}
handleAttributeNameFormat(response, mdsp)
for _, q := range config.ElementsToSign {
err = gosaml.SignResponse(response, q, mdhub)
if err != nil {
return err
}
}
} else {
err = gosaml.SignResponse(response, "/samlp:Response", mdhub)
if err != nil {
return
}
}
data := formdata{Acs: destination, Samlresponse: base64.StdEncoding.EncodeToString([]byte(response.Doc.Dump(false))), RelayState: relayState}
postForm.Execute(w, data)
return
}
func BirkSLOService(w http.ResponseWriter, r *http.Request) (err error) {
return SLOService(w, r, config.ExternalSP, config.ExternalIdP, config.Hub, config.Internal, gosaml.IdPRole, "BIRK-SLO")
}
func KribSLOService(w http.ResponseWriter, r *http.Request) (err error) {
return SLOService(w, r, config.ExternalIdP, config.ExternalSP, config.Hub, config.Internal, gosaml.SPRole, "KRIB-SLO")
}
func SPSLOService(w http.ResponseWriter, r *http.Request) (err error) {
return SLOService(w, r, config.Internal, config.Hub, config.ExternalIdP, config.ExternalSP, gosaml.SPRole, "BIRK-SLO")
}
func IdPSLOService(w http.ResponseWriter, r *http.Request) (err error) {
return SLOService(w, r, config.Internal, config.Hub, config.ExternalSP, config.ExternalIdP, gosaml.IdPRole, "KRIB-SLO")
}
func SLOService(w http.ResponseWriter, r *http.Request, issuerMdSet, destinationMdSet, finalIssuerMdSet, finalDestinationMdSet gosaml.Md, role int, tag string) (err error) {
req := []string{"idpreq", "spreq"}
res := []string{"idpres", "spres"}
defer r.Body.Close()
r.ParseForm()
if _, ok := r.Form["SAMLRequest"]; ok {
request, issuer, destination, relayState, err := gosaml.ReceiveLogoutMessage(r, issuerMdSet, destinationMdSet, role)
if err != nil {
return err
}
sloinfo, _ := SLOInfoHandler(w, r, request, request, nil, tag)
if sloinfo.NameID != "" {
finaldestination, err := finalDestinationMdSet.MDQ(sloinfo.EntityID)
if err != nil {
return err
}
newRequest := gosaml.NewLogoutRequest(config.StdTiming.Refresh(), issuer, finaldestination, request, sloinfo)
async := request.QueryBool(nil, "boolean(./samlp:Extensions/aslo:Asynchronous)")
if !async {
config.Session.Set(w, r, tag+"-REQ", []byte(request.Doc.Dump(true)))
}
// send LogoutRequest to sloinfo.EntityID med sloinfo.NameID as nameid
legacyStatLog("birk-99", "saml20-idp-SLO "+req[role], issuer.Query1(nil, "@entityID"), destination.Query1(nil, "@entityID"), sloinfo.NameID+fmt.Sprintf(" async:%t", async))
u, _ := gosaml.SAMLRequest2Url(newRequest, relayState, "", "", "")
http.Redirect(w, r, u.String(), http.StatusFound)
} else {
err = fmt.Errorf("no Logout info found")
return err
}
} else if _, ok := r.Form["SAMLResponse"]; ok {
response, issuer, destination, relayState, err := gosaml.ReceiveLogoutMessage(r, issuerMdSet, destinationMdSet, role)
if err != nil {
return err
}
value, err := config.Session.GetDel(w, r, tag+"-REQ")
if err != nil {
return err
}
legacyStatLog("birk-99", "saml20-idp-SLO "+res[role], issuer.Query1(nil, "@entityID"), destination.Query1(nil, "@entityID"), "")
// we checked the request when we received in birkService - we can use it without fear ie. we just parse it
request := goxml.NewXp(string(value))
issuermd, _ := finalIssuerMdSet.MDQ(request.Query1(nil, "@Destination"))
destinationmd, _ := finalDestinationMdSet.MDQ(request.Query1(nil, "./saml:Issuer"))
newResponse := gosaml.NewLogoutResponse(config.StdTiming.Refresh(), issuermd, destinationmd, request, response)
u, _ := gosaml.SAMLRequest2Url(newResponse, relayState, "", "", "")
http.Redirect(w, r, u.String(), http.StatusFound)
// forward the LogoutResponse to orig sender
} else {
err = fmt.Errorf("no LogoutRequest/logoutResponse found")
return err
}
return
}
// Saves or retrieves the SLO info relevant to the contents of the samlMessage
// For now uses cookies to keep the SLOInfo
func SLOInfoHandler(w http.ResponseWriter, r *http.Request, samlIn, samlOut, destination *goxml.Xp, tag string) (sloinfo *gosaml.SLOInfo, err error) {
nameIDHash := gosaml.NameIDHash(samlOut, tag)
switch samlIn.QueryString(nil, "local-name(/*)") {
case "LogoutRequest":
sloinfo = config.SLOStore.GetSLOInfo(w, r, nameIDHash)
case "LogoutResponse":
// needed at all ???
case "Response":
config.SLOStore.PutSLOInfo(w, r, nameIDHash, gosaml.NewSLOInfo(samlIn, destination))
}
return
}
func legacyStatLog(server, tag, idp, sp, hash string) {
log.Printf("%s ssp-wayf[%s]: 5 STAT [%d] %s %s %s %s\n", server, "007", time.Now().UnixNano(), tag, idp, sp, hash)
}
func handleAttributeNameFormat(response, mdsp *goxml.Xp) {
requestedattributes := mdsp.Query(nil, "./md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute")
attributestatement := response.Query(nil, "./saml:Assertion/saml:AttributeStatement")[0]
for _, attr := range requestedattributes {
nameFormat, _ := attr.(types.Element).GetAttribute("NameFormat")
if nameFormat.NodeValue() == "urn:oasis:names:tc:SAML:2.0:attrname-format:basic" {
basicname, _ := attr.(types.Element).GetAttribute("Name")
uriname := config.Basic2uri[basicname.NodeValue()]
responseattribute := response.Query(attributestatement, "saml:Attribute[@Name='"+uriname+"']")
if len(responseattribute) > 0 {
responseattribute[0].(types.Element).SetAttribute("Name", basicname.NodeValue())
responseattribute[0].(types.Element).SetAttribute("NameFormat", "urn:oasis:names:tc:SAML:2.0:attrname-format:basic")
}
}
}
}