Blame bindings/python/tests/profiles_tests.py

Packit 228f82
# -*- coding: UTF-8 -*-
Packit 228f82
#
Packit 228f82
# $Id: profiles_tests.py 3254 2007-06-05 21:23:57Z fpeters $
Packit 228f82
#
Packit 228f82
# Python unit tests for Lasso library
Packit 228f82
#
Packit 228f82
# Copyright (C) 2004-2007 Entr'ouvert
Packit 228f82
# http://lasso.entrouvert.org
Packit 228f82
#
Packit 228f82
# Authors: See AUTHORS file in top-level directory.
Packit 228f82
#
Packit 228f82
# This program is free software; you can redistribute it and/or modify
Packit 228f82
# it under the terms of the GNU General Public License as published by
Packit 228f82
# the Free Software Foundation; either version 2 of the License, or
Packit 228f82
# (at your option) any later version.
Packit 228f82
#
Packit 228f82
# This program is distributed in the hope that it will be useful,
Packit 228f82
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 228f82
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 228f82
# GNU General Public License for more details.
Packit 228f82
#
Packit 228f82
# You should have received a copy of the GNU General Public License
Packit 228f82
# along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 228f82
Packit 228f82
Packit 228f82
import os
Packit 228f82
import unittest
Packit 228f82
import sys
Packit 228f82
Packit 228f82
if not '..' in sys.path:
Packit 228f82
    sys.path.insert(0, '..')
Packit 228f82
if not '../.libs' in sys.path:
Packit 228f82
    sys.path.insert(0, '../.libs')
Packit 228f82
Packit 228f82
import lasso
Packit 228f82
import logging
Packit 228f82
Packit 228f82
logging.basicConfig()
Packit 228f82
Packit 228f82
Packit 228f82
try:
Packit 228f82
    dataDir
Packit 228f82
except NameError:
Packit 228f82
    srcdir = os.environ.get('TOP_SRCDIR', '.')
Packit 228f82
    dataDir = '%s/tests/data' % srcdir
Packit 228f82
Packit 228f82
def server(local_name, remote_role, remote_name):
Packit 228f82
    pwd = os.path.join(dataDir, local_name, 'password')
Packit 228f82
    password = None
Packit 228f82
    if os.path.exists(pwd):
Packit 228f82
        password = open(pwd).read()
Packit 228f82
    s = lasso.Server(os.path.join(dataDir, local_name, 'metadata.xml'),
Packit 228f82
            os.path.join(dataDir, local_name, 'private-key.pem'),
Packit 228f82
            password)
Packit 228f82
    s.addProvider(remote_role, os.path.join(dataDir, remote_name, 'metadata.xml'))
Packit 228f82
    return s
Packit 228f82
Packit 228f82
class ServerTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        """Server construction, dump & newFromDump."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        dump = lassoServer.dump()
Packit 228f82
        lassoServer2 = lassoServer.newFromDump(dump)
Packit 228f82
        dump2 = lassoServer2.dump()
Packit 228f82
        self.failUnlessEqual(dump, dump2)
Packit 228f82
Packit 228f82
    def test02(self):
Packit 228f82
        """Server construction without argument, dump & newFromDump."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'))
Packit 228f82
        dump = lassoServer.dump()
Packit 228f82
        lassoServer2 = lassoServer.newFromDump(dump)
Packit 228f82
        dump2 = lassoServer2.dump()
Packit 228f82
        self.failUnlessEqual(dump, dump2)
Packit 228f82
Packit 228f82
Packit 228f82
class LoginTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        """SP login; testing access to authentication request."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        login = lasso.Login(lassoServer)
Packit 228f82
        login.initAuthnRequest()
Packit 228f82
        login.request
Packit 228f82
        login.request.protocolProfile = lasso.LIB_PROTOCOL_PROFILE_BRWS_ART
Packit 228f82
        self.failUnlessEqual(login.request.protocolProfile, lasso.LIB_PROTOCOL_PROFILE_BRWS_ART)
Packit 228f82
Packit 228f82
    def test02(self):
Packit 228f82
        """SP login; testing processing of an empty Response."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        login = lasso.Login(lassoServer)
Packit 228f82
        try:
Packit 228f82
            login.processResponseMsg('')
Packit 228f82
        except lasso.Error as error:
Packit 228f82
            if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
Packit 228f82
                raise
Packit 228f82
Packit 228f82
    def test03(self):
Packit 228f82
        """Conversion of a lib:AuthnRequest with an AuthnContext into a query and back."""
Packit 228f82
Packit 228f82
        sp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        sp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        spLogin = lasso.Login(sp)
Packit 228f82
        spLogin.initAuthnRequest()
Packit 228f82
        requestAuthnContext = lasso.LibRequestAuthnContext()
Packit 228f82
        authnContextClassRefsList = []
Packit 228f82
        authnContextClassRefsList.append(
Packit 228f82
            lasso.LIB_AUTHN_CONTEXT_CLASS_REF_PASSWORD)
Packit 228f82
        requestAuthnContext.authnContextClassRef = tuple(authnContextClassRefsList)
Packit 228f82
        spLogin.request.requestAuthnContext = requestAuthnContext
Packit 228f82
        spLogin.request.protocolProfile = lasso.LIB_PROTOCOL_PROFILE_BRWS_ART
Packit 228f82
        spLogin.buildAuthnRequestMsg()
Packit 228f82
        authnRequestUrl = spLogin.msgUrl
Packit 228f82
        authnRequestQuery = spLogin.msgUrl[spLogin.msgUrl.index('?') + 1:]
Packit 228f82
        idp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        idp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        idpLogin = lasso.Login(idp)
Packit 228f82
        idpLogin.processAuthnRequestMsg(authnRequestQuery)
Packit 228f82
        self.failUnless(idpLogin.request.requestAuthnContext)
Packit 228f82
        authnContextClassRefsList = idpLogin.request.requestAuthnContext.authnContextClassRef
Packit 228f82
        self.failUnlessEqual(len(authnContextClassRefsList), 1)
Packit 228f82
        self.failUnlessEqual(authnContextClassRefsList[0],
Packit 228f82
                             lasso.LIB_AUTHN_CONTEXT_CLASS_REF_PASSWORD)
Packit 228f82
Packit 228f82
    def test04(self):
Packit 228f82
        """Conversion of a lib:AuthnRequest with extensions into a query and back."""
Packit 228f82
Packit 228f82
        sp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        sp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        spLogin = lasso.Login(sp)
Packit 228f82
        spLogin.initAuthnRequest()
Packit 228f82
        requestAuthnContext = lasso.LibRequestAuthnContext()
Packit 228f82
        extensionList = []
Packit 228f82
        for extension in (
Packit 228f82
                '<action>do</action>',
Packit 228f82
                '<action2>do action 2</action2><action3>do action 3</action3>'):
Packit 228f82
            extensionList.append(
Packit 228f82
                '<lib:Extension xmlns:lib="urn:liberty:iff:2003-08">%s</lib:Extension>'
Packit 228f82
                % extension)
Packit 228f82
        spLogin.request.extension = tuple(extensionList)
Packit 228f82
        spLogin.request.protocolProfile = lasso.LIB_PROTOCOL_PROFILE_BRWS_ART
Packit 228f82
        spLogin.buildAuthnRequestMsg()
Packit 228f82
        authnRequestUrl = spLogin.msgUrl
Packit 228f82
        authnRequestQuery = spLogin.msgUrl[spLogin.msgUrl.index('?') + 1:]
Packit 228f82
        idp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        idp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        idpLogin = lasso.Login(idp)
Packit 228f82
        idpLogin.processAuthnRequestMsg(authnRequestQuery)
Packit 228f82
        self.failUnless(idpLogin.request.extension)
Packit 228f82
        extensionsList = idpLogin.request.extension
Packit 228f82
        self.failUnlessEqual(len(extensionsList), 1)
Packit 228f82
        self.failUnless('<action>do</action>' in extensionsList[0])
Packit 228f82
        self.failUnless('<action2>do action 2</action2>' in extensionsList[0])
Packit 228f82
        self.failUnless('<action3>do action 3</action3>' in extensionsList[0])
Packit 228f82
Packit 228f82
    def test05(self):
Packit 228f82
        '''SAMLv2 Authn request emitted and received using Artifact binding'''
Packit 228f82
        sp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/private-key.pem'))
Packit 228f82
        assert sp
Packit 228f82
        sp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp5-saml2/metadata.xml'))
Packit 228f82
        sp_login = lasso.Login(sp)
Packit 228f82
        assert sp_login
Packit 228f82
        sp_login.initAuthnRequest(None, lasso.HTTP_METHOD_ARTIFACT_GET)
Packit 228f82
        sp_login.buildAuthnRequestMsg()
Packit 228f82
        sp_login_dump = sp_login.dump()
Packit 228f82
        idp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp5-saml2/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp5-saml2/private-key.pem'))
Packit 228f82
        idp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/metadata.xml'))
Packit 228f82
        idp_login = lasso.Login(idp)
Packit 228f82
        idp_login.initRequest(sp_login.msgUrl.split('?')[1], lasso.HTTP_METHOD_ARTIFACT_GET)
Packit 228f82
        idp_login.buildRequestMsg()
Packit 228f82
        sp_login2 = lasso.Login.newFromDump(sp, sp_login_dump)
Packit 228f82
        assert isinstance(sp_login2, lasso.Login)
Packit 228f82
        assert idp_login.msgBody
Packit 228f82
        sp_login2.processRequestMsg(idp_login.msgBody)
Packit 228f82
        sp_login2.buildResponseMsg()
Packit 228f82
        assert sp_login2.msgBody
Packit 228f82
        try:
Packit 228f82
            idp_login.processResponseMsg(sp_login2.msgBody)
Packit 228f82
        except:
Packit 228f82
            raise
Packit 228f82
        assert isinstance(idp_login.request, lasso.Samlp2AuthnRequest)
Packit 228f82
Packit 228f82
    def test_06(self):
Packit 228f82
        '''Login test between SP and IdP with encrypted private keys'''
Packit 228f82
        sp_server = server('sp7-saml2', lasso.PROVIDER_ROLE_IDP, 'idp7-saml2')
Packit 228f82
        idp_server = server('idp7-saml2', lasso.PROVIDER_ROLE_SP, 'sp7-saml2')
Packit 228f82
Packit 228f82
        sp_login = lasso.Login(sp_server)
Packit 228f82
        sp_login.initAuthnRequest()
Packit 228f82
        sp_login.request.protocolBinding = lasso.SAML2_METADATA_BINDING_POST;
Packit 228f82
        sp_login.buildAuthnRequestMsg()
Packit 228f82
        idp_login = lasso.Login(idp_server)
Packit 228f82
        idp_login.setSignatureVerifyHint(lasso.PROFILE_SIGNATURE_VERIFY_HINT_FORCE)
Packit 228f82
        idp_login.processAuthnRequestMsg(sp_login.msgUrl.split('?')[1])
Packit 228f82
        idp_login.validateRequestMsg(True, True)
Packit 228f82
        idp_login.buildAssertion("None", "None", "None", "None", "None")
Packit 228f82
        idp_login.buildAuthnResponseMsg()
Packit 228f82
        sp_login.setSignatureVerifyHint(lasso.PROFILE_SIGNATURE_VERIFY_HINT_FORCE)
Packit 228f82
        sp_login.processAuthnResponseMsg(idp_login.msgBody)
Packit 228f82
        sp_login.acceptSso()
Packit 228f82
Packit 228f82
    def test07(self):
Packit 228f82
        '''SAMLv2 SSO with DSA key for the IdP'''
Packit 228f82
        sp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/private-key.pem'))
Packit 228f82
        assert sp
Packit 228f82
        sp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp12-dsa-saml2/metadata.xml'))
Packit 228f82
        sp_login = lasso.Login(sp)
Packit 228f82
        assert sp_login
Packit 228f82
        sp_login.initAuthnRequest(None, lasso.HTTP_METHOD_REDIRECT)
Packit 228f82
        sp_login.buildAuthnRequestMsg()
Packit 228f82
        idp = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp12-dsa-saml2/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp12-dsa-saml2/private-key.pem'))
Packit 228f82
        idp.signatureMethod = lasso.SIGNATURE_METHOD_DSA_SHA1
Packit 228f82
        idp.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp5-saml2/metadata.xml'))
Packit 228f82
        idp_login = lasso.Login(idp)
Packit 228f82
        idp_login.processAuthnRequestMsg(sp_login.msgUrl.split('?')[1])
Packit 228f82
        idp_login.protocolProfile = lasso.LOGIN_PROTOCOL_PROFILE_BRWS_POST;
Packit 228f82
        idp_login.validateRequestMsg(True, True)
Packit 228f82
        idp_login.buildAssertion("None", "None", "None", "None", "None")
Packit 228f82
        idp_login.buildAuthnResponseMsg()
Packit 228f82
Packit 228f82
class LogoutTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        """SP logout without session and identity; testing initRequest."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_IDP,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        logout = lasso.Logout(lassoServer)
Packit 228f82
        try:
Packit 228f82
            logout.initRequest()
Packit 228f82
        except lasso.Error as error:
Packit 228f82
            if error[0] != lasso.PROFILE_ERROR_SESSION_NOT_FOUND:
Packit 228f82
                raise
Packit 228f82
        else:
Packit 228f82
            self.fail('logout.initRequest without having set identity before should fail')
Packit 228f82
Packit 228f82
    def test02(self):
Packit 228f82
        """IDP logout without session and identity; testing logout.getNextProviderId."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        logout = lasso.Logout(lassoServer)
Packit 228f82
        self.failIf(logout.getNextProviderId())
Packit 228f82
Packit 228f82
    def test03(self):
Packit 228f82
        """IDP logout; testing processRequestMsg with non Liberty query."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        logout = lasso.Logout(lassoServer)
Packit 228f82
        # The processRequestMsg should fail but not abort.
Packit 228f82
        try:
Packit 228f82
            logout.processRequestMsg('passport=0&lasso=1')
Packit 228f82
        except lasso.Error as error:
Packit 228f82
            if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
Packit 228f82
                raise
Packit 228f82
        else:
Packit 228f82
            self.fail('Logout processRequestMsg should have failed.')
Packit 228f82
Packit 228f82
    def test04(self):
Packit 228f82
        """IDP logout; testing processResponseMsg with non Liberty query."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        logout = lasso.Logout(lassoServer)
Packit 228f82
        # The processResponseMsg should fail but not abort.
Packit 228f82
        try:
Packit 228f82
            logout.processResponseMsg('liberty=&alliance')
Packit 228f82
        except lasso.Error as error:
Packit 228f82
            if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
Packit 228f82
                raise
Packit 228f82
        else:
Packit 228f82
            self.fail('Logout processResponseMsg should have failed.')
Packit 228f82
Packit Service e5601a
    def test05(self):
Packit Service e5601a
        '''Test parsing of a logout request with more than one session index'''
Packit Service e5601a
        content = '''<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="xxxx" Version="2.0" IssueInstant="2010-06-14T22:00:00">
Packit Service e5601a
        <saml:Issuer>me</saml:Issuer>
Packit Service e5601a
        <saml:NameID>coin</saml:NameID>
Packit Service e5601a
        <samlp:SessionIndex>id1</samlp:SessionIndex>
Packit Service e5601a
        <samlp:SessionIndex>id2</samlp:SessionIndex>
Packit Service e5601a
        <samlp:SessionIndex>id3</samlp:SessionIndex>
Packit Service e5601a
        </samlp:LogoutRequest>'''
Packit Service e5601a
Packit Service e5601a
        node = lasso.Samlp2LogoutRequest.newFromXmlNode(content)
Packit Service e5601a
        assert isinstance(node, lasso.Samlp2LogoutRequest)
Packit Service e5601a
        assert node.sessionIndex == 'id1'
Packit Service e5601a
        assert node.sessionIndexes == ('id1', 'id2', 'id3')
Packit Service e5601a
Packit 228f82
class DefederationTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        """IDP initiated defederation; testing processNotificationMsg with non Liberty query."""
Packit 228f82
Packit 228f82
        lassoServer = lasso.Server(
Packit 228f82
            os.path.join(dataDir, 'idp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'idp1-la/private-key-raw.pem'),
Packit 228f82
            None,
Packit 228f82
            os.path.join(dataDir, 'idp1-la/certificate.pem'))
Packit 228f82
        lassoServer.addProvider(
Packit 228f82
            lasso.PROVIDER_ROLE_SP,
Packit 228f82
            os.path.join(dataDir, 'sp1-la/metadata.xml'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/public-key.pem'),
Packit 228f82
            os.path.join(dataDir, 'sp1-la/certificate.pem'))
Packit 228f82
        defederation = lasso.Defederation(lassoServer)
Packit 228f82
        # The processNotificationMsg should fail but not abort.
Packit 228f82
        try:
Packit 228f82
            defederation.processNotificationMsg('nonLibertyQuery=1')
Packit 228f82
        except lasso.Error as error:
Packit 228f82
            if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
Packit 228f82
                raise
Packit 228f82
        else:
Packit 228f82
            self.fail('Defederation processNotificationMsg should have failed.')
Packit 228f82
Packit 228f82
Packit 228f82
class IdentityTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        """Identity newFromDump & dump."""
Packit 228f82
        return
Packit 228f82
        # test disabled since dump format changed
Packit 228f82
        identityDump = """<Identity xmlns="http://www.entrouvert.org/namespaces/lasso/0.0" Version="1"><Federations><Federation xmlns="http://www.entrouvert.org/namespaces/lasso/0.0" Version="1" RemoteProviderID="https://sp1.entrouvert.lan/metadata"><LocalNameIdentifier><saml:NameIdentifier xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" NameQualifier="https://proxy2.entrouvert.lan/metadata" Format="urn:liberty:iff:nameid:federated">_CD739B41C602EAEA93626EBD1751CB46</saml:NameIdentifier></LocalNameIdentifier></Federation><Federation xmlns="http://www.entrouvert.org/namespaces/lasso/0.0" Version="1" RemoteProviderID="https://idp1.entrouvert.lan/metadata"><RemoteNameIdentifier><saml:NameIdentifier xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" NameQualifier="https://idp1.entrouvert.lan/metadata" Format="urn:liberty:iff:nameid:federated">_11EA77A4FED32C41824AC5DE87298E65</saml:NameIdentifier></RemoteNameIdentifier></Federation></Federations></Identity>"""
Packit 228f82
        identity = lasso.Identity.newFromDump(identityDump)
Packit 228f82
        newIdentityDump = identity.dump()
Packit 228f82
        self.failUnlessEqual(identityDump, newIdentityDump)
Packit 228f82
Packit 228f82
class AttributeAuthorityTestCase(unittest.TestCase):
Packit 228f82
    def test01(self):
Packit 228f82
        '''Attribute request and response test between sp5 and idp6'''
Packit 228f82
        s = lasso.Server(
Packit 228f82
                os.path.join(dataDir, 'sp5-saml2/metadata.xml'),
Packit 228f82
                os.path.join(dataDir, 'sp5-saml2/private-key.pem'))
Packit 228f82
        s.addProvider(lasso.PROVIDER_ROLE_ATTRIBUTE_AUTHORITY,
Packit 228f82
                os.path.join(dataDir, 'idp6-saml2/metadata.xml'))
Packit 228f82
Packit 228f82
        s2 = lasso.Server(
Packit 228f82
                os.path.join(dataDir, 'idp6-saml2/metadata.xml'),
Packit 228f82
                os.path.join(dataDir, 'idp6-saml2/private-key.pem'))
Packit 228f82
        s2.addProvider(lasso.PROVIDER_ROLE_SP,
Packit 228f82
                os.path.join(dataDir, 'sp5-saml2/metadata.xml'))
Packit 228f82
Packit 228f82
        aq = lasso.AssertionQuery(s)
Packit 228f82
        rpid = list(s.providers.keys())[0]
Packit 228f82
        aq.initRequest(rpid,
Packit 228f82
                lasso.HTTP_METHOD_SOAP,
Packit 228f82
                lasso.ASSERTION_QUERY_REQUEST_TYPE_ATTRIBUTE)
Packit 228f82
        assert aq.request
Packit 228f82
        assert aq.remoteProviderId == rpid
Packit 228f82
        nid = lasso.Saml2NameID.newWithPersistentFormat(
Packit 228f82
                lasso.buildUniqueId(32),
Packit 228f82
                s.providerId, s2.providerId)
Packit 228f82
        aq.nameIdentifier = nid
Packit 228f82
        aq.addAttributeRequest(
Packit 228f82
                lasso.SAML2_ATTRIBUTE_NAME_FORMAT_BASIC,
Packit 228f82
                'testAttribute')
Packit 228f82
        aq.buildRequestMsg()
Packit 228f82
        assert aq.msgUrl
Packit 228f82
        assert aq.msgBody
Packit 228f82
Packit 228f82
        aq2 = lasso.AssertionQuery(s2)
Packit 228f82
        aq2.processRequestMsg(aq.msgBody)
Packit 228f82
        assert aq.request
Packit 228f82
        aq2.validateRequest()
Packit 228f82
        assert aq2.response
Packit 228f82
        assertion = lasso.Saml2Assertion()
Packit 228f82
        aq2.response.assertion = (assertion, )
Packit 228f82
        for attribute in aq2.request.attribute:
Packit 228f82
            content = lasso.MiscTextNode.newWithString("xxx")
Packit 228f82
            content.textChild = True
Packit 228f82
            assertion.addAttributeWithNode(attribute.name, attribute.nameFormat,
Packit 228f82
                    content)
Packit 228f82
            assertion.addAttributeWithNode(attribute.name, attribute.nameFormat,
Packit 228f82
                    content)
Packit 228f82
        assertion.subject = aq.request.subject
Packit 228f82
        s2.saml2AssertionSetupSignature(assertion)
Packit 228f82
        aq2.buildResponseMsg()
Packit 228f82
        aq.processResponseMsg(aq2.msgBody)
Packit 228f82
        assert aq.response
Packit 228f82
        assert aq.response.assertion[0]
Packit 228f82
        assert aq.response.assertion[0].attributeStatement[0]
Packit 228f82
        assert aq.response.assertion[0].attributeStatement[0].attribute[0]
Packit 228f82
        assert aq.response.assertion[0].attributeStatement[0].attribute[0].attributeValue[0]
Packit 228f82
Packit 228f82
serverSuite = unittest.makeSuite(ServerTestCase, 'test')
Packit 228f82
loginSuite = unittest.makeSuite(LoginTestCase, 'test')
Packit 228f82
logoutSuite = unittest.makeSuite(LogoutTestCase, 'test')
Packit 228f82
defederationSuite = unittest.makeSuite(DefederationTestCase, 'test')
Packit 228f82
identitySuite = unittest.makeSuite(IdentityTestCase, 'test')
Packit 228f82
attributeSuite = unittest.makeSuite(AttributeAuthorityTestCase, 'test')
Packit 228f82
Packit 228f82
allTests = unittest.TestSuite((serverSuite, loginSuite, logoutSuite, defederationSuite,
Packit Service e5601a
                               identitySuite, attributeSuite))
Packit 228f82
Packit 228f82
if __name__ == '__main__':
Packit 228f82
    sys.exit(not unittest.TextTestRunner(verbosity = 2).run(allTests).wasSuccessful())
Packit 228f82