-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathsaml2.py
731 lines (632 loc) · 28.9 KB
/
saml2.py
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
"""
A saml2 backend module for the satosa proxy
"""
import copy
import functools
import json
import logging
import warnings as _warnings
from base64 import urlsafe_b64encode
from urllib.parse import urlparse
from saml2 import BINDING_HTTP_REDIRECT
from saml2.client import Saml2Client
from saml2.config import SPConfig
from saml2.extension.mdui import NAMESPACE as UI_NAMESPACE
from saml2.metadata import create_metadata_string
from saml2.authn_context import requested_authn_context
from saml2.samlp import RequesterID
from saml2.samlp import Scoping
import satosa.logging_util as lu
import satosa.util as util
from satosa.base import SAMLBaseModule
from satosa.base import SAMLEIDASBaseModule
from satosa.base import STATE_KEY as STATE_KEY_BASE
from satosa.context import Context
from satosa.internal import AuthenticationInformation
from satosa.internal import InternalData
from satosa.exception import SATOSAAuthenticationError
from satosa.exception import SATOSAMissingStateError
from satosa.exception import SATOSAAuthenticationFlowError
from satosa.response import SeeOther, Response
from satosa.saml_util import make_saml_response
from satosa.metadata_creation.description import (
MetadataDescription, OrganizationDesc, ContactPersonDesc, UIInfoDesc
)
from satosa.backends.base import BackendModule
logger = logging.getLogger(__name__)
def get_memorized_idp(context, config, force_authn):
memorized_idp = (
config.get(SAMLBackend.KEY_MEMORIZE_IDP)
and context.state.get(Context.KEY_MEMORIZED_IDP)
)
use_when_force_authn = config.get(
SAMLBackend.KEY_USE_MEMORIZED_IDP_WHEN_FORCE_AUTHN
)
value = (not force_authn or use_when_force_authn) and memorized_idp
return value
def get_force_authn(context, config, sp_config):
"""
Return the force_authn value.
The value comes from one of three place:
- the configuration of the backend
- the context, as it came through in the AuthnRequest handled by the frontend.
note: the frontend should have been set to mirror the force_authn value.
- the cookie, as it has been stored by the proxy on a redirect to the DS
note: the frontend should have been set to mirror the force_authn value.
The value is either "true" or None
"""
mirror = config.get(SAMLBackend.KEY_MIRROR_FORCE_AUTHN)
from_state = mirror and context.state.get(Context.KEY_FORCE_AUTHN)
from_context = (
mirror and context.get_decoration(Context.KEY_FORCE_AUTHN) in ["true", "1"]
)
from_config = sp_config.getattr("force_authn", "sp")
is_set = str(from_state or from_context or from_config).lower() == "true"
value = "true" if is_set else None
return value
class SAMLBackend(BackendModule, SAMLBaseModule):
"""
A saml2 backend module (acting as a SP).
"""
KEY_DISCO_SRV = 'disco_srv'
KEY_SAML_DISCOVERY_SERVICE_URL = 'saml_discovery_service_url'
KEY_SAML_DISCOVERY_SERVICE_POLICY = 'saml_discovery_service_policy'
KEY_SP_CONFIG = 'sp_config'
KEY_SEND_REQUESTER_ID = 'send_requester_id'
KEY_MIRROR_FORCE_AUTHN = 'mirror_force_authn'
KEY_IS_PASSIVE = 'is_passive'
KEY_MEMORIZE_IDP = 'memorize_idp'
KEY_USE_MEMORIZED_IDP_WHEN_FORCE_AUTHN = 'use_memorized_idp_when_force_authn'
VALUE_ACR_COMPARISON_DEFAULT = 'exact'
def __init__(self, outgoing, internal_attributes, config, base_url, name):
"""
:type outgoing:
(satosa.context.Context, satosa.internal.InternalData) -> satosa.response.Response
:type internal_attributes: dict[str, dict[str, list[str] | str]]
:type config: dict[str, Any]
:type base_url: str
:type name: str
:param outgoing: Callback should be called by the module after
the authorization in the backend is done.
:param internal_attributes: Internal attribute map
:param config: The module config
:param base_url: base url of the service
:param name: name of the plugin
"""
super().__init__(outgoing, internal_attributes, base_url, name)
self.config = self.init_config(config)
self.discosrv = config.get(SAMLBackend.KEY_DISCO_SRV)
self.encryption_keys = []
self.outstanding_queries = {}
self.idp_blacklist_file = config.get('idp_blacklist_file', None)
sp_config = SPConfig().load(copy.deepcopy(config[SAMLBackend.KEY_SP_CONFIG]))
# if encryption_keypairs is defined, use those keys for decryption
# else, if key_file and cert_file are defined, use them for decryption
# otherwise, do not use any decryption key.
# ensure the choice is reflected back in the configuration.
sp_conf_encryption_keypairs = sp_config.getattr('encryption_keypairs', '')
sp_conf_key_file = sp_config.getattr('key_file', '')
sp_conf_cert_file = sp_config.getattr('cert_file', '')
sp_keypairs = (
sp_conf_encryption_keypairs
if sp_conf_encryption_keypairs
else [{'key_file': sp_conf_key_file, 'cert_file': sp_conf_cert_file}]
if sp_conf_key_file and sp_conf_cert_file
else []
)
sp_config.setattr('', 'encryption_keypairs', sp_keypairs)
# load the encryption keys
key_file_paths = [pair['key_file'] for pair in sp_keypairs]
for p in key_file_paths:
with open(p) as key_file:
self.encryption_keys.append(key_file.read())
# finally, initialize the client object
self.sp = Saml2Client(sp_config)
def get_idp_entity_id(self, context):
"""
:type context: satosa.context.Context
:rtype: str | None
:param context: The current context
:return: the entity_id of the idp or None
"""
idps = self.sp.metadata.identity_providers()
only_one_idp_in_metadata = (
"mdq" not in self.config["sp_config"]["metadata"]
and len(idps) == 1
)
only_idp = only_one_idp_in_metadata and idps[0]
target_entity_id = context.get_decoration(Context.KEY_TARGET_ENTITYID)
force_authn = get_force_authn(context, self.config, self.sp.config)
memorized_idp = get_memorized_idp(context, self.config, force_authn)
entity_id = only_idp or target_entity_id or memorized_idp or None
msg = {
"message": "Selected IdP",
"only_one": only_idp,
"target_entity_id": target_entity_id,
"force_authn": force_authn,
"memorized_idp": memorized_idp,
"entity_id": entity_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
return entity_id
def start_auth(self, context, internal_req):
"""
See super class method satosa.backends.base.BackendModule#start_auth
:type context: satosa.context.Context
:type internal_req: satosa.internal.InternalData
:rtype: satosa.response.Response
"""
entity_id = self.get_idp_entity_id(context)
if entity_id is None:
# since context is not passed to disco_query
# keep the information in the state cookie
context.state[Context.KEY_FORCE_AUTHN] = get_force_authn(
context, self.config, self.sp.config
)
return self.disco_query(context)
return self.authn_request(context, entity_id)
def disco_query(self, context):
"""
Makes a request to the discovery server
:type context: satosa.context.Context
:type internal_req: satosa.internal.InternalData
:rtype: satosa.response.SeeOther
:param context: The current context
:param internal_req: The request
:return: Response
"""
endpoints = self.sp.config.getattr("endpoints", "sp")
return_url = "{}/{}".format(self.base_url, endpoints["discovery_response"][0][0])
disco_url = (
context.get_decoration(SAMLBackend.KEY_SAML_DISCOVERY_SERVICE_URL)
or self.discosrv
)
disco_policy = context.get_decoration(
SAMLBackend.KEY_SAML_DISCOVERY_SERVICE_POLICY
)
args = {"return": return_url}
if disco_policy:
args["policy"] = disco_policy
loc = self.sp.create_discovery_service_request(
disco_url, self.sp.config.entityid, **args
)
msg = {
"message": "Sending user to the discovery service",
"disco_url": loc
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
return SeeOther(loc)
def construct_requested_authn_context(self, entity_id, *, target_accr=None):
acr_entry = (
target_accr
or util.get_dict_defaults(self.acr_mapping or {}, entity_id)
)
if not acr_entry:
return None
if type(acr_entry) is not dict:
acr_entry = {
"class_ref": acr_entry,
"comparison": self.VALUE_ACR_COMPARISON_DEFAULT,
}
authn_context = requested_authn_context(
acr_entry['class_ref'], comparison=acr_entry.get(
'comparison', self.VALUE_ACR_COMPARISON_DEFAULT
)
)
return authn_context
def authn_request(self, context, entity_id):
"""
Do an authorization request on idp with given entity id.
This is the start of the authorization.
:type context: satosa.context.Context
:type entity_id: str
:rtype: satosa.response.Response
:param context: The current context
:param entity_id: Target IDP entity id
:return: response to the user agent
"""
# If IDP blacklisting is enabled and the selected IDP is blacklisted,
# stop here
if self.idp_blacklist_file:
with open(self.idp_blacklist_file) as blacklist_file:
blacklist_array = json.load(blacklist_file)['blacklist']
if entity_id in blacklist_array:
msg = {
"message": "AuthnRequest Failed",
"error": f"Selected IdP with EntityID {entity_id} is blacklisted for this backend",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg)
kwargs = {}
target_accr = context.state.get(Context.KEY_TARGET_AUTHN_CONTEXT_CLASS_REF)
authn_context = self.construct_requested_authn_context(entity_id, target_accr=target_accr)
if authn_context:
kwargs["requested_authn_context"] = authn_context
if self.config.get(SAMLBackend.KEY_MIRROR_FORCE_AUTHN):
kwargs["force_authn"] = get_force_authn(
context, self.config, self.sp.config
)
if self.config.get(SAMLBackend.KEY_SEND_REQUESTER_ID):
requester = context.state.state_dict[STATE_KEY_BASE]['requester']
kwargs["scoping"] = Scoping(requester_id=[RequesterID(text=requester)])
if self.config.get(SAMLBackend.KEY_IS_PASSIVE):
kwargs["is_passive"] = "true"
try:
acs_endp, response_binding = self._get_acs(context)
acs_endp_url = "{}/{}".format(self.base_url, acs_endp)
relay_state = util.rndstr()
req_id, binding, http_info = self.sp.prepare_for_negotiated_authenticate(
entityid=entity_id,
assertion_consumer_service_url=acs_endp_url,
response_binding=response_binding,
relay_state=relay_state,
**kwargs,
)
except Exception as e:
msg = {
"message": "AuthnRequest Failed",
"error": f"Failed to construct the AuthnRequest for state: {e}",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg) from e
if self.sp.config.getattr('allow_unsolicited', 'sp') is False:
if req_id in self.outstanding_queries:
msg = {
"message": "AuthnRequest Failed",
"error": f"Request with duplicate id {req_id}",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg)
self.outstanding_queries[req_id] = req_id
context.state[self.name] = {"relay_state": relay_state}
return make_saml_response(binding, http_info)
def _get_acs(self, context):
"""
Select the AssertionConsumerServiceURL and binding.
:param context: The current context
:type context: satosa.context.Context
:return: Selected ACS URL and binding
:rtype: tuple(str, str)
"""
acs_strategy = self.config.get("acs_selection_strategy", "use_first_acs")
if acs_strategy == "use_first_acs":
acs_strategy_fn = self._use_first_acs
elif acs_strategy == "prefer_matching_host":
acs_strategy_fn = self._prefer_matching_host
else:
msg = "Invalid value for '{}' ({}). Using the first ACS instead".format(
"acs_selection_strategy", acs_strategy
)
logger.error(msg)
acs_strategy_fn = self._use_first_acs
return acs_strategy_fn(context)
def _use_first_acs(self, context):
return self.sp.config.getattr("endpoints", "sp")["assertion_consumer_service"][
0
]
def _prefer_matching_host(self, context):
acs_config = self.sp.config.getattr("endpoints", "sp")[
"assertion_consumer_service"
]
try:
hostname = context.http_headers["HTTP_HOST"]
for acs, binding in acs_config:
parsed_acs = urlparse(acs)
if hostname == parsed_acs.netloc:
msg = "Selected ACS '{}' based on the request".format(acs)
logline = lu.LOG_FMT.format(
id=lu.get_session_id(context.state), message=msg
)
logger.debug(logline)
return acs, binding
except (TypeError, KeyError):
pass
msg = "Can't find an ACS URL to this hostname ({}), selecting the first one".format(
context.http_headers.get("HTTP_HOST", "") if context.http_headers else ""
)
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.debug(logline)
return self._use_first_acs(context)
def authn_response(self, context, binding):
"""
Endpoint for the idp response
:type context: satosa.context,Context
:type binding: str
:rtype: satosa.response.Response
:param context: The current context
:param binding: The saml binding type
:return: response
"""
if self.name not in context.state:
"""
If we end up here, it means that the user returns to the proxy
without the SATOSA session cookie. This can happen at least in the
following cases:
- the user deleted the cookie from the browser
- the browser of the user blocked the cookie
- the user has completed an authentication flow, the cookie has
been removed by SATOSA and then the user used the back button
of their browser and resend the authentication response, but
without the SATOSA session cookie
"""
msg = {
"message": "Authentication failed",
"error": "Received AuthN response without a SATOSA session cookie",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAMissingStateError(msg)
samlresponse = context.request.get("SAMLResponse")
if not samlresponse:
msg = {
"message": "Authentication failed",
"error": "SAML Response not found in context.request",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg)
try:
authn_response = self.sp.parse_authn_request_response(
samlresponse, binding, outstanding=self.outstanding_queries
)
except Exception as e:
msg = {
"message": "Authentication failed",
"error": f"Failed to parse Authn response: {e}",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.debug(logline, exc_info=True)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg) from e
if self.sp.config.getattr('allow_unsolicited', 'sp') is False:
req_id = authn_response.in_response_to
if req_id not in self.outstanding_queries:
msg = {
"message": "Authentication failed",
"error": f"No corresponding request with id: {req_id}",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg)
del self.outstanding_queries[req_id]
# check if the relay_state matches the cookie state
if context.state[self.name].get("relay_state") != context.request["RelayState"]:
msg = {
"message": "Authentication failed",
"error": "Response state query param did not match relay state for request",
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
raise SATOSAAuthenticationError(context.state, msg)
context.decorate(Context.KEY_METADATA_STORE, self.sp.metadata)
if self.config.get(SAMLBackend.KEY_MEMORIZE_IDP):
issuer = authn_response.response.issuer.text.strip()
context.state[Context.KEY_MEMORIZED_IDP] = issuer
context.state.pop(Context.KEY_FORCE_AUTHN, None)
return self.auth_callback_func(context, self._translate_response(authn_response, context.state))
def disco_response(self, context):
"""
Endpoint for the discovery server response
:type context: satosa.context.Context
:rtype: satosa.response.Response
:param context: The current context
:return: response
"""
info = context.request
state = context.state
if 'SATOSA_BASE' not in state:
raise SATOSAAuthenticationFlowError("Discovery response without AuthN request")
entity_id = info.get("entityID")
msg = {
"message": "Received response from the discovery service",
"entity_id": entity_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.info(logline)
if not entity_id:
raise SATOSAAuthenticationError(state, msg) from err
return self.authn_request(context, entity_id)
def _translate_response(self, response, state):
"""
Translates a saml authorization response to an internal response
:type response: saml2.response.AuthnResponse
:rtype: satosa.internal.InternalData
:param response: The saml authorization response
:return: A translated internal response
"""
# The response may have been encrypted by the IdP so if we have an
# encryption key, try it.
if self.encryption_keys:
response.parse_assertion(keys=self.encryption_keys)
issuer = response.response.issuer.text
authn_context_ref, authenticating_authorities, authn_instant = next(
iter(response.authn_info()), [None, None, None]
)
authenticating_authority = (
authenticating_authorities[-1]
if authenticating_authorities
else None
)
auth_info = AuthenticationInformation(
auth_class_ref=authn_context_ref,
timestamp=authn_instant,
authority=authenticating_authority,
issuer=issuer,
)
# The SAML response may not include a NameID.
subject = response.get_subject()
name_id = subject.text if subject else None
name_id_format = subject.format if subject else None
attributes = self.converter.to_internal(
self.attribute_profile, response.ava,
)
internal_resp = InternalData(
auth_info=auth_info,
attributes=attributes,
subject_type=name_id_format,
subject_id=name_id,
)
msg = "backend received attributes: {}".format(response.ava)
logline = lu.LOG_FMT.format(id=lu.get_session_id(state), message=msg)
logger.debug(logline)
msg = {
"message": "Attributes received by the backend",
"issuer": issuer,
"attributes": " ".join(list(response.ava.keys()))
}
if name_id_format:
msg['name_id'] = name_id_format
logline = lu.LOG_FMT.format(id=lu.get_session_id(state), message=msg)
logger.info(logline)
return internal_resp
def _metadata_endpoint(self, context):
"""
Endpoint for retrieving the backend metadata
:type context: satosa.context.Context
:rtype: satosa.response.Response
:param context: The current context
:return: response with metadata
"""
msg = "Sending metadata response for entityId = {}".format(self.sp.config.entityid)
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.debug(logline)
metadata_string = create_metadata_string(
configfile=None, config=self.sp.config, valid=4
).decode("utf-8")
return Response(metadata_string, content="text/xml")
def register_endpoints(self):
"""
See super class method satosa.backends.base.BackendModule#register_endpoints
:rtype list[(str, ((satosa.context.Context, Any) -> Any, Any))]
"""
url_map = []
sp_endpoints = self.sp.config.getattr("endpoints", "sp")
for endp, binding in sp_endpoints["assertion_consumer_service"]:
url_map.append(("^%s$" % endp, functools.partial(self.authn_response, binding=binding)))
if binding == BINDING_HTTP_REDIRECT:
msg = " ".join(
[
"AssertionConsumerService endpoint with binding",
BINDING_HTTP_REDIRECT,
"is not recommended.",
"Quoting section 4.1.2 of",
"'Profiles for the OASIS Security Assertion Markup Language (SAML) V2.0':",
"The HTTP Redirect binding MUST NOT be used,",
"as the response will typically exceed the URL length",
"permitted by most user agents.",
]
)
_warnings.warn(msg, UserWarning)
if self.discosrv:
for endp, binding in sp_endpoints["discovery_response"]:
url_map.append(
("^%s$" % endp, self.disco_response))
if self.expose_entityid_endpoint():
url_map.append(
("^%s$" % sp_endpoints["metadata_exposal"], self._metadata_endpoint))
if self.enable_metadata_reload():
url_map.append(
("^%s$" % sp_endpoints["metadata_reload"], self._reload_metadata))
logger.debug(f"Loaded SAML2 endpoints: {url_map}")
return url_map
def _reload_metadata(self, context):
"""
Reload SAML metadata
"""
logger.debug("Reloading metadata")
res = self.sp.reload_metadata(
copy.deepcopy(self.config[SAMLBackend.KEY_SP_CONFIG]['metadata'])
)
message = "Metadata reload %s" % ("OK" if res else "failed")
status = "200 OK" if res else "500 FAILED"
return Response(message=message, status=status)
def get_metadata_desc(self):
"""
See super class satosa.backends.backend_base.BackendModule#get_metadata_desc
:rtype: satosa.metadata_creation.description.MetadataDescription
"""
entity_descriptions = []
idp_entities = self.sp.metadata.with_descriptor("idpsso")
for entity_id, entity in idp_entities.items():
description = MetadataDescription(urlsafe_b64encode(entity_id.encode("utf-8")).decode("utf-8"))
# Add organization info
try:
organization_info = entity["organization"]
except KeyError:
pass
else:
organization = OrganizationDesc()
for name_info in organization_info.get("organization_name", []):
organization.add_name(name_info["text"], name_info["lang"])
for display_name_info in organization_info.get("organization_display_name", []):
organization.add_display_name(display_name_info["text"], display_name_info["lang"])
for url_info in organization_info.get("organization_url", []):
organization.add_url(url_info["text"], url_info["lang"])
description.organization = organization
# Add contact person info
try:
contact_persons = entity["contact_person"]
except KeyError:
pass
else:
for person in contact_persons:
person_desc = ContactPersonDesc()
person_desc.contact_type = person.get("contact_type")
for address in person.get('email_address', []):
person_desc.add_email_address(address["text"])
if "given_name" in person:
person_desc.given_name = person["given_name"]["text"]
if "sur_name" in person:
person_desc.sur_name = person["sur_name"]["text"]
description.add_contact_person(person_desc)
# Add UI info
ui_info = self.sp.metadata.extension(entity_id, "idpsso_descriptor", "{}&UIInfo".format(UI_NAMESPACE))
if ui_info:
ui_info = ui_info[0]
ui_info_desc = UIInfoDesc()
for desc in ui_info.get("description", []):
ui_info_desc.add_description(desc["text"], desc["lang"])
for name in ui_info.get("display_name", []):
ui_info_desc.add_display_name(name["text"], name["lang"])
for logo in ui_info.get("logo", []):
ui_info_desc.add_logo(logo["text"], logo["width"], logo["height"], logo.get("lang"))
for keywords in ui_info.get("keywords", []):
ui_info_desc.add_keywords(keywords.get("text", []), keywords.get("lang"))
for information_url in ui_info.get("information_url", []):
ui_info_desc.add_information_url(information_url.get("text"), information_url.get("lang"))
for privacy_statement_url in ui_info.get("privacy_statement_url", []):
ui_info_desc.add_privacy_statement_url(
privacy_statement_url.get("text"), privacy_statement_url.get("lang")
)
description.ui_info = ui_info_desc
entity_descriptions.append(description)
return entity_descriptions
class SAMLEIDASBackend(SAMLBackend, SAMLEIDASBaseModule):
"""
A saml2 eidas backend module (acting as a SP).
"""
VALUE_ACR_CLASS_REF_DEFAULT = 'http://eidas.europa.eu/LoA/high'
VALUE_ACR_COMPARISON_DEFAULT = 'minimum'
def init_config(self, config):
config = super().init_config(config)
spec_eidas_sp = {
'acr_mapping': {
"": {
'class_ref': self.VALUE_ACR_CLASS_REF_DEFAULT,
'comparison': self.VALUE_ACR_COMPARISON_DEFAULT,
},
},
'sp_config.service.sp.authn_requests_signed': True,
'sp_config.service.sp.want_response_signed': True,
'sp_config.service.sp.allow_unsolicited': False,
'sp_config.service.sp.force_authn': True,
'sp_config.service.sp.hide_assertion_consumer_service': True,
'sp_config.service.sp.sp_type': ['private', 'public'],
'sp_config.service.sp.sp_type_in_metadata': [True, False],
}
return util.check_set_dict_defaults(config, spec_eidas_sp)