Index: main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/authentication.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/authentication.py	(revision 5759)
+++ 	(revision )
@@ -1,262 +1,0 @@
-##
-## authentication.py
-## Login : <uli@pu.smp.net>
-## Started on  Tue Jul 27 14:26:35 2010 Uli Fouquet
-## $Id$
-## 
-## Copyright (C) 2010 Uli Fouquet
-## 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
-##
-"""Special authentication for applicants.
-
-   XXX: This is work in progress, experimental code! Don't do that at home!
-"""
-import grok
-from zope.event import notify
-from zope.pluggableauth.factories import Principal
-from zope.pluggableauth.interfaces import (
-    ICredentialsPlugin, IAuthenticatorPlugin, IPrincipalInfo,
-    IAuthenticatedPrincipalFactory, AuthenticatedPrincipalCreated)
-from zope.pluggableauth.plugins.session import SessionCredentialsPlugin
-from zope.publisher.interfaces import IRequest
-from zope.publisher.interfaces.http import IHTTPRequest
-from zope.session.interfaces import ISession
-from waeup.sirp.authentication import Account, PrincipalInfo
-from waeup.sirp.applicants.jambtables.interfaces import (
-    IApplicantPrincipalInfo, IApplicantPrincipal, IApplicantSessionCredentials,
-    IJAMBApplicantSessionCredentials)
-from waeup.sirp.applicants.jambtables.util import (
-    get_applicant_data, application_exists,
-    )
-from waeup.sirp.accesscodes import get_access_code
-
-class PortalUser(grok.Role):
-    """A role for applicants.
-    """
-    grok.name('waeup.Applicant')
-    grok.permissions('waeup.Public')
-
-class ApplicantPrincipalInfo(object):
-    """Infos about an applicant principal.
-    """
-    grok.implements(IApplicantPrincipalInfo)
-
-    def __init__(self, access_code, jamb_reg_no=None):
-        self.id = principal_id(access_code, jamb_reg_no)
-        self.title = u'Applicant'
-        self.description = u'An Applicant'
-        self.credentialsPlugin = None
-        self.authenticatorPlugin = None
-        self.reg_no = jamb_reg_no
-        self.access_code = access_code
-
-class ApplicantPrincipal(Principal):
-    """An applicant principal.
-
-    Applicant principals provide an extra `access_code` and `reg_no`
-    attribute extending ordinary principals.
-    """
-
-    grok.implements(IApplicantPrincipal)
-
-    def __init__(self, access_code, reg_no, prefix=None):
-        self.id = principal_id(access_code, reg_no)
-        if prefix is not None:
-            self.id = '%s.%s' % (prefix, self.id)
-        self.title = u'Applicant'
-        self.description = u'An applicant'
-        self.groups = []
-        self.reg_no = reg_no
-        self.access_code = access_code
-
-    def __repr__(self):
-        return 'ApplicantPrincipal(%r)' % self.id
-
-class AuthenticatedApplicantPrincipalFactory(grok.MultiAdapter):
-    """Creates 'authenticated' applicant principals.
-
-    Adapts (principal info, request) to an ApplicantPrincipal instance.
-
-    This adapter is used by the standard PAU to transform
-    PrincipalInfos into Principal instances.
-    """
-    grok.adapts(IApplicantPrincipalInfo, IRequest)
-    grok.implements(IAuthenticatedPrincipalFactory)
-
-    def __init__(self, info, request):
-        self.info = info
-        self.request = request
-
-    def __call__(self, authentication):
-        principal = ApplicantPrincipal(
-            self.info.access_code,
-            self.info.reg_no,
-            authentication.prefix,
-            )
-        notify(
-            AuthenticatedPrincipalCreated(
-                authentication, principal, self.info, self.request))
-        return principal
-
-
-#
-# Credentials plugins and related....
-#
-
-class ApplicantCredentials(object):
-    """Credentials class for ordinary applicants.
-    """
-    grok.implements(IApplicantSessionCredentials)
-
-    def __init__(self, access_code):
-        self.access_code = access_code
-
-    def getAccessCode(self):
-        """Get the access code.
-        """
-        return self.access_code
-
-    def getLogin(self):
-        """Stay compatible with non-applicant authenticators.
-        """
-        return None
-
-    def getPassword(self):
-        """Stay compatible with non-applicant authenticators.
-        """
-        return None
-    
-class JAMBApplicantCredentials(ApplicantCredentials):
-    """Credentials class for JAMB-screened applicants.
-    """
-    grok.implements(IJAMBApplicantSessionCredentials)
-
-    def __init__(self, access_code, jamb_reg_no):
-        self.access_code = access_code
-        self.jamb_reg_no = jamb_reg_no
-
-    def getJAMBRegNo(self):
-        """Get the JAMB registration no.
-        """
-        return self.jamb_reg_no
-
-class WAeUPApplicantCredentialsPlugin(grok.GlobalUtility,
-                                      SessionCredentialsPlugin):
-    """A credentials plugin that scans requests for applicant credentials.
-    """
-    grok.provides(ICredentialsPlugin)
-    grok.name('applicant_credentials')
-
-    loginpagename = 'login'
-    accesscode_prefix_field = 'form.prefix'
-    accesscode_series_field = 'form.ac_series'
-    accesscode_number_field = 'form.ac_number'
-    jamb_reg_no_field = 'form.jamb_reg_no'
-
-    def extractCredentials(self, request):
-        """Extracts credentials from a session if they exist.
-        """
-        if not IHTTPRequest.providedBy(request):
-            return None
-        session = ISession(request)
-        sessionData = session.get(
-            'zope.pluggableauth.browserplugins')
-        access_code_prefix = request.get(self.accesscode_prefix_field, None)
-        access_code_series = request.get(self.accesscode_series_field, None)
-        access_code_no = request.get(self.accesscode_number_field, None)
-        jamb_reg_no = request.get(self.jamb_reg_no_field, None)
-        access_code = '%s-%s-%s' % (
-            access_code_prefix, access_code_series, access_code_no)
-        if None in [access_code_prefix, access_code_series, access_code_no]:
-            access_code = None
-        credentials = None
-
-        if access_code and jamb_reg_no:
-            credentials = JAMBApplicantCredentials(
-                access_code, jamb_reg_no)
-        elif access_code:
-            credentials = ApplicantCredentials(access_code)
-        elif not sessionData:
-            return None
-        sessionData = session[
-            'zope.pluggableauth.browserplugins']
-        if credentials:
-            sessionData['credentials'] = credentials
-        else:
-            credentials = sessionData.get('credentials', None)
-        if not credentials:
-            return None
-        if not IApplicantSessionCredentials.providedBy(credentials):
-            # If credentials were stored in session from another
-            # credentials plugin then we cannot make assumptions about
-            # its structure.
-            return None
-        if not IJAMBApplicantSessionCredentials.providedBy(credentials):
-            # Entered credentials are ordinary applicant credentials,
-            # not JAMB-screened applicant credentials
-            return {'accesscode': credentials.getAccessCode()}
-        return {'accesscode': credentials.getAccessCode(),
-                'jambregno': credentials.getJAMBRegNo()}
-
-
-
-class ApplicantsAuthenticatorPlugin(grok.GlobalUtility):
-    """Authenticate applicants.
-
-    XXX: This plugin currently accepts any input and authenticates the
-    user as applicant.
-    """
-    grok.provides(IAuthenticatorPlugin)
-    grok.name('applicants')
-
-    def authenticateCredentials(self, credentials):
-        if not isinstance(credentials, dict):
-            return None
-        accesscode = credentials.get('accesscode', None)
-        jambregno = credentials.get('jambregno', None)
-        if accesscode is None:
-            return None
-        applicant_data, ac = get_applicant_data(jambregno, accesscode)
-        appl_ac = getattr(applicant_data, 'access_code', None)
-        #print "AUTH", accesscode, jambregno
-        if ac is None:
-            return None
-        if jambregno is not None and applicant_data is None:
-            return None
-        if ac.invalidation_date is not None and appl_ac != ac.representation:
-            return None
-        if appl_ac is not None and appl_ac != ac.representation:
-            return None
-        return ApplicantPrincipalInfo(accesscode, jambregno)
-
-    def principalInfo(self, id):
-        """Returns an IPrincipalInfo object for the specified principal id.
-
-        This method is used by the stadard PAU to lookup for instance
-        groups. If a principal belongs to a group, the group is looked
-        up by the id.  Currently we always return ``None``,
-        indicating, that the principal could not be found. This also
-        means, that is has no effect if applicant users belong to a
-        certain group. They can not gain extra-permissions this way.
-        """
-        return None
-
-def principal_id(access_code, jamb_reg_no=None):
-    """Get a principal ID for applicants.
-
-    We need unique principal ids for appliants. As access codes must
-    be unique we simply return them.
-    """
-    return access_code
Index: main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/tests/test_authentication.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/tests/test_authentication.py	(revision 5759)
+++ 	(revision )
@@ -1,347 +1,0 @@
-##
-## test_authentication.py
-## Login : <uli@pu.smp.net>
-## Started on  Fri Aug 20 08:18:58 2010 Uli Fouquet
-## $Id$
-## 
-## Copyright (C) 2010 Uli Fouquet
-## 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 grok
-import unittest
-from zope.component import provideAdapter
-from zope.component.hooks import setSite
-from zope.interface import verify
-from zope.pluggableauth.interfaces import  IAuthenticatedPrincipalFactory
-from zope.publisher.browser import TestRequest
-from zope.publisher.interfaces import IRequest
-from zope.session.session import RAMSessionDataContainer
-from zope.session.interfaces import ISession
-from zope.site import LocalSiteManager
-from zope.site.folder import Folder
-from zope.site.testing import siteSetUp, siteTearDown
-from zope.testing import cleanup
-from waeup.sirp.applicants.jambtables.authentication import (
-    ApplicantsAuthenticatorPlugin, WAeUPApplicantCredentialsPlugin,
-    ApplicantCredentials, AuthenticatedApplicantPrincipalFactory,
-    ApplicantPrincipalInfo, ApplicantPrincipal)
-from waeup.sirp.applicants.jambtables.interfaces import(
-    IJAMBApplicantSessionCredentials,)
-
-class FakeBatch(dict):
-    def getAccessCode(self, id):
-        return self.get(id)
-
-class FakeAccessCode(object):
-    def __init__(self, repr, inv_date=None):
-        self.invalidation_date = inv_date
-        self.representation = repr
-
-class FakeInvAccessCode(object):
-    invalidation_date = True
-
-class FakeApplication(object):
-    def __init__(self, ac=None):
-        self.access_code = ac
-
-class FakeSite(grok.Application, grok.Container):
-    def __init__(self):
-        super(FakeSite, self).__init__()
-        self['applications'] = {
-            'APP-12345': FakeApplication('APP-12345'),
-            'APP-54321': FakeApplication('APP-54321'),
-            'APP-22222': FakeApplication('APP-OTHER'),
-            'APP-44444': FakeApplication(),
-            'APP-55555': FakeApplication('APP-OTHER'),
-            'JAMB1': FakeApplication(),
-            'JAMB2': FakeApplication('APP-12345'),
-            'JAMB3': FakeApplication('APP-54321'),
-            'JAMB4': FakeApplication('APP-44444'),
-            }
-        self['accesscodes'] = {
-            'APP': FakeBatch({
-                    'APP-12345': FakeAccessCode('APP-12345'),
-                    'APP-54321': FakeAccessCode('APP-54321', True),
-                    'APP-11111': FakeAccessCode('APP-11111'),
-                    'APP-22222': FakeAccessCode('APP-22222'),
-                    'APP-33333': FakeAccessCode('APP-33333', True),
-                    'APP-44444': FakeAccessCode('APP-44444', True),
-                    'APP-55555': FakeAccessCode('APP-55555', True),
-                    })
-            }
-
-class AuthenticatorPluginTest(unittest.TestCase):
-
-    def setUp(self):
-        self.root = Folder()
-        siteSetUp(self.root)
-        self.app = FakeSite() #grok.Application()
-        self.root['app'] = self.app
-        self.app.setSiteManager(LocalSiteManager(self.app))
-        self.plugin = ApplicantsAuthenticatorPlugin()
-        setSite(self.app)
-        return
-
-    def tearDown(self):
-        siteTearDown()
-        return
-
-    def test_invalid_credentials(self):
-        result = self.plugin.authenticateCredentials('not-a-dict')
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode=None, foo='blah'))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(jambregno=None, foo='blah'))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode=None, jambregno=None))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='Nonsense', jambregno='Nonsense'))
-        assert result is None
-
-        # The nine possible cases, where formal correct authentication
-        # data is not valid:
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-22222'))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-33333'))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-44444'))
-        assert result is None
-        
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-55555'))
-        assert result is None
-        
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-12345', jambregno='JAMB-NOT-EXISTENT'))
-        assert result is None
-        
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-12345', jambregno='JAMB3'))
-        assert result is None
-        
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-54321', jambregno='JAMB-NOT-EXISTENT'))
-        assert result is None
-        
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-54321', jambregno='JAMB1'))
-        assert result is None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-54321', jambregno='JAMB2'))
-        assert result is None
-        
-        return
-
-    def test_valid_credentials(self):
-        """The six different cases where we allow login.
-
-        All other combinations should be forbidden.
-        """
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-11111'))
-        assert result is not None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-12345'))
-        assert result is not None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-54321'))
-        assert result is not None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-12345', jambregno='JAMB1'))
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-12345', jambregno='JAMB2'))
-        assert result is not None
-
-        result = self.plugin.authenticateCredentials(
-            dict(accesscode='APP-54321', jambregno='JAMB3'))
-        assert result is not None
-
-        # check the `principalInfo` method of authenticator
-        # plugin. This is only here to satisfy the coverage report.
-        assert self.plugin.principalInfo('not-an-id') is None
-        return
-
-session_data = {
-    'zope.pluggableauth.browserplugins': {}
-    }
-
-class FakeSession(dict):
-    def __init__(self, request):
-        pass
-
-    def get(self, key, default=None):
-        return self.__getitem__(key, default)
-    
-    def __getitem__(self, key, default=None):
-        return session_data.get(key, default)
-
-    def __setitem__(self, key, value):
-        session_data[key] = value
-        return
-
-class CredentialsPluginTest(unittest.TestCase):
-
-    def setUp(self):
-        self.request = TestRequest()
-        provideAdapter(FakeSession, (IRequest,), ISession)
-        self.plugin = WAeUPApplicantCredentialsPlugin()
-        self.full_request = TestRequest()
-        session_data['zope.pluggableauth.browserplugins'] = {}
-        return
-
-    def tearDown(self):
-        cleanup.tearDown()
-        return
-
-    def filled_request(self, form_dict):
-        request = TestRequest()
-        for key, value in form_dict.items():
-            request.form[key] = value
-        return request
-
-    def test_extractCredentials_invalid(self):
-        result = self.plugin.extractCredentials('not-a-request')
-        assert result is None
-        return
-
-    def test_extractCredentials_empty(self):
-        result = self.plugin.extractCredentials(self.request)
-        assert result is None
-        return
-
-    def test_extractCredentials_full_set(self):
-        request = self.filled_request({
-                'form.prefix': 'APP',
-                'form.ac_series': '1',
-                'form.ac_number': '1234567890',
-                'form.jamb_reg_no': 'JAMB_NUMBER',
-                })
-        result = self.plugin.extractCredentials(request)
-        self.assertEqual(result, {'jambregno': 'JAMB_NUMBER',
-                                  'accesscode': 'APP-1-1234567890'})
-        return
-
-    def test_extractCredentials_accesscode_only(self):
-        request = self.filled_request({
-                'form.prefix': 'APP',
-                'form.ac_series': '1',
-                'form.ac_number': '1234567890',
-                })
-        result = self.plugin.extractCredentials(request)
-        self.assertEqual(result, {'accesscode': 'APP-1-1234567890'})
-        return
-
-    def test_extractCredentials_from_empty_session(self):
-        session_data['zope.pluggableauth.browserplugins']['credentials'] = None
-        result = self.plugin.extractCredentials(self.request)
-        assert result is None
-        return
-
-    def test_extractCredentials_from_nonempty_session(self):
-        credentials = ApplicantCredentials('APP-1-12345')
-        session_data['zope.pluggableauth.browserplugins'][
-            'credentials'] = credentials
-        result = self.plugin.extractCredentials(self.request)
-        self.assertEqual(result, {'accesscode': 'APP-1-12345'})
-        return
-
-
-class ApplicantCredentialsTest(unittest.TestCase):
-
-    def setUp(self):
-        self.credentials = ApplicantCredentials('SOME_ACCESSCODE')
-        return
-
-    def tearDown(self):
-        return
-
-    def test_methods(self):
-        self.assertEqual(self.credentials.getAccessCode(), 'SOME_ACCESSCODE')
-        assert self.credentials.getLogin() is None
-        assert self.credentials.getPassword() is None
-        return
-
-class FakePluggableAuth(object):
-    prefix = 'foo'
-
-class PrincipalFactoryTest(unittest.TestCase):
-
-    def setUp(self):
-        self.info = ApplicantPrincipalInfo('APP-1-1234567890')
-        return
-
-    def tearDown(self):
-        pass
-
-    def test_principalFactory_interface(self):
-        verify.verifyClass(IAuthenticatedPrincipalFactory,
-                           AuthenticatedApplicantPrincipalFactory
-                           )
-        return
-
-    def test_principalFactory_create(self):
-        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
-
-        assert factory.info is self.info
-        assert factory.request is None
-        return
-
-    def test_principalFactory_call_w_prefix(self):
-        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
-        principal = factory(FakePluggableAuth())
-
-        assert isinstance(principal, ApplicantPrincipal)
-        self.assertEqual(principal.__repr__(),
-                         "ApplicantPrincipal('foo.APP-1-1234567890')")
-        self.assertEqual(principal.id, 'foo.APP-1-1234567890')
-        return
-
-    def test_principalFactory_call_wo_prefix(self):
-        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
-        fake_auth = FakePluggableAuth()
-        fake_auth.prefix = None
-        principal = factory(fake_auth)
-        self.assertEqual(principal.id, 'APP-1-1234567890')
-        return
-        
-def test_suite():
-    suite = unittest.TestSuite()
-    for testcase in [
-        AuthenticatorPluginTest, CredentialsPluginTest,
-        ApplicantCredentialsTest, PrincipalFactoryTest,
-        ]:
-        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
-                testcase
-                )
-        )
-    return suite
