Changeset 10478 for main/waeup.cas/trunk/waeup/cas
- Timestamp:
- 12 Aug 2013, 08:52:26 (11 years ago)
- Location:
- main/waeup.cas/trunk/waeup/cas
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.cas/trunk/waeup/cas/authenticators.py
r10475 r10478 3 3 import re 4 4 from pkg_resources import iter_entry_points 5 try: 6 import xmlrpclib # Python 2.x 7 except ImportError: # pragma: no cover 8 import xmlrpc.client as xmlrpclib # Python 3.x 5 9 6 10 … … 112 116 113 117 118 #: Regular expression matching a starting university marker like 119 #: the string 'MA-' in 'MA-M121212' or 'MA-' in 'MA-APP-13123' 120 RE_SCHOOL_MARKER = re.compile('^[^\-]+-') 121 122 114 123 class KofaAuthenticator(Authenticator): 115 124 """Authenticate against a running Kofa instance. … … 148 157 """Do the real check. 149 158 """ 150 return False, 'Not implemented.' 159 for backend_name, backend in self.backends.items(): 160 if not re.match(backend['marker'], username): 161 continue 162 # remove school marker 163 username = RE_SCHOOL_MARKER.sub('', username) 164 proxy = xmlrpclib.ServerProxy( 165 backend['url'], allow_none=True) 166 valid = proxy.check_credentials(username, password) 167 if valid is not None: 168 return (True, '') 169 return (False, 'Invalid username or password.') -
main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py
r10476 r10478 1 # Tests for waeup.cas.authentictors 1 2 import os 3 import threading 2 4 import unittest 3 5 from paste.deploy import loadapp 6 try: 7 from SimpleXMLRPCServer import SimpleXMLRPCServer # Python 2.x 8 except ImportError: 9 from xmlrpc.server import SimpleXMLRPCServer # Python 3.x 10 try: 11 import xmlrpclib # Python 2.x 12 except ImportError: 13 import xmlrpc.client as xmlrpclib # Python 3.x 4 14 from waeup.cas.authenticators import ( 5 15 get_all_authenticators, get_authenticator, filter_auth_opts, … … 59 69 60 70 61 BACKENDS = dict(71 BACKENDS1 = dict( 62 72 inst1=dict( 63 73 url='http://localhost:6666/app', 64 marker='^M[0-9]+$', 74 marker='^MA-', 75 ) 76 ) 77 78 BACKENDS2 = dict( 79 inst1=dict( 80 url='http://localhost:6666/', 81 marker='^SCHOOL1-', 65 82 ) 66 83 ) … … 81 98 def test_options(self): 82 99 # we can pass options 83 auth = KofaAuthenticator(auth_backends=str(BACKENDS ))84 assert auth.backends == BACKENDS 100 auth = KofaAuthenticator(auth_backends=str(BACKENDS1)) 101 assert auth.backends == BACKENDS1 85 102 auth = KofaAuthenticator(auth_backends='{"foo": {"url": "bar"}}') 86 103 assert auth.backends['foo']['marker'] == '.+' … … 108 125 assert isinstance(app.auth, KofaAuthenticator) 109 126 110 def DIStest_check_credentials(self): 127 128 class FakeKofaServer(SimpleXMLRPCServer): 129 # A fake Kofa server that provides only XMLRPC methods 130 131 allow_reuse_address = True 132 133 def __init__(self, *args, **kw): 134 kw.update(allow_none=True) 135 SimpleXMLRPCServer.__init__(self, *args, **kw) 136 self.register_function(self._check_credentials, 'check_credentials') 137 138 def _check_credentials(self, username, password): 139 # fake waeup.kofa check_credentials method. 140 # 141 # This method is supposed to mimic the behaviour of an 142 # original waeup.kofa check_credentials method. It returns a 143 # positive result for the credentials `bird`/`bebop`. 144 if username == 'bird' and password == 'bebop': 145 return {'id': 'bird', 'email': 'bird@gods.net', 146 'description': 'Mr. Charles Parker'} 147 return None 148 149 150 class XMLRPCFakeKofaTestCase(unittest.TestCase): 151 # A test case where a fake Kofa server is started before tests (and 152 # shut down afterwards). 153 154 server = None 155 th = None 156 157 @classmethod 158 def setUpClass(cls): 159 cls.server = FakeKofaServer(('localhost', 6666)) 160 cls.th = threading.Thread(target=cls.server.serve_forever) 161 cls.th.daemon = True 162 cls.th.start() 163 164 @classmethod 165 def tearDownClass(cls): 166 cls.server.shutdown() 167 cls.server.server_close() 168 169 170 class MyCase(XMLRPCFakeKofaTestCase): 171 172 def test_fake_kofa_works(self): 173 # make sure the local fake kofa works 174 proxy = xmlrpclib.ServerProxy("http://localhost:6666", allow_none=True) 175 result = proxy.check_credentials('bird', 'bebop') 176 assert result == { 177 'description': 'Mr. Charles Parker', 178 'email': 'bird@gods.net', 179 'id': 'bird'} 180 return 181 182 def test_check_credentials(self): 111 183 # we get real responses when querying Kofa instances 112 auth = KofaAuthenticator( )113 result1 = auth.check_credentials(' bird', 'bebop')184 auth = KofaAuthenticator(auth_backends=str(BACKENDS2)) 185 result1 = auth.check_credentials('SCHOOL1-bird', 'bebop') 114 186 assert result1 == (True, '') 115 result2 = auth.check_credentials(' foo', 'bar')187 result2 = auth.check_credentials('SCHOOL1-foo', 'bar') 116 188 assert result2 == (False, 'Invalid username or password.') 189 result3 = auth.check_credentials('SCHOOL2-bar', 'baz') 190 assert result3 == (False, 'Invalid username or password.')
Note: See TracChangeset for help on using the changeset viewer.