summaryrefslogtreecommitdiffstats
path: root/python/tests/login_tests.py
blob: 75b00aad1bf1a640ede8aeaf63f891812543d0a1 (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
#! /usr/bin/env python
# -*- coding: UTF-8 -*-


# Python unit tests for Lasso library
#
# Copyright (C) 2004 Entr'ouvert
# http://lasso.entrouvert.org
# 
# Authors: Emmanuel Raviart <eraviart@entrouvert.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import unittest
import sys

sys.path.insert(0, '..')
sys.path.insert(0, '../.libs')

import lasso

from IdentityProvider import IdentityProvider
from ServiceProvider import ServiceProvider
from websimulator import *


class LoginTestCase(unittest.TestCase):
    def generateIdpSite(self, internet):
        site = IdentityProvider(self, internet, 'https://identity-provider/')
        site.providerId = 'https://identity-provider/metadata'

        server = lasso.Server.new(
            '../../examples/data/idp-metadata.xml',
            '../../examples/data/idp-public-key.pem',
            '../../examples/data/idp-private-key.pem',
            '../../examples/data/idp-crt.pem',
            lasso.signatureMethodRsaSha1)
        server.add_provider(
            '../../examples/data/sp-metadata.xml',
            '../../examples/data/sp-public-key.pem',
            '../../examples/data/ca-crt.pem')
        site.serverDump = server.dump()
        self.failUnless(site.serverDump)
        server.destroy()

        site.addWebUser('Chantereau')
        site.addWebUser('Clapies')
        site.addWebUser('Febvre')
        site.addWebUser('Nowicki')
        # Frederic Peters has no account on identity provider.
        return site

    def generateSpSite(self, internet):
        site = ServiceProvider(self, internet, 'https://service-provider/')
        site.providerId = 'https://service-provider/metadata'

        server = lasso.Server.new(
            '../../examples/data/sp-metadata.xml',
            '../../examples/data/sp-public-key.pem',
            '../../examples/data/sp-private-key.pem',
            '../../examples/data/sp-crt.pem',
            lasso.signatureMethodRsaSha1)
        server.add_provider(
            '../../examples/data/idp-metadata.xml',
            '../../examples/data/idp-public-key.pem',
            '../../examples/data/ca-crt.pem')
        site.serverDump = server.dump()
        self.failUnless(site.serverDump)
        server.destroy()

        site.addWebUser('Nicolas')
        site.addWebUser('Romain')
        site.addWebUser('Valery')
        # Christophe Nowicki has no account on service provider.
        site.addWebUser('Frederic')
        return site

##     def setUp(self):
##         pass

##     def tearDown(self):
##         pass

    def test01(self):
        """Service provider initiated login using HTTP redirect and service provider initiated logout using SOAP."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Romain Chantereau')
        principal.keyring[idpSite.url] = 'Chantereau'
        principal.keyring[spSite.url] = 'Romain'

        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        self.failIf(spSite.webSessions)
        self.failIf(idpSite.webSessions)

    def test02(self):
        """Service provider initiated login using HTTP redirect and service provider initiated logout using SOAP. Done three times."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Romain Chantereau')
        principal.keyring[idpSite.url] = 'Chantereau'
        principal.keyring[spSite.url] = 'Romain'

        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)

        # Once again. Now the principal already has a federation between spSite and idpSite.
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)

        # Once again. Do a new passive login between normal login and logout.
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        del principal.keyring[idpSite.url] # Ensure identity provider will be really passive.
        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?isPassive=1'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)

        # Once again, with isPassive and the user having no web session.
        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?isPassive=1'))
        self.failUnlessEqual(httpResponse.statusCode, 401)

    def test03(self):
        """Service provider initiated login using HTTP redirect, but user fail to authenticate himself on identity provider. Then logout, with same problem."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Frederic Peters')
        # Frederic Peters has no account on identity provider.
        principal.keyring[spSite.url] = 'Frederic'

        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 401)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 401)

    def test04(self):
        """Service provider initiated login using HTTP redirect, but user has no account on service
        provider and doesn't create one."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Christophe Nowicki')
        principal.keyring[idpSite.url] = 'Nowicki'
        # Christophe Nowicki has no account on service provider.

        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/loginUsingRedirect'))
        self.failUnlessEqual(httpResponse.statusCode, 401)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 401)

    def test05(self):
        """Service provider initiated login using HTTP redirect with isPassive for a user without federation yet."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Romain Chantereau')
        principal.keyring[idpSite.url] = 'Chantereau'
        principal.keyring[spSite.url] = 'Romain'

        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?isPassive=1'))
        self.failUnlessEqual(httpResponse.statusCode, 401)

    def test06(self):
        """Testing forceAuthn flag."""

        internet = Internet()
        idpSite = self.generateIdpSite(internet)
        spSite = self.generateSpSite(internet)
        spSite.idpSite = idpSite
        principal = Principal(internet, 'Romain Chantereau')
        principal.keyring[idpSite.url] = 'Chantereau'
        principal.keyring[spSite.url] = 'Romain'

        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?forceAuthn=1'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)

        # Ask user to reauthenticate while he is already logged.
        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?forceAuthn=1'))
        self.failUnlessEqual(httpResponse.statusCode, 200)
        del principal.keyring[idpSite.url] # Ensure user can't authenticate.
        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?forceAuthn=1'))
        self.failUnlessEqual(httpResponse.statusCode, 401)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 200)

        # Force authentication, but user won't authenticate.
        httpResponse = spSite.doHttpRequest(HttpRequest(
            principal, 'GET', '/loginUsingRedirect?forceAuthn=1'))
        self.failUnlessEqual(httpResponse.statusCode, 401)
        httpResponse = spSite.doHttpRequest(HttpRequest(principal, 'GET', '/logoutUsingSoap'))
        self.failUnlessEqual(httpResponse.statusCode, 401)

##     def test06(self):
##         """Service provider LECP login."""

##         # LECP has asked service provider for login.
##         spServer = self.getServer()

##         # FIXME: Why doesn't lasso.Lecp.new have spServer as argument?
##         # spLecp = lasso.Lecp.new(spServer)
##         spLecp = lasso.Lecp.new()
##         spLecp.init_authn_request_envelope(sp, )
##         lasso_lecp_init_authn_request_envelope(sp_lecp, spserver, authnRequest);
##         lasso_lecp_build_authn_request_envelope_msg(sp_lecp);
##         msg = g_strdup(sp_lecp->msg_body);
##         lasso_lecp_destroy(sp_lecp);


suite1 = unittest.makeSuite(LoginTestCase, 'test')

allTests = unittest.TestSuite((suite1,))

if __name__ == '__main__':
    sys.exit(not unittest.TextTestRunner(verbosity=2).run(allTests).wasSuccessful())