summaryrefslogtreecommitdiffstats
path: root/python/tests/ServiceProvider.py
blob: 12879d050add8ae2dc343b721bc72a9b26f7e959 (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
# -*- coding: UTF-8 -*-


# Lasso Simulator
# By: Emmanuel Raviart <eraviart@entrouvert.com>
#
# Copyright (C) 2004 Entr'ouvert
# http://lasso.entrouvert.org
#
# 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 lasso

import Provider


class ServiceProviderMixin(Provider.ProviderMixin):
    createNewAccountWhenNewFederationForUnknownUser = False
    idpSite = None # The identity provider, this service provider will use to authenticate users.

    def assertionConsumer(self, handler):
        lassoServer = self.getLassoServer()
        login = lasso.Login.new(lassoServer)

        if handler.httpRequest.method == 'GET':
            relayState = handler.httpRequest.getQueryField('RelayState', None)
            login.init_request(handler.httpRequest.query, lasso.httpMethodRedirect)
            login.build_request_msg()

            soapEndpoint = login.msg_url
            failUnless(soapEndpoint)
            soapRequestMsg = login.msg_body
            failUnless(soapRequestMsg)
            httpResponse = self.sendHttpRequest(
                'POST', soapEndpoint, headers = {'Content-Type': 'text/xml'},
                body = soapRequestMsg)
            failUnlessEqual(httpResponse.statusCode, 200)
            try:
                login.process_response_msg(httpResponse.body)
            except lasso.Error, error:
                if error.code == -7: # FIXME: This will change, he said.
                    return handler.respond(
                        401,
                        'Access Unauthorized: User authentication failed on identity provider.')
                else:
                    raise
        elif handler.httpRequest.method == 'POST':
            relayState = handler.httpRequest.getFormField('RelayState', None)
            authnResponseMsg = handler.httpRequest.getFormField('LARES', None)
            failUnless(authnResponseMsg)
            # FIXME: Should we do an init before process_authn_response_msg?
            try:
                login.process_authn_response_msg(authnResponseMsg)
            except lasso.Error, error:
                if error.code == -7: # FIXME: This will change, he said.
                    return handler.respond(
                        401,
                        'Access Unauthorized: User authentication failed on identity provider.')
                else:
                    raise
        else:
            return handler.respond(
                400,
                'Bad Request: Method %s not handled by assertionConsumer'
                    % handler.httpRequest.method)

        nameIdentifier = login.nameIdentifier
        failUnless(nameIdentifier)

        # Retrieve session dump, using name identifier or else try to use the client web session.
        # If session dump exists, give it to Lasso, so that it updates it.
        session = self.getSessionFromNameIdentifier(nameIdentifier)
        if session is None:
            session = handler.session
        if session is not None and session.lassoSessionDump is not None:
            login.set_session_from_dump(session.lassoSessionDump)
        # Retrieve identity dump, using name identifier or else try to retrieve him from web
        # session. If identity dump exists, give it to Lasso, so that it updates it.
        user = self.getUserFromNameIdentifier(nameIdentifier)
        if user is None:
            user = handler.user
        if user is not None and user.lassoIdentityDump is not None:
            login.set_identity_from_dump(user.lassoIdentityDump)

        login.accept_sso()
        if user is not None and user.lassoIdentityDump is None:
            failUnless(login.is_identity_dirty())
        lassoIdentity = login.get_identity()
        failUnless(lassoIdentity)
        lassoIdentityDump = lassoIdentity.dump()
        failUnless(lassoIdentityDump)
        failUnless(login.is_session_dirty())
        lassoSession = login.get_session()
        failUnless(lassoSession)
        lassoSessionDump = lassoSession.dump()
        failUnless(lassoSessionDump)

        # User is now authenticated.

        # If there was no web session yet, create it. Idem for the web user account.
        if session is None:
            session = handler.createSession()
            session.publishToken = True
        if user is None:
            # The user has been successfully authenticated on identity provider, but he has no
            # account on this service provider or his account is not federated yet and he is not
            # logged.
            # A real service provider would ask user to login locally to create a federation. Or it
            # would ask user informations to create a local account. Or it would automatically
            # create a new account...
            if self.createNewAccountWhenNewFederationForUnknownUser:
                user = handler.createUser()
            else:
                # Save some informations in session for a short time (until user is logged).
                # These informations can't be stored as fields in URL query, because they are too
                # large.
                session.lassoIdentityDump = lassoIdentityDump
                session.lassoSessionDump = lassoSessionDump
                session.nameIdentifier = nameIdentifier
                session.relayState = relayState

                # We do a redirect now for two reasons:
                # - We don't want the user to be able to reload assertionConsumer page (because the
                #   artifact has been removed from identity-provider).
                # - For HTTP authentication, we don't want to emit a 401 Unauthorized that would
                #   force the Principal to reload the assertionConsumer page.
                # FIXME: Add the session token to redirect URL.
                return handler.respondRedirectTemporarily('/login_local')

        session.userId = user.uniqueId
        user.sessionToken = session.token

        # Store the updated identity dump and session dump.
        session.lassoSessionDump = lassoSessionDump
        if login.is_identity_dirty():
            user.lassoIdentityDump = lassoIdentityDump

        self.userIdsByNameIdentifier[nameIdentifier] = user.uniqueId
        self.sessionTokensByNameIdentifier[nameIdentifier] = session.token

        # We do a redirect now because we don't want the user to be able to reload
        # assertionConsumer page (because the artifact has been removed from identity-provider).
        # FIXME: Add the session token to redirect URL.
        redirectUrl = '/assertionConsumer_done'
        if relayState:
            redirectUrl = '%s?RelayState=%s' % (redirectUrl, relayState)
        return handler.respondRedirectTemporarily(redirectUrl)

    def assertionConsumer_done(self, handler):
        # A real service provider could use the string relayState for any purpose.
        relayState = handler.httpRequest.getQueryField('RelayState', None)
        return handler.respond(
            200, headers = {'Content-Type': 'text/plain'},
            body = 'Liberty authentication succeeded\nRelayState = %s' % relayState)

    def login(self, handler):
        libertyEnabled = handler.httpRequest.headers.get('Liberty-Enabled', None)
        userAgent = handler.httpRequest.headers.get('User-Agent', None)
        # FIXME: Lasso should have a function to compute useLecp.
        # Or this should be done in lasso.Login.new(lassoServer, libertyEnabled, userAgent)
        useLecp = False
        if libertyEnabled:
            useLecp = 'urn:liberty:iff:2003-08' in libertyEnabled
            if not useLecp:
                return handler.respond(501, 'Unsupported Liberty Version.')
        elif userAgent:
            useLecp = 'urn:liberty:iff:2003-08' in userAgent
            if not useLecp and "LIBV=" in userAgent:
                return handler.respond(501, 'Unsupported Liberty Version.')
        else:
            useLecp = False

        forceAuthn = handler.httpRequest.getQueryBoolean('forceAuthn', False)
        isPassive = handler.httpRequest.getQueryBoolean('isPassive', False)
        relayState = handler.httpRequest.getQueryField('RelayState', None)
        lassoServer = self.getLassoServer()
        if useLecp:
            lecp = lasso.Lecp.new(lassoServer)
            lecp.init_authn_request()
            failUnlessEqual(lecp.request_type, lasso.messageTypeAuthnRequest)

            # FIXME: This protocol profile should be set by default by Lasso.
            lecp.request.set_protocolProfile(lasso.libProtocolProfileBrwsPost)

            # Same treatement as for non LECP login.
            if forceAuthn:
                lecp.request.set_forceAuthn(forceAuthn)
            if not isPassive:
                lecp.request.set_isPassive(isPassive)
            lecp.request.set_nameIDPolicy(lasso.libNameIDPolicyTypeFederated)
            lecp.request.set_consent(lasso.libConsentObtained)
            if relayState:
                lecp.request.set_relayState(relayState)

            lecp.build_authn_request_envelope_msg()
            authnRequestEnvelopeMsg = lecp.msg_body
            failUnless(authnRequestEnvelopeMsg)
            # FIXME: Lasso should set a lecp.msg_content_type to
            # "application/vnd.liberty-request+xml". This should also be done for SOAP, etc, with
            # other profiles.
            # contentType = lecp.msg_content_type
            # failUnlessEqual(contentType, 'application/vnd.liberty-request+xml')
            contentType = 'application/vnd.liberty-request+xml'
            headers = {
                'Content-Type': contentType,
                'Cache-Control': 'no-cache',
                'Pragma': 'no-cache',
                }
            headers.update(self.libertyEnabledHeaders)
            return handler.respond(headers = headers, body = authnRequestEnvelopeMsg)
        else:
            login = lasso.Login.new(lassoServer)
            login.init_authn_request()
            failUnlessEqual(login.request_type, lasso.messageTypeAuthnRequest)
            if forceAuthn:
                login.request.set_forceAuthn(forceAuthn)
            if not isPassive:
                login.request.set_isPassive(isPassive)
            login.request.set_nameIDPolicy(lasso.libNameIDPolicyTypeFederated)
            login.request.set_consent(lasso.libConsentObtained)
            if relayState:
                login.request.set_relayState(relayState)
            login.build_authn_request_msg(self.idpSite.providerId)
            authnRequestUrl = login.msg_url
            failUnless(authnRequestUrl)
            return handler.respondRedirectTemporarily(authnRequestUrl)

    def login_done(self, handler, userAuthenticated, authenticationMethod):
        # Remove  informations that are no more needed in session.
        session = handler.session
        lassoIdentityDump = session.lassoIdentityDump
        del session.lassoIdentityDump
        nameIdentifier = session.nameIdentifier
        del session.nameIdentifier
        relayState = session.relayState
        del session.relayState

        if not userAuthenticated:
            return self.login_failed(handler)

        # User has been authenticated => Create federation.
        user = handler.user
        user.lassoIdentityDump = lassoIdentityDump
        self.userIdsByNameIdentifier[nameIdentifier] = user.uniqueId
        self.sessionTokensByNameIdentifier[nameIdentifier] = session.token
        # Note: The uppercase for RelayState below is not a bug.
        return self.callHttpFunction(self.assertionConsumer_done, handler, RelayState = relayState)

    def logout(self, handler):
        session = handler.session
        if session is None:
            return handler.respond(401, 'Access Unauthorized: User has no session opened.')
        user = handler.user
        if user is None:
            return handler.respond(401, 'Access Unauthorized: User is not logged in.')
        return self.logout_do(handler, session, user)

    def logout_do(self, handler, session, user):
        lassoServer = self.getLassoServer()
        logout = lasso.Logout.new(lassoServer, lasso.providerTypeSp)
        if user.lassoIdentityDump is not None:
            logout.set_identity_from_dump(user.lassoIdentityDump)
        if session.lassoSessionDump is not None:
            logout.set_session_from_dump(session.lassoSessionDump)
        logout.init_request()
        logout.build_request_msg()

        soapEndpoint = logout.msg_url
        failUnless(soapEndpoint)
        soapRequestMsg = logout.msg_body
        failUnless(soapRequestMsg)
        httpResponse = self.sendHttpRequest(
            'POST', soapEndpoint, headers = {'Content-Type': 'text/xml'}, body = soapRequestMsg)
        failUnlessEqual(httpResponse.statusCode, 200)

        logout.process_response_msg(httpResponse.body, lasso.httpMethodSoap)
        failIf(logout.is_identity_dirty())
        identity = logout.get_identity()
        failUnless(identity)
        lassoIdentityDump = identity.dump()
        failUnless(lassoIdentityDump)
        failUnless(logout.is_session_dirty())
        lassoSession = logout.get_session()
        if lassoSession is None:
            # The user is no more authenticated on any identity provider. Log him out.
            del session.lassoSessionDump
            del session.userId
            # We also delete the session, but it is not mandantory, since the user is logged out
            # anyway.
            del self.sessions[session.token] 
        else:
            # The user is still logged in on some other identity providers.
            lassoSessionDump = lassoSession.dump()
            failUnless(lassoSessionDump)
            session.lassoSessionDump = lassoSessionDump
        nameIdentifier = logout.nameIdentifier
        return self.logout_done(handler, nameIdentifier)

    def logout_done(self, handler, nameIdentifier):
        failUnless(nameIdentifier)
        del self.sessionTokensByNameIdentifier[nameIdentifier]

        return handler.respond(200, headers = {'Content-Type': 'text/plain'},
                               body = 'Liberty logout succeeded')