summaryrefslogtreecommitdiff
path: root/tests/test_client.py
blob: d838b1e4d6affc1c63d9ea52e0517876b4a61f63 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from saml2.client import Saml2Client
from saml2 import samlp, client
from saml2 import saml, utils, config

XML_RESPONSE_FILE = "tests/saml_signed.xml"
XML_RESPONSE_FILE2 = "tests/saml2_response.xml"

import os

try:
    XMLSEC_BINARY = "/usr/local/bin/xmlsec1"
    os.stat(XMLSEC_BINARY)
except OSError:
    try:
        XMLSEC_BINARY = "/usr/bin/xmlsec1"
        os.stat(XMLSEC_BINARY)
    except OSError:
        raise
        
def for_me(condition, me ):
    for restriction in condition.audience_restriction:
        audience = restriction.audience
        if audience.text.strip() == me:
            return True

def ava(attribute_statement):
    result = {}
    for attribute in attribute_statement.attribute:
        # Check name_format ??
        name = attribute.name.strip()
        result[name] = []
        for value in attribute.attribute_value:
            result[name].append(value.text.strip())
    return result

        
# def test_parse_3():
#     xml_response = open(XML_RESPONSE_FILE3).read()
#     response = samlp.response_from_string(xml_response)
#     client = Saml2Client({})
#     (ava, name_id, real_uri) = \
#             client.do_response(response, "xenosmilus.umdc.umu.se")
#     print 40*"="
#     print ava
#     print 40*","
#     print name_id
#     assert False

REQ1 = """<?xml version='1.0' encoding='UTF-8'?>
<ns0:AttributeQuery Destination="https://idp.example.com/idp/" ID="1" IssueInstant="%s" Version="2.0" xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol"><ns1:Issuer xmlns:ns1="urn:oasis:names:tc:SAML:2.0:assertion">http://vo.example.com/sp1</ns1:Issuer><ns1:Subject xmlns:ns1="urn:oasis:names:tc:SAML:2.0:assertion"><ns1:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent">E8042FB4-4D5B-48C3-8E14-8EDD852790DD</ns1:NameID></ns1:Subject></ns0:AttributeQuery>"""

class TestClient:
    def setup_class(self):
        conf = config.Config()
        conf.load_file("tests/server.config")
        self.client = Saml2Client({},conf)
    
    def test_verify_1(self):
        xml_response = open(XML_RESPONSE_FILE).read()
        (ava, came_from) = \
            self.client.verify_response(xml_response, 
                                "xenosmilus.umdc.umu.se", 
                                decode=False)
        assert ava == {'__userid': '_cddc88563d433f556d4cc70c3162deabddea3b5019', 
                        'eduPersonAffiliation': ['member', 'student'], 
                        'uid': ['student']}
    
    def test_parse_1(self):
        xml_response = open(XML_RESPONSE_FILE).read()
        response = samlp.response_from_string(xml_response)
        (ava, name_id, real_uri) = \
            self.client.do_response(response, "xenosmilus.umdc.umu.se")
        assert ava == {'eduPersonAffiliation': ['member', 'student'],
                        'uid': ['student']}
        assert name_id == "_cddc88563d433f556d4cc70c3162deabddea3b5019"

    def test_parse_2(self):
        xml_response = open(XML_RESPONSE_FILE2).read()
        response = samlp.response_from_string(xml_response)
        (ava, name_id, real_uri) = \
            self.client.do_response(response, "xenosmilus.umdc.umu.se")
        assert ava == {'uid': ['andreas'], 
                        'mobile': ['+4741107700'], 
                        'edupersonnickname': ['erlang'], 
                        'o': ['Feide RnD'], 
                        'edupersonentitlement': [
                                'urn:mace:feide.no:entitlement:test'], 
                        'edupersonaffiliation': ['employee'], 
                        'eduPersonPrincipalName': ['andreas@rnd.feide.no'], 
                        'sn': ['Solberg'], 
                        'mail': ['andreas@uninett.no'], 
                        'ou': ['Guests'], 
                        'cn': ['Andreas Solberg']}
        assert name_id == "_242f88493449e639aab95dd9b92b1d04234ab84fd8"

    def test_create_attribute_query1(self):
        req = self.client.create_attribute_query("1", 
            "E8042FB4-4D5B-48C3-8E14-8EDD852790DD",
            "http://vo.example.com/sp1",
            "https://idp.example.com/idp/" )
        str = "%s" % req.to_string()
        print str
        assert str == REQ1 % req.issue_instant
        assert req.destination == "https://idp.example.com/idp/"
        assert req.id == "1"
        assert req.version == "2.0"
        subject = req.subject
        name_id = subject.name_id
        assert name_id.format == saml.NAMEID_FORMAT_PERSISTENT
        assert name_id.text == "E8042FB4-4D5B-48C3-8E14-8EDD852790DD"
        issuer = req.issuer
        assert issuer.text == "http://vo.example.com/sp1"
        
    def test_create_attribute_query2(self):
        req = self.client.create_attribute_query("1", 
            "E8042FB4-4D5B-48C3-8E14-8EDD852790DD", 
            "http://vo.example.com/sp1",
            "https://idp.example.com/idp/",
            attribute={
                ("urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
                "urn:oid:2.5.4.42","givenName"):None,
                ("urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
                "urn:oid:2.5.4.4","surname"):None,
                ("urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
                "urn:oid:1.2.840.113549.1.9.1"):None,
                })
                
        print req.to_string()
        assert req.destination == "https://idp.example.com/idp/"
        assert req.id == "1"
        assert req.version == "2.0"
        subject = req.subject
        name_id = subject.name_id
        assert name_id.format == saml.NAMEID_FORMAT_PERSISTENT
        assert name_id.text == "E8042FB4-4D5B-48C3-8E14-8EDD852790DD"
        assert len(req.attribute) == 3
        # one is givenName
        seen = []
        for attribute in req.attribute:
            if attribute.name == "urn:oid:2.5.4.42":
                assert attribute.name_format == saml.NAME_FORMAT_URI
                assert attribute.friendly_name == "givenName"
                seen.append("givenName")
            elif attribute.name == "urn:oid:2.5.4.4":
                assert attribute.name_format == saml.NAME_FORMAT_URI
                assert attribute.friendly_name == "surname"
                seen.append("surname")
            elif attribute.name == "urn:oid:1.2.840.113549.1.9.1":
                assert attribute.name_format == saml.NAME_FORMAT_URI
                if getattr(attribute,"friendly_name"):
                    assert False
                seen.append("email")
        assert set(seen) == set(["givenName","surname","email"])
        
    def test_create_attribute_query_3(self):
        req = self.client.create_attribute_query("1",
                "_e7b68a04488f715cda642fbdd90099f5", 
                "urn:mace:umu.se:saml/rolandsp",
                "https://aai-demo-idp.switch.ch/idp/shibboleth",
                format=saml.NAMEID_FORMAT_TRANSIENT )
                
        assert isinstance(req, samlp.AttributeQuery)
        assert req.destination == "https://aai-demo-idp.switch.ch/idp/shibboleth"
        assert req.id == "1"
        assert req.version == "2.0"
        assert req.issue_instant
        assert req.issuer.text == "urn:mace:umu.se:saml/rolandsp"
        nameid = req.subject.name_id
        assert nameid.format == saml.NAMEID_FORMAT_TRANSIENT
        assert nameid.text == "_e7b68a04488f715cda642fbdd90099f5"

    def test_idp_entry(self):
        idp_entry = utils.make_instance( samlp.IDPEntry,
                            self.client.idp_entry(name="Umeå Universitet",
                            location="https://idp.umu.se/"))
        
        assert idp_entry.name == "Umeå Universitet"
        assert idp_entry.loc == "https://idp.umu.se/"
        
    def test_scope(self):
        scope = utils.make_instance(samlp.Scoping, self.client.scoping(
                                [self.client.idp_entry(name="Umeå Universitet",
                                    location="https://idp.umu.se/")]))
        
        assert scope.idp_list
        assert len(scope.idp_list.idp_entry) == 1
        idp_entry = scope.idp_list.idp_entry[0]
        assert idp_entry.name == "Umeå Universitet"
        assert idp_entry.loc == "https://idp.umu.se/"