~ddellav/ubuntu/wily/python-pysaml2/debian-merge

« back to all changes in this revision

Viewing changes to src/saml2/sdb.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-08 16:11:53 UTC
  • Revision ID: package-import@ubuntu.com-20140908161153-vms9r4gu0oz4v4ai
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
 
 
3
from hashlib import sha1
 
4
 
 
5
from saml2.ident import code
 
6
 
 
7
from saml2 import md
 
8
from saml2 import saml
 
9
from saml2.extension import mdui
 
10
from saml2.extension import idpdisc
 
11
from saml2.extension import dri
 
12
from saml2.extension import mdattr
 
13
from saml2.extension import ui
 
14
import xmldsig
 
15
import xmlenc
 
16
 
 
17
 
 
18
ONTS = {
 
19
    saml.NAMESPACE: saml,
 
20
    mdui.NAMESPACE: mdui,
 
21
    mdattr.NAMESPACE: mdattr,
 
22
    dri.NAMESPACE: dri,
 
23
    ui.NAMESPACE: ui,
 
24
    idpdisc.NAMESPACE: idpdisc,
 
25
    md.NAMESPACE: md,
 
26
    xmldsig.NAMESPACE: xmldsig,
 
27
    xmlenc.NAMESPACE: xmlenc
 
28
}
 
29
 
 
30
__author__ = 'rolandh'
 
31
 
 
32
logger = logging.getLogger(__name__)
 
33
 
 
34
 
 
35
def context_match(cfilter, cntx):
 
36
    # TODO
 
37
    return True
 
38
 
 
39
# The key to the stored authn statement is placed encrypted in the cookie
 
40
 
 
41
 
 
42
class SessionStorage(object):
 
43
    """ In memory storage of session information """
 
44
 
 
45
    def __init__(self):
 
46
        self.db = {"assertion": {}, "authn": {}}
 
47
        self.assertion = self.db["assertion"]
 
48
        self.authn = self.db["authn"]
 
49
 
 
50
    def store_assertion(self, assertion, to_sign):
 
51
        self.assertion[assertion.id] = (assertion, to_sign)
 
52
        key = sha1(code(assertion.subject.name_id)).hexdigest()
 
53
        try:
 
54
            self.authn[key].append(assertion.authn_statement)
 
55
        except KeyError:
 
56
            self.authn[key] = [assertion.authn_statement]
 
57
 
 
58
    def get_assertion(self, cid):
 
59
        return self.assertion[cid]
 
60
 
 
61
    def get_authn_statements(self, name_id, session_index=None,
 
62
                             requested_context=None):
 
63
        """
 
64
 
 
65
        :param name_id:
 
66
        :param session_index:
 
67
        :param requested_context:
 
68
        :return:
 
69
        """
 
70
        result = []
 
71
        key = sha1(code(name_id)).hexdigest()
 
72
        try:
 
73
            statements = self.authn[key]
 
74
        except KeyError:
 
75
            logger.info("Unknown subject %s" % name_id)
 
76
            return []
 
77
 
 
78
        for statement in statements:
 
79
            if session_index:
 
80
                if statement.session_index != session_index:
 
81
                    continue
 
82
            if requested_context:
 
83
                if not context_match(requested_context,
 
84
                                     statement[0].authn_context):
 
85
                    continue
 
86
            result.append(statement)
 
87
 
 
88
        return result
 
89
 
 
90
    def remove_authn_statements(self, name_id):
 
91
        logger.debug("remove authn about: %s" % name_id)
 
92
        nkey = sha1(code(name_id)).hexdigest()
 
93
 
 
94
        del self.authn[nkey]