57
57
"subject_locality" : "subject_locality"
58
58
}
59
59
60
+
60
61
def _shelve_compat (name , * args , ** kwargs ):
61
62
try :
62
63
return shelve .open (name , * args , ** kwargs )
@@ -132,7 +133,7 @@ def init_config(self, stype="idp"):
132
133
elif isinstance (dbspec , six .string_types ):
133
134
idb = _shelve_compat (dbspec , writeback = True , protocol = 2 )
134
135
else : # database spec is a a 2-tuple (type, address)
135
- #print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
136
+ # print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
136
137
(typ , addr ) = dbspec
137
138
if typ == "shelve" :
138
139
idb = _shelve_compat (addr , writeback = True , protocol = 2 )
@@ -289,8 +290,10 @@ def parse_name_id_mapping_request(self, xml_string, binding):
289
290
290
291
# ------------------------------------------------------------------------
291
292
292
- def setup_assertion (self , authn , sp_entity_id , in_response_to , consumer_url , name_id , policy , _issuer ,
293
- authn_statement , identity , best_effort , sign_response , add_subject = True ):
293
+ def setup_assertion (self , authn , sp_entity_id , in_response_to , consumer_url ,
294
+ name_id , policy , _issuer ,
295
+ authn_statement , identity , best_effort , sign_response ,
296
+ add_subject = True ):
294
297
ast = Assertion (identity )
295
298
ast .acs = self .config .getattr ("attribute_converters" , "idp" )
296
299
if policy is None :
@@ -305,34 +308,40 @@ def setup_assertion(self, authn, sp_entity_id, in_response_to, consumer_url, nam
305
308
if authn : # expected to be a dictionary
306
309
# Would like to use dict comprehension but ...
307
310
authn_args = dict ([
308
- (AUTHN_DICT_MAP [k ], v ) for k , v in authn .items ()
309
- if k in AUTHN_DICT_MAP ])
311
+ (AUTHN_DICT_MAP [k ], v ) for k , v in
312
+ authn .items ()
313
+ if k in AUTHN_DICT_MAP ])
310
314
311
315
assertion = ast .construct (sp_entity_id , in_response_to ,
312
316
consumer_url , name_id ,
313
317
self .config .attribute_converters ,
314
- policy , issuer = _issuer , add_subject = add_subject ,
318
+ policy , issuer = _issuer ,
319
+ add_subject = add_subject ,
315
320
** authn_args )
316
321
elif authn_statement : # Got a complete AuthnStatement
317
322
assertion = ast .construct (sp_entity_id , in_response_to ,
318
323
consumer_url , name_id ,
319
324
self .config .attribute_converters ,
320
325
policy , issuer = _issuer ,
321
- authn_statem = authn_statement , add_subject = add_subject )
326
+ authn_statem = authn_statement ,
327
+ add_subject = add_subject )
322
328
else :
323
329
assertion = ast .construct (sp_entity_id , in_response_to ,
324
330
consumer_url , name_id ,
325
331
self .config .attribute_converters ,
326
- policy , issuer = _issuer , add_subject = add_subject )
332
+ policy , issuer = _issuer ,
333
+ add_subject = add_subject )
327
334
return assertion
328
335
329
336
def _authn_response (self , in_response_to , consumer_url ,
330
337
sp_entity_id , identity = None , name_id = None ,
331
338
status = None , authn = None , issuer = None , policy = None ,
332
339
sign_assertion = False , sign_response = False ,
333
340
best_effort = False , encrypt_assertion = False ,
334
- encrypt_cert_advice = None , encrypt_cert_assertion = None , authn_statement = None ,
335
- encrypt_assertion_self_contained = False , encrypted_advice_attributes = False , pefim = False ):
341
+ encrypt_cert_advice = None , encrypt_cert_assertion = None ,
342
+ authn_statement = None ,
343
+ encrypt_assertion_self_contained = False ,
344
+ encrypted_advice_attributes = False , pefim = False ):
336
345
""" Create a response. A layer of indirection.
337
346
338
347
:param in_response_to: The session identifier of the request
@@ -351,23 +360,27 @@ def _authn_response(self, in_response_to, consumer_url,
351
360
:param best_effort: Even if not the SPs demands can be met send a
352
361
response.
353
362
:param encrypt_assertion: True if assertions should be encrypted.
354
- :param encrypt_assertion_self_contained: True if all encrypted assertions should have alla namespaces
363
+ :param encrypt_assertion_self_contained: True if all encrypted
364
+ assertions should have alla namespaces
355
365
selfcontained.
356
- :param encrypted_advice_attributes: True if assertions in the advice element should be encrypted.
357
- :param encrypt_cert_advice: Certificate to be used for encryption of assertions in the advice element.
358
- :param encrypt_cert_assertion: Certificate to be used for encryption of assertions.
366
+ :param encrypted_advice_attributes: True if assertions in the advice
367
+ element should be encrypted.
368
+ :param encrypt_cert_advice: Certificate to be used for encryption of
369
+ assertions in the advice element.
370
+ :param encrypt_cert_assertion: Certificate to be used for encryption
371
+ of assertions.
359
372
:param authn_statement: Authentication statement.
360
373
:param sign_assertion: True if assertions should be signed.
361
- :param pefim: True if a response according to the PEFIM profile should be created.
374
+ :param pefim: True if a response according to the PEFIM profile
375
+ should be created.
362
376
:return: A response instance
363
377
"""
364
378
365
- to_sign = []
366
379
args = {}
367
- #if identity:
380
+ # if identity:
368
381
_issuer = self ._issuer (issuer )
369
382
370
- #if encrypt_assertion and show_nameid:
383
+ # if encrypt_assertion and show_nameid:
371
384
# tmp_name_id = name_id
372
385
# name_id = None
373
386
# name_id = None
@@ -379,58 +392,56 @@ def _authn_response(self, in_response_to, consumer_url,
379
392
if pefim :
380
393
encrypted_advice_attributes = True
381
394
encrypt_assertion_self_contained = True
382
- assertion_attributes = self .setup_assertion (None , sp_entity_id , None , None , None , policy ,
383
- None , None , identity , best_effort , sign_response , False )
384
- assertion = self .setup_assertion (authn , sp_entity_id , in_response_to , consumer_url ,
385
- name_id , policy , _issuer , authn_statement , [], True ,
395
+ assertion_attributes = self .setup_assertion (None , sp_entity_id ,
396
+ None , None , None ,
397
+ policy ,
398
+ None , None , identity ,
399
+ best_effort ,
400
+ sign_response , False )
401
+ assertion = self .setup_assertion (authn , sp_entity_id ,
402
+ in_response_to , consumer_url ,
403
+ name_id , policy , _issuer ,
404
+ authn_statement , [], True ,
386
405
sign_response )
387
406
assertion .advice = saml .Advice ()
388
407
389
- #assertion.advice.assertion_id_ref.append(saml.AssertionIDRef())
390
- #assertion.advice.assertion_uri_ref.append(saml.AssertionURIRef())
408
+ # assertion.advice.assertion_id_ref.append(saml.AssertionIDRef())
409
+ # assertion.advice.assertion_uri_ref.append(saml.AssertionURIRef())
391
410
assertion .advice .assertion .append (assertion_attributes )
392
411
else :
393
- assertion = self .setup_assertion (authn , sp_entity_id , in_response_to , consumer_url ,
394
- name_id , policy , _issuer , authn_statement , identity , True ,
412
+ assertion = self .setup_assertion (authn , sp_entity_id ,
413
+ in_response_to , consumer_url ,
414
+ name_id , policy , _issuer ,
415
+ authn_statement , identity , True ,
395
416
sign_response )
396
417
397
418
to_sign = []
398
419
if not encrypt_assertion :
399
420
if sign_assertion :
400
- assertion .signature = pre_signature_part (assertion .id , self .sec .my_cert , 1 )
421
+ assertion .signature = pre_signature_part (assertion .id ,
422
+ self .sec .my_cert , 1 )
401
423
to_sign .append ((class_name (assertion ), assertion .id ))
402
424
403
- #if not encrypted_advice_attributes:
404
- # if sign_assertion:
405
- # if assertion.advice and assertion.advice.assertion:
406
- # for tmp_assertion in assertion.advice.assertion:
407
- # tmp_assertion.signature = pre_signature_part(tmp_assertion.id, self.sec.my_cert, 1)
408
- # to_sign.append((class_name(tmp_assertion), tmp_assertion.id))
409
-
410
- # Store which assertion that has been sent to which SP about which
411
- # subject.
412
-
413
- # self.cache.set(assertion.subject.name_id.text,
414
- # sp_entity_id, {"ava": identity, "authn": authn},
415
- # assertion.conditions.not_on_or_after)
416
-
417
425
args ["assertion" ] = assertion
418
426
419
427
if (self .support_AssertionIDRequest () or self .support_AuthnQuery ()):
420
428
self .session_db .store_assertion (assertion , to_sign )
421
429
422
- return self ._response (in_response_to , consumer_url , status , issuer ,
423
- sign_response , to_sign ,sp_entity_id = sp_entity_id , encrypt_assertion = encrypt_assertion ,
424
- encrypt_cert_advice = encrypt_cert_advice ,
425
- encrypt_cert_assertion = encrypt_cert_assertion ,
426
- encrypt_assertion_self_contained = encrypt_assertion_self_contained ,
427
- encrypted_advice_attributes = encrypted_advice_attributes ,sign_assertion = sign_assertion ,
428
- pefim = pefim ,
429
- ** args )
430
+ return self ._response (
431
+ in_response_to , consumer_url , status , issuer ,
432
+ sign_response , to_sign , sp_entity_id = sp_entity_id ,
433
+ encrypt_assertion = encrypt_assertion ,
434
+ encrypt_cert_advice = encrypt_cert_advice ,
435
+ encrypt_cert_assertion = encrypt_cert_assertion ,
436
+ encrypt_assertion_self_contained = encrypt_assertion_self_contained ,
437
+ encrypted_advice_attributes = encrypted_advice_attributes ,
438
+ sign_assertion = sign_assertion ,
439
+ pefim = pefim ,
440
+ ** args )
430
441
431
442
# ------------------------------------------------------------------------
432
443
433
- #noinspection PyUnusedLocal
444
+ # noinspection PyUnusedLocal
434
445
def create_attribute_response (self , identity , in_response_to , destination ,
435
446
sp_entity_id , userid = "" , name_id = None ,
436
447
status = None , issuer = None ,
@@ -500,7 +511,9 @@ def create_authn_response(self, identity, in_response_to, destination,
500
511
sp_entity_id , name_id_policy = None , userid = None ,
501
512
name_id = None , authn = None , issuer = None ,
502
513
sign_response = None , sign_assertion = None ,
503
- encrypt_cert_advice = None , encrypt_cert_assertion = None , encrypt_assertion = None ,
514
+ encrypt_cert_advice = None ,
515
+ encrypt_cert_assertion = None ,
516
+ encrypt_assertion = None ,
504
517
encrypt_assertion_self_contained = True ,
505
518
encrypted_advice_attributes = False , pefim = False ,
506
519
** kwargs ):
@@ -520,13 +533,18 @@ def create_authn_response(self, identity, in_response_to, destination,
520
533
:param sign_assertion: Whether the assertion should be signed or not.
521
534
:param sign_response: Whether the response should be signed or not.
522
535
:param encrypt_assertion: True if assertions should be encrypted.
523
- :param encrypt_assertion_self_contained: True if all encrypted assertions should have alla namespaces
536
+ :param encrypt_assertion_self_contained: True if all encrypted
537
+ assertions should have alla namespaces
524
538
selfcontained.
525
- :param encrypted_advice_attributes: True if assertions in the advice element should be encrypted.
526
- :param encrypt_cert_advice: Certificate to be used for encryption of assertions in the advice element.
527
- :param encrypt_cert_assertion: Certificate to be used for encryption of assertions.
539
+ :param encrypted_advice_attributes: True if assertions in the advice
540
+ element should be encrypted.
541
+ :param encrypt_cert_advice: Certificate to be used for encryption of
542
+ assertions in the advice element.
543
+ :param encrypt_cert_assertion: Certificate to be used for encryption
544
+ of assertions.
528
545
:param sign_assertion: True if assertions should be signed.
529
- :param pefim: True if a response according to the PEFIM profile should be created.
546
+ :param pefim: True if a response according to the PEFIM profile
547
+ should be created.
530
548
:return: A response instance
531
549
"""
532
550
@@ -555,34 +573,43 @@ def create_authn_response(self, identity, in_response_to, destination,
555
573
if encrypt_assertion is None :
556
574
encrypt_assertion = False
557
575
558
-
559
576
if encrypt_assertion_self_contained is None :
560
- encrypt_assertion_self_contained = self .config .getattr ("encrypt_assertion_self_contained" , "idp" )
577
+ encrypt_assertion_self_contained = self .config .getattr (
578
+ "encrypt_assertion_self_contained" , "idp" )
561
579
if encrypt_assertion_self_contained is None :
562
580
encrypt_assertion_self_contained = True
563
581
564
582
if encrypted_advice_attributes is None :
565
- encrypted_advice_attributes = self .config .getattr ("encrypted_advice_attributes" , "idp" )
583
+ encrypted_advice_attributes = self .config .getattr (
584
+ "encrypted_advice_attributes" , "idp" )
566
585
if encrypted_advice_attributes is None :
567
586
encrypted_advice_attributes = False
568
587
569
588
if encrypted_advice_attributes or pefim :
570
- verify_encrypt_cert = self .config .getattr ("verify_encrypt_cert_advice" , "idp" )
589
+ verify_encrypt_cert = self .config .getattr (
590
+ "verify_encrypt_cert_advice" , "idp" )
571
591
if verify_encrypt_cert is not None :
572
592
if encrypt_cert_advice is None :
573
- raise CertificateError ("No SPCertEncType certificate for encryption contained in authentication "
574
- "request." )
593
+ raise CertificateError (
594
+ "No SPCertEncType certificate for encryption "
595
+ "contained in authentication "
596
+ "request." )
575
597
if not verify_encrypt_cert (encrypt_cert_advice ):
576
- raise CertificateError ("Invalid certificate for encryption!" )
598
+ raise CertificateError (
599
+ "Invalid certificate for encryption!" )
577
600
578
601
if encrypt_assertion :
579
- verify_encrypt_cert = self .config .getattr ("verify_encrypt_cert_assertion" , "idp" )
602
+ verify_encrypt_cert = self .config .getattr (
603
+ "verify_encrypt_cert_assertion" , "idp" )
580
604
if verify_encrypt_cert is not None :
581
605
if encrypt_cert_assertion is None :
582
- raise CertificateError ("No SPCertEncType certificate for encryption contained in authentication "
583
- "request." )
606
+ raise CertificateError (
607
+ "No SPCertEncType certificate for encryption "
608
+ "contained in authentication "
609
+ "request." )
584
610
if not verify_encrypt_cert (encrypt_cert_assertion ):
585
- raise CertificateError ("Invalid certificate for encryption!" )
611
+ raise CertificateError (
612
+ "Invalid certificate for encryption!" )
586
613
587
614
if not name_id :
588
615
try :
@@ -624,13 +651,17 @@ def create_authn_response(self, identity, in_response_to, destination,
624
651
625
652
try :
626
653
_authn = authn
627
- if (sign_assertion or sign_response ) and self .sec .cert_handler .generate_cert ():
654
+ if (
655
+ sign_assertion or sign_response ) and \
656
+ self .sec .cert_handler .generate_cert ():
628
657
with self .lock :
629
658
self .sec .cert_handler .update_cert (True )
630
- return self ._authn_response (in_response_to , # in_response_to
659
+ return self ._authn_response (in_response_to ,
660
+ # in_response_to
631
661
destination , # consumer_url
632
662
sp_entity_id , # sp_entity_id
633
- identity , # identity as dictionary
663
+ identity ,
664
+ # identity as dictionary
634
665
name_id ,
635
666
authn = _authn ,
636
667
issuer = issuer ,
@@ -679,7 +710,7 @@ def create_authn_request_response(self, identity, in_response_to,
679
710
sign_response , sign_assertion ,
680
711
authn_decl = authn_decl )
681
712
682
- #noinspection PyUnusedLocal
713
+ # noinspection PyUnusedLocal
683
714
def create_assertion_id_request_response (self , assertion_id , sign = False ,
684
715
** kwargs ):
685
716
"""
@@ -703,7 +734,7 @@ def create_assertion_id_request_response(self, assertion_id, sign=False,
703
734
else :
704
735
return assertion
705
736
706
- #noinspection PyUnusedLocal
737
+ # noinspection PyUnusedLocal
707
738
def create_name_id_mapping_response (self , name_id = None , encrypted_id = None ,
708
739
in_response_to = None ,
709
740
issuer = None , sign_response = False ,
0 commit comments