summaryrefslogtreecommitdiff
path: root/src/saml2/server.py
blob: 9e34cce283471f260006d42359ae0690dff27c8b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

"""Contains classes and functions that a SAML2.0 Identity provider (IdP)
or attribute authority (AA) may use to conclude its tasks.
"""
import logging
import os

import importlib
import dbm
import shelve
import six
import threading

import saml2.cryptography.symmetric
from saml2 import saml
from saml2 import element_to_extension_element
from saml2 import class_name
from saml2 import BINDING_HTTP_REDIRECT
from saml2.argtree import add_path, is_set

from saml2.entity import Entity
from saml2.eptid import Eptid
from saml2.eptid import EptidShelve
from saml2.samlp import NameIDMappingResponse
from saml2.sdb import SessionStorage
from saml2.schema import soapenv

from saml2.request import AuthnRequest
from saml2.request import AssertionIDRequest
from saml2.request import AttributeQuery
from saml2.request import NameIDMappingRequest
from saml2.request import AuthzDecisionQuery
from saml2.request import AuthnQuery

from saml2.s_utils import MissingValue
from saml2.s_utils import rndstr
from saml2.s_utils import Unknown

from saml2.sigver import pre_signature_part
from saml2.sigver import signed_instance_factory
from saml2.sigver import CertificateError

from saml2.assertion import Assertion
from saml2.assertion import Policy
from saml2.assertion import restriction_from_attribute_spec
from saml2.assertion import filter_attribute_value_assertions

from saml2.ident import IdentDB, decode
from saml2.profile import ecp

logger = logging.getLogger(__name__)

AUTHN_DICT_MAP = {
    "decl": "authn_decl",
    "authn_auth": "authn_auth",
    "class_ref": "authn_class",
    "authn_instant": "authn_instant",
    "subject_locality": "subject_locality"
}


def _shelve_compat(name, *args, **kwargs):
    try:
        return shelve.open(name, *args, **kwargs)
    except dbm.error[0]:
        # Python 3 whichdb needs to try .db to determine type
        if name.endswith('.db'):
            name = name.rsplit('.db', 1)[0]
            return shelve.open(name, *args, **kwargs)
        else:
            raise


class Server(Entity):
    """ A class that does things that IdPs or AAs do """

    def __init__(
        self,
        config_file="",
        config=None,
        cache=None,
        stype="idp",
        symkey="",
        msg_cb=None,
    ):
        Entity.__init__(self, stype, config, config_file, msg_cb=msg_cb)
        self.eptid = None
        self.init_config(stype)
        self.cache = cache
        self.ticket = {}
        self.session_db = self.choose_session_storage()
        if symkey:
            self.symkey = symkey.encode()
        else:
            self.symkey = saml2.cryptography.symmetric.Default.generate_key()
        self.seed = rndstr()
        self.lock = threading.Lock()

    def getvalid_certificate_str(self):
        if self.sec.cert_handler is not None:
            return self.sec.cert_handler._last_validated_cert
        return None

    def support_AssertionIDRequest(self):
        return True

    def support_AuthnQuery(self):
        return True

    def choose_session_storage(self):
        _spec = self.config.getattr("session_storage", "idp")
        if not _spec:
            return SessionStorage()
        elif isinstance(_spec, six.string_types):
            if _spec.lower() == "memory":
                return SessionStorage()
        else:  # Should be tuple
            typ, data = _spec
            if typ.lower() == "mongodb":
                from saml2.mongo_store import SessionStorageMDB
                return SessionStorageMDB(database=data, collection="session")

        raise NotImplementedError("No such storage type implemented")

    def init_config(self, stype="idp"):
        """ Remaining init of the server configuration

        :param stype: The type of Server ("idp"/"aa")
        """
        if stype == "aa":
            return

        # subject information is stored in a database
        # default database is in memory which is OK in some setups
        dbspec = self.config.getattr("subject_data", "idp")
        idb = None
        typ = ""
        if not dbspec:
            idb = {}
        elif isinstance(dbspec, six.string_types):
            idb = _shelve_compat(dbspec, writeback=True, protocol=2)
        else:  # database spec is a a 2-tuple (type, address)
            # print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
            (typ, addr) = dbspec
            if typ == "shelve":
                idb = _shelve_compat(addr, writeback=True, protocol=2)
            elif typ == "memcached":
                import memcache
                idb = memcache.Client(addr)
            elif typ == "dict":  # in-memory dictionary
                idb = {}
            elif typ == "mongodb":
                from saml2.mongo_store import IdentMDB
                self.ident = IdentMDB(database=addr, collection="ident")
            elif typ == "identdb":
                mod, clas = addr.rsplit('.', 1)
                mod = importlib.import_module(mod)
                self.ident = getattr(mod, clas)()

        if typ == "mongodb" or typ == "identdb":
            pass
        elif idb is not None:
            self.ident = IdentDB(idb)
        elif dbspec:
            raise Exception("Couldn't open identity database: %s" %
                            (dbspec,))

        try:
            _domain = self.config.getattr("domain", "idp")
            if _domain:
                self.ident.domain = _domain

            self.ident.name_qualifier = self.config.entityid

            dbspec = self.config.getattr("edu_person_targeted_id", "idp")
            if not dbspec:
                pass
            else:
                typ = dbspec[0]
                addr = dbspec[1]
                secret = dbspec[2]
                if typ == "shelve":
                    self.eptid = EptidShelve(secret, addr)
                elif typ == "mongodb":
                    from saml2.mongo_store import EptidMDB
                    self.eptid = EptidMDB(secret, database=addr,
                                          collection="eptid")
                else:
                    self.eptid = Eptid(secret)
        except Exception:
            self.ident.close()
            raise

    def wants(self, sp_entity_id, index=None):
        """ Returns what attributes the SP requires and which are optional
        if any such demands are registered in the Metadata.

        :param sp_entity_id: The entity id of the SP
        :param index: which of the attribute consumer services its all about
            if index == None then all attribute consumer services are clumped
            together.
        :return: 2-tuple, list of required and list of optional attributes
        """
        return self.metadata.attribute_requirement(sp_entity_id, index)

    def verify_assertion_consumer_service(self, request):
        _acs = request.assertion_consumer_service_url
        _aci = request.assertion_consumer_service_index
        _binding = request.protocol_binding
        _eid = request.issuer.text
        if _acs:
            # look up acs in for that binding in the metadata given the issuer
            # Assuming the format is entity
            for acs in self.metadata.assertion_consumer_service(_eid, _binding):
                if _acs == acs.text:
                    return True
        elif _aci:
            for acs in self.metadata.assertion_consumer_service(_eid, _binding):
                if _aci == acs.index:
                    return True

        return False

    # -------------------------------------------------------------------------

    def parse_authn_request(self, enc_request, binding=BINDING_HTTP_REDIRECT):
        """Parse a Authentication Request

        :param enc_request: The request in its transport format
        :param binding: Which binding that was used to transport the message
            to this entity.
        :return: A request instance
        """

        return self._parse_request(enc_request, AuthnRequest,
                                   "single_sign_on_service", binding)

    def parse_attribute_query(self, xml_string, binding):
        """ Parse an attribute query

        :param xml_string: The Attribute Query as an XML string
        :param binding: Which binding that was used for the request
        :return: A query instance
        """

        return self._parse_request(xml_string, AttributeQuery,
                                   "attribute_service", binding)

    def parse_authz_decision_query(self, xml_string, binding):
        """ Parse an authorization decision query

        :param xml_string: The Authz decision Query as an XML string
        :param binding: Which binding that was used when receiving this query
        :return: Query instance
        """

        return self._parse_request(xml_string, AuthzDecisionQuery,
                                   "authz_service", binding)

    def parse_assertion_id_request(self, xml_string, binding):
        """ Parse an assertion id query

        :param xml_string: The AssertionIDRequest as an XML string
        :param binding: Which binding that was used when receiving this request
        :return: Query instance
        """

        return self._parse_request(xml_string, AssertionIDRequest,
                                   "assertion_id_request_service", binding)

    def parse_authn_query(self, xml_string, binding):
        """ Parse an authn query

        :param xml_string: The AuthnQuery as an XML string
        :param binding: Which binding that was used when receiving this query
        :return: Query instance
        """

        return self._parse_request(xml_string, AuthnQuery,
                                   "authn_query_service", binding)

    def parse_name_id_mapping_request(self, xml_string, binding):
        """ Parse a nameid mapping request

        :param xml_string: The NameIDMappingRequest as an XML string
        :param binding: Which binding that was used when receiving this request
        :return: Query instance
        """

        return self._parse_request(xml_string, NameIDMappingRequest,
                                   "name_id_mapping_service", binding)

    @staticmethod
    def update_farg(in_response_to, consumer_url, farg=None):
        if not farg:
            farg = add_path(
                {},
                ['assertion', 'subject', 'subject_confirmation', 'method',
                 saml.SCM_BEARER])
            add_path(
                farg['assertion']['subject']['subject_confirmation'],
                ['subject_confirmation_data', 'in_response_to', in_response_to])
            add_path(
                farg['assertion']['subject']['subject_confirmation'],
                ['subject_confirmation_data', 'recipient', consumer_url])
        else:
            if not is_set(farg,
                          ['assertion', 'subject', 'subject_confirmation',
                           'method']):
                add_path(farg,
                         ['assertion', 'subject', 'subject_confirmation',
                          'method', saml.SCM_BEARER])
            if not is_set(farg,
                          ['assertion', 'subject', 'subject_confirmation',
                           'subject_confirmation_data', 'in_response_to']):
                add_path(farg,
                         ['assertion', 'subject', 'subject_confirmation',
                          'subject_confirmation_data', 'in_response_to',
                          in_response_to])
            if not is_set(farg, ['assertion', 'subject', 'subject_confirmation',
                                 'subject_confirmation_data', 'recipient']):
                add_path(farg,
                         ['assertion', 'subject', 'subject_confirmation',
                          'subject_confirmation_data', 'recipient',
                          consumer_url])
        return farg

    def setup_assertion(
        self,
        authn,
        sp_entity_id,
        in_response_to,
        consumer_url,
        name_id,
        policy,
        _issuer,
        authn_statement,
        identity,
        best_effort,
        sign_response,
        farg=None,
        session_not_on_or_after=None,
        sign_alg=None,
        digest_alg=None,
        **kwargs,
    ):
        """
        Construct and return the Assertion

        :param authn: Authentication information
        :param sp_entity_id:
        :param in_response_to: The ID of the request this is an answer to
        :param consumer_url: The recipient of the assertion
        :param name_id: The NameID of the subject
        :param policy: Assertion policies
        :param _issuer: Issuer of the statement
        :param authn_statement: An AuthnStatement instance
        :param identity: Identity information about the Subject
        :param best_effort: Even if not the SPs demands can be met send a
            response.
        :param sign_response: Sign the response, only applicable if
            ErrorResponse
        :param kwargs: Extra keyword arguments
        :return: An Assertion instance
        """

        ast = Assertion(identity)
        ast.acs = self.config.getattr("attribute_converters")
        if policy is None:
            policy = Policy(mds=self.metadata)
        try:
            ast.apply_policy(sp_entity_id, policy)
        except MissingValue as exc:
            if not best_effort:
                response = self.create_error_response(
                    in_response_to,
                    destination=consumer_url,
                    info=exc,
                    sign=sign_response,
                    sign_alg=sign_alg,
                    digest_alg=digest_alg,
                )
                return str(response).split("\n")

        farg = self.update_farg(in_response_to, consumer_url, farg)

        if authn:  # expected to be a dictionary
            # Would like to use dict comprehension but ...
            authn_args = dict(
                [(AUTHN_DICT_MAP[k], v) for k, v in authn.items() if
                 k in AUTHN_DICT_MAP])
            authn_args.update(kwargs)

            assertion = ast.construct(
                sp_entity_id, self.config.attribute_converters, policy,
                issuer=_issuer, farg=farg['assertion'], name_id=name_id,
                session_not_on_or_after=session_not_on_or_after,
                **authn_args)

        elif authn_statement:  # Got a complete AuthnStatement
            assertion = ast.construct(
                sp_entity_id, self.config.attribute_converters, policy,
                issuer=_issuer, authn_statem=authn_statement,
                farg=farg['assertion'], name_id=name_id,
                **kwargs)
        else:
            assertion = ast.construct(
                sp_entity_id, self.config.attribute_converters, policy,
                issuer=_issuer, farg=farg['assertion'], name_id=name_id,
                session_not_on_or_after=session_not_on_or_after,
                **kwargs)
        return assertion

    # XXX DONE calls pre_signature_part
    # XXX calls _response
    def _authn_response(
        self,
        in_response_to,
        consumer_url,
        sp_entity_id,
        identity=None,
        name_id=None,
        status=None,
        authn=None,
        issuer=None,
        policy=None,
        sign_assertion=None,
        sign_response=None,
        best_effort=False,
        encrypt_assertion=False,
        encrypt_cert_advice=None,
        encrypt_cert_assertion=None,
        authn_statement=None,
        encrypt_assertion_self_contained=False,
        encrypted_advice_attributes=False,
        pefim=False,
        sign_alg=None,
        digest_alg=None,
        farg=None,
        session_not_on_or_after=None,
    ):
        """ Create a response. A layer of indirection.

        :param in_response_to: The session identifier of the request
        :param consumer_url: The URL which should receive the response
        :param sp_entity_id: The entity identifier of the SP
        :param identity: A dictionary with attributes and values that are
            expected to be the bases for the assertion in the response.
        :param name_id: The identifier of the subject
        :param status: The status of the response
        :param authn: A dictionary containing information about the
            authn context.
        :param issuer: The issuer of the response
        :param policy:
        :param sign_assertion: Whether the assertion should be signed or not
        :param sign_response: Whether the response should be signed or not
        :param best_effort: Even if not the SPs demands can be met send a
            response.
        :param encrypt_assertion: True if assertions should be encrypted.
        :param encrypt_assertion_self_contained: True if all encrypted
        assertions should have alla namespaces
        selfcontained.
        :param encrypted_advice_attributes: True if assertions in the advice
        element should be encrypted.
        :param encrypt_cert_advice: Certificate to be used for encryption of
        assertions in the advice element.
        :param encrypt_cert_assertion: Certificate to be used for encryption
        of assertions.
        :param authn_statement: Authentication statement.
        :param pefim: True if a response according to the PEFIM profile
        should be created.
        :param farg: Argument to pass on to the assertion constructor
        :return: A response instance
        """

        if farg is None:
            assertion_args = {}

        # if identity:
        _issuer = self._issuer(issuer)

        # if encrypt_assertion and show_nameid:
        #    tmp_name_id = name_id
        #    name_id = None
        #    name_id = None
        #    tmp_authn = authn
        #    authn = None
        #    tmp_authn_statement = authn_statement
        #    authn_statement = None

        if pefim:
            encrypted_advice_attributes = True
            encrypt_assertion_self_contained = True
            assertion_attributes = self.setup_assertion(
                None, sp_entity_id, None, None, None, policy, None, None,
                identity, best_effort, sign_response, farg=farg)
            assertion = self.setup_assertion(
                authn, sp_entity_id, in_response_to, consumer_url, name_id,
                policy, _issuer, authn_statement, [], True, sign_response,
                farg=farg, session_not_on_or_after=session_not_on_or_after)
            assertion.advice = saml.Advice()

            # assertion.advice.assertion_id_ref.append(saml.AssertionIDRef())
            # assertion.advice.assertion_uri_ref.append(saml.AssertionURIRef())
            assertion.advice.assertion.append(assertion_attributes)
        else:
            assertion = self.setup_assertion(
                authn, sp_entity_id, in_response_to, consumer_url, name_id,
                policy, _issuer, authn_statement, identity, True,
                sign_response, farg=farg,
                session_not_on_or_after=session_not_on_or_after)

        to_sign = []
        if not encrypt_assertion:
            if sign_assertion:
                # XXX self.signing_algorithm self.digest_algorithm defined by entity
                # XXX this should be handled through entity.py
                # XXX sig/digest-allowed should be configurable
                sign_alg = sign_alg or self.signing_algorithm
                digest_alg = digest_alg or self.digest_algorithm

                assertion.signature = pre_signature_part(
                    assertion.id,
                    self.sec.my_cert,
                    2,
                    sign_alg=sign_alg,
                    digest_alg=digest_alg,
                )
                to_sign.append((class_name(assertion), assertion.id))

        if (self.support_AssertionIDRequest() or self.support_AuthnQuery()):
            self.session_db.store_assertion(assertion, to_sign)

        return self._response(
            in_response_to,
            consumer_url,
            status,
            issuer,
            sign_response,
            to_sign,
            sp_entity_id=sp_entity_id,
            encrypt_assertion=encrypt_assertion,
            encrypt_cert_advice=encrypt_cert_advice,
            encrypt_cert_assertion=encrypt_cert_assertion,
            encrypt_assertion_self_contained=encrypt_assertion_self_contained,
            encrypted_advice_attributes=encrypted_advice_attributes,
            sign_assertion=sign_assertion,
            pefim=pefim,
            sign_alg=sign_alg,
            digest_alg=digest_alg,
            assertion=assertion,
        )

    # ------------------------------------------------------------------------

    # XXX DONE idp create > _response
    def create_attribute_response(
        self,
        identity,
        in_response_to,
        destination,
        sp_entity_id,
        userid="",
        name_id=None,
        status=None,
        issuer=None,
        sign_assertion=None,
        sign_response=None,
        attributes=None,
        sign_alg=None,
        digest_alg=None,
        farg=None,
        **kwargs,
    ):
        """ Create an attribute assertion response.

        :param identity: A dictionary with attributes and values that are
            expected to be the bases for the assertion in the response.
        :param in_response_to: The session identifier of the request
        :param destination: The URL which should receive the response
        :param sp_entity_id: The entity identifier of the SP
        :param userid: A identifier of the user
        :param name_id: The identifier of the subject
        :param status: The status of the response
        :param issuer: The issuer of the response
        :param sign_assertion: Whether the assertion should be signed or not
        :param sign_response: Whether the whole response should be signed
        :param attributes:
        :param kwargs: To catch extra keyword arguments
        :return: A response instance
        """

        policy = self.config.getattr("policy", "aa")

        if not name_id and userid:
            try:
                name_id = self.ident.construct_nameid(userid, policy, sp_entity_id)
                logger.warning("Unspecified NameID format")
            except Exception:
                pass

        to_sign = []

        if identity:
            farg = self.update_farg(in_response_to, sp_entity_id, farg=farg)

            _issuer = self._issuer(issuer)
            ast = Assertion(identity)
            if policy:
                ast.apply_policy(sp_entity_id, policy)
            else:
                policy = Policy(mds=self.metadata)

            if attributes:
                restr = restriction_from_attribute_spec(attributes)
                ast = filter_attribute_value_assertions(ast)

            assertion = ast.construct(
                sp_entity_id, self.config.attribute_converters, policy,
                issuer=_issuer, name_id=name_id,
                farg=farg['assertion'])

        return self._response(
            in_response_to,
            destination,
            status,
            issuer,
            sign_response,
            to_sign,
            sign_assertion=sign_assertion,
            sign_alg=sign_alg,
            digest_alg=digest_alg,
            assertion=assertion,
            sp_entity_id=sp_entity_id,
            **kwargs,
        )

    def gather_authn_response_args(
        self, sp_entity_id, name_id_policy, userid, **kwargs
    ):
        kwargs["policy"] = kwargs.get("release_policy")

        # collect args and return them
        args = {}

        # XXX will be passed to _authn_response
        param_defaults = {
            'policy': None,
            'best_effort': False,
            'sign_assertion': False,
            'sign_response': False,
            'encrypt_assertion': False,
            'encrypt_assertion_self_contained': True,
            'encrypted_advice_attributes': False,
            'encrypt_cert_advice': None,
            'encrypt_cert_assertion': None,
            # need to be named sign_alg and digest_alg
        }
        for param, val_default in param_defaults.items():
            val_kw = kwargs.get(param)
            val_config = self.config.getattr(param, "idp")
            args[param] = (
                val_kw
                if val_kw is not None
                else val_config
                if val_config is not None
                else val_default
            )

        for arg, attr, eca, pefim in [
            ('encrypted_advice_attributes', 'verify_encrypt_cert_advice',
             'encrypt_cert_advice', kwargs["pefim"]),
            ('encrypt_assertion', 'verify_encrypt_cert_assertion',
             'encrypt_cert_assertion', False)]:

            if args[arg] or pefim:
                _enc_cert = self.config.getattr(attr, "idp")

                if _enc_cert is not None:
                    if kwargs[eca] is None:
                        raise CertificateError(
                            "No SPCertEncType certificate for encryption "
                            "contained in authentication "
                            "request.")
                    if not _enc_cert(kwargs[eca]):
                        raise CertificateError(
                            "Invalid certificate for encryption!")

        if 'name_id' not in kwargs or not kwargs['name_id']:
            nid_formats = []
            for _sp in self.metadata[sp_entity_id]["spsso_descriptor"]:
                if "name_id_format" in _sp:
                    nid_formats.extend([n["text"] for n in
                                        _sp["name_id_format"]])
            try:
                snq = name_id_policy.sp_name_qualifier
            except AttributeError:
                snq = sp_entity_id

            if not snq:
                snq = sp_entity_id

            kwa = {"sp_name_qualifier": snq}

            try:
                kwa["format"] = name_id_policy.format
            except AttributeError:
                pass

            _nids = self.ident.find_nameid(userid, **kwa)
            # either none or one
            if _nids:
                args['name_id'] = _nids[0]
            else:
                args['name_id'] = self.ident.construct_nameid(
                    userid, args['policy'], sp_entity_id, name_id_policy)
                logger.debug("construct_nameid: %s => %s", userid,
                             args['name_id'])
        else:
            args['name_id'] = kwargs['name_id']

        for param in ['status', 'farg']:
            try:
                args[param] = kwargs[param]
            except KeyError:
                pass

        return args

    # XXX DONE idp create > _authn_response > _response
    def create_authn_response(
        self,
        identity,
        in_response_to,
        destination,
        sp_entity_id,
        name_id_policy=None,
        userid=None,
        name_id=None,
        authn=None,
        issuer=None,
        sign_response=None,
        sign_assertion=None,
        encrypt_cert_advice=None,
        encrypt_cert_assertion=None,
        encrypt_assertion=None,
        encrypt_assertion_self_contained=True,
        encrypted_advice_attributes=False,
        pefim=False,
        sign_alg=None,
        digest_alg=None,
        session_not_on_or_after=None,
        **kwargs,
    ):
        """ Constructs an AuthenticationResponse

        :param identity: Information about an user
        :param in_response_to: The identifier of the authentication request
            this response is an answer to.
        :param destination: Where the response should be sent
        :param sp_entity_id: The entity identifier of the Service Provider
        :param name_id_policy: How the NameID should be constructed
        :param userid: The subject identifier
        :param name_id: The identifier of the subject. A saml.NameID instance.
        :param authn: Dictionary with information about the authentication
            context
        :param issuer: Issuer of the response
        :param sign_assertion: Whether the assertion should be signed or not.
        :param sign_response: Whether the response should be signed or not.
        :param encrypt_assertion: True if assertions should be encrypted.
        :param encrypt_assertion_self_contained: True if all encrypted
        assertions should have alla namespaces
        selfcontained.
        :param encrypted_advice_attributes: True if assertions in the advice
        element should be encrypted.
        :param encrypt_cert_advice: Certificate to be used for encryption of
        assertions in the advice element.
        :param encrypt_cert_assertion: Certificate to be used for encryption
        of assertions.
        :param pefim: True if a response according to the PEFIM profile
        should be created.
        :return: A response instance
        """

        try:
            args = self.gather_authn_response_args(
                sp_entity_id,
                name_id_policy=name_id_policy,
                userid=userid,
                name_id=name_id,
                sign_response=sign_response,
                sign_assertion=sign_assertion,
                encrypt_cert_advice=encrypt_cert_advice,
                encrypt_cert_assertion=encrypt_cert_assertion,
                encrypt_assertion=encrypt_assertion,
                encrypt_assertion_self_contained=encrypt_assertion_self_contained,
                encrypted_advice_attributes=encrypted_advice_attributes,
                pefim=pefim,
                **kwargs,
            )
        except IOError as exc:
            response = self.create_error_response(
                in_response_to,
                destination=destination,
                info=exc,
                sign=sign_response,
                sign_alg=sign_alg,
                digest_alg=digest_alg,
            )
            return str(response).split("\n")

        try:
            _authn = authn
            return self._authn_response(
                in_response_to,
                destination,
                sp_entity_id,
                identity,
                authn=_authn,
                issuer=issuer,
                pefim=pefim,
                sign_alg=sign_alg,
                digest_alg=digest_alg,
                session_not_on_or_after=session_not_on_or_after,
                **args,
            )
        except MissingValue as exc:
            return self.create_error_response(
                in_response_to,
                destination=destination,
                info=exc,
                sign=sign_response,
                sign_alg=sign_alg,
                digest_alg=digest_alg,
            )

    # XXX DONE idp create > create_authn_response > _authn_response > _response
    def create_authn_request_response(
        self,
        identity,
        in_response_to,
        destination,
        sp_entity_id,
        name_id_policy=None,
        userid=None,
        name_id=None,
        authn=None,
        authn_decl=None,
        issuer=None,
        sign_response=None,
        sign_assertion=None,
        session_not_on_or_after=None,
        sign_alg=None,
        digest_alg=None,
        **kwargs,
    ):
        return self.create_authn_response(
            identity,
            in_response_to,
            destination,
            sp_entity_id,
            name_id_policy,
            userid,
            name_id,
            authn,
            issuer,
            sign_response,
            sign_assertion,
            authn_decl=authn_decl,
            session_not_on_or_after=session_not_on_or_after,
            sign_alg=sign_alg,
            digest_alg=digest_alg,
        )

    # XXX DONE calls pre_signature_part
    # XXX DONE idp create > [...]
    def create_assertion_id_request_response(
        self, assertion_id, sign=None, sign_alg=None, digest_alg=None, **kwargs
    ):
        try:
            (assertion, to_sign) = self.session_db.get_assertion(assertion_id)
        except KeyError:
            raise Unknown

        if to_sign:
            if assertion.signature is None:
                # XXX self.signing_algorithm self.digest_algorithm defined by entity
                # XXX this should be handled through entity.py
                # XXX sig/digest-allowed should be configurable
                sign_alg = sign_alg or self.signing_algorithm
                digest_alg = digest_alg or self.digest_algorithm

                assertion.signature = pre_signature_part(
                    assertion.id,
                    self.sec.my_cert,
                    1,
                    sign_alg=sign_alg,
                    digest_alg=digest_alg,
                )
            return signed_instance_factory(assertion, self.sec, to_sign)
        else:
            return assertion

    # XXX calls self.sign without ensuring sign
    # XXX calls self.sign => should it call _message (which calls self.sign)?
    # XXX idp create > NameIDMappingResponse & sign?
    def create_name_id_mapping_response(
        self,
        name_id=None,
        encrypted_id=None,
        in_response_to=None,
        issuer=None,
        sign_response=None,
        status=None,
        sign_alg=None,
        digest_alg=None,
        **kwargs,
    ):
        """
        protocol for mapping a principal's name identifier into a
        different name identifier for the same principal.
        Done over soap.

        :param name_id:
        :param encrypted_id:
        :param in_response_to:
        :param issuer:
        :param sign_response:
        :param status:
        :return:
        """
        # Done over SOAP

        ms_args = self.message_args()

        _resp = NameIDMappingResponse(
            name_id, encrypted_id, in_response_to=in_response_to, **ms_args
        )

        if sign_response:
            return self.sign(_resp, sign_alg=sign_alg, digest_alg=digest_alg)
        else:
            logger.info("Message: %s", _resp)
            return _resp

    # XXX DONE idp create > _response
    def create_authn_query_response(
        self,
        subject,
        session_index=None,
        requested_context=None,
        in_response_to=None,
        issuer=None,
        sign_response=None,
        status=None,
        sign_alg=None,
        digest_alg=None,
        **kwargs,
    ):
        """
        A successful <Response> will contain one or more assertions containing
        authentication statements.

        :return:
        """

        margs = self.message_args()
        asserts = [
            saml.Assertion(authn_statement=statement, subject=subject, **margs)
            for statement in self.session_db.get_authn_statements(
                subject.name_id, session_index, requested_context
            )
        ]

        if asserts:
            args = {"assertion": asserts}
        else:
            args = {}

        return self._response(
            in_response_to,
            "",
            status,
            issuer,
            sign_response,
            to_sign=[],
            sign_alg=sign_alg,
            digest_alg=digest_alg,
            **args,
        )

    # ---------

    def parse_ecp_authn_request(self):
        pass

    # XXX DONE idp create > create_authn_response > _authn_response > _response
    def create_ecp_authn_request_response(
        self,
        acs_url,
        identity,
        in_response_to,
        destination,
        sp_entity_id,
        name_id_policy=None,
        userid=None,
        name_id=None,
        authn=None,
        issuer=None,
        sign_response=None,
        sign_assertion=None,
        sign_alg=None,
        digest_alg=None,
        **kwargs,
    ):

        # ----------------------------------------
        # <ecp:Response
        # ----------------------------------------

        ecp_response = ecp.Response(assertion_consumer_service_url=acs_url)
        header = soapenv.Header()
        header.extension_elements = [element_to_extension_element(ecp_response)]

        # ----------------------------------------
        # <samlp:Response
        # ----------------------------------------

        response = self.create_authn_response(
            identity,
            in_response_to,
            destination,
            sp_entity_id,
            name_id_policy,
            userid,
            name_id,
            authn,
            issuer,
            sign_response,
            sign_assertion,
            sign_alg=sign_alg,
            digest_alg=digest_alg
        )
        body = soapenv.Body()
        body.extension_elements = [element_to_extension_element(response)]

        soap_envelope = soapenv.Envelope(header=header, body=body)

        return str(soap_envelope)

    def close(self):
        self.ident.close()

    def clean_out_user(self, name_id):
        """
        Remove all authentication statements that belongs to a user identified
        by a NameID instance

        :param name_id: NameID instance
        :return: The local identifier for the user
        """

        lid = self.ident.find_local_id(name_id)
        logger.info("Clean out %s", lid)

        # remove the authentications
        try:
            for _nid in [decode(x) for x in self.ident.db[lid].split(" ")]:
                try:
                    self.session_db.remove_authn_statements(_nid)
                except KeyError:
                    pass
        except KeyError:
            pass

        return lid