Changeset 10475 for main/waeup.cas


Ignore:
Timestamp:
8 Aug 2013, 10:09:00 (11 years ago)
Author:
uli
Message:

Check backend values when initializing Kofa authenticator.

Location:
main/waeup.cas/trunk/waeup/cas
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.cas/trunk/waeup/cas/authenticators.py

    r10474 r10475  
    11"""Components that do the real authentication stuff.
    22"""
     3import re
    34from pkg_resources import iter_entry_points
    45
     
    117118    name = 'kofa1'
    118119
    119     def __init__(self, auth_backends=None):
    120         self.backends = auth_backends
     120    def __init__(self, auth_backends="{}"):
     121        try:
     122            self.backends = eval(auth_backends)
     123        except:
     124            raise ValueError('auth_backends must be a '
     125                             'valid Python expression.')
     126        self._check_options()
     127
     128    def _check_options(self):
     129        if not isinstance(self.backends, dict):
     130            raise ValueError('Backends must be configured as dicts.')
     131        for key, val in self.backends.items():
     132            if not isinstance(val, dict):
     133                raise ValueError(
     134                    'Backend %s: config must be a dict' % key)
     135            if not 'url' in val:
     136                raise ValueError(
     137                    'Backend %s: config must contain an `url` key.' % key)
     138            if not 'marker' in val:
     139                self.backends[key]['marker'] = '.+'
     140            try:
     141                re.compile(self.backends[key]['marker'])
     142            except:
     143                raise ValueError(
     144                    'Backend %s: marker must be a valid regular expr.:' % (
     145                        key,))
    121146
    122147    def check_credentials(self, username='', password=''):
  • main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py

    r10474 r10475  
    6262    inst1 = dict(
    6363        url = 'http://localhost:6666/app',
    64         marker = '^M[0-9]+',
     64        marker = '^M[0-9]+$',
    6565        )
    6666    )
     
    7676        # all options have sensible defaults
    7777        auth = KofaAuthenticator()
    78         assert auth.backends is None
     78        assert auth.backends == {}
    7979
    8080    def test_options(self):
    8181        # we can pass options
    82         auth = KofaAuthenticator(auth_backends=BACKENDS)
     82        auth = KofaAuthenticator(auth_backends=str(BACKENDS))
    8383        assert auth.backends == BACKENDS
     84        auth = KofaAuthenticator(auth_backends='{"foo": {"url": "bar"}}')
     85        assert auth.backends['foo']['marker'] == '.+'
     86        self.assertRaises(
     87            ValueError, KofaAuthenticator, auth_backends='not-a-py-expr')
     88        self.assertRaises(
     89            ValueError, KofaAuthenticator, auth_backends='"Not a dict"')
     90        self.assertRaises(
     91            ValueError, KofaAuthenticator,
     92            auth_backends='{"foo": "not-a-dict"}')
     93        self.assertRaises(
     94            ValueError, KofaAuthenticator,
     95            auth_backends='{"foo": {"no-url-key": "bar"}}')
     96        self.assertRaises(
     97            ValueError, KofaAuthenticator,
     98            auth_backends='{"foo": {"url": "bar", "marker": "inv_re)"}}')
    8499
    85100    def test_paste_deploy_options(self):
Note: See TracChangeset for help on using the changeset viewer.