def test_constructor_with_classes(self): conf = { 'delay_401': True } m = middleware.create_middleware(interfaces.Identifies, interfaces.Authenticates, **conf) self.assertTrue(isinstance(m, middleware.Middleware)) self.assertTrue(isinstance(m.identifiers[0], interfaces.Identifies)) self.assertTrue(isinstance(m.authenticators[0], interfaces.Authenticates))
def test_constructor_with_classes(self): conf = {'delay_401': True} m = middleware.create_middleware(interfaces.Identifies, interfaces.Authenticates, interfaces.Authorizes, **conf) self.assertTrue(isinstance(m, middleware.Middleware)) self.assertTrue(isinstance(m.identifiers[0], interfaces.Identifies)) self.assertTrue( isinstance(m.authenticators[0], interfaces.Authenticates)) self.assertTrue(isinstance(m.authorizer, interfaces.Authorizes))
def __init__(self): """ Constructor for the authentication middleware. """ authenticator = Authenticator(drupal_auth, system_authenticator) authorizer = Authorizer(authorize, SystemAuthorize().authorize) self._auth_middleware = middleware.create_middleware( identify_with=[Identifier], authenticate_with=authenticator, authorize_with=authorizer) self._auth_middleware.raise_401_no_identity = self._raise_no_ident self._auth_middleware.raise_401_fail_authenticate = self._raise_failed
middlewares = [] passwds = settings('PASSWDS') if passwds: def authenticate(identity): try: return identity.key == passwds[identity.login] except KeyError: return False def authorize(identity, request_action): return True auth = middleware.create_middleware( identify_with=[basicauth.Identifier], authenticate_with=external.Authenticator( external_authn_callable=authenticate), authorize_with=external.Authorizer( external_authz_callable=authorize) ) middlewares.append(auth) crawled = Crawled(scheduler) request = Request(scheduler, settings('DEFAULT_REQS')) app = application = falcon.API(before=middlewares) app.add_route('/crawled', crawled) app.add_route('/request', request) key_path = settings('SSL_KEY') cert_path = settings('SSL_CERT') if (key_path and not cert_path) or (cert_path and not key_path):
def test_wrong_args(self): i = mock.MagicMock(spec=interfaces.Identifies) a = mock.MagicMock(spec=interfaces.Authenticates) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(None, None) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(i, None) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(None, a) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(dict(i=i), None) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(None, dict(a=a)) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(a, i) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(interfaces.Authenticates, interfaces.Identifies) with testtools.ExpectedException(exc.BadConfiguration): middleware.create_middleware(interfaces.Identifies, interfaces.Authenticates, interfaces.Identifies)
middlewares = [] passwds = settings('PASSWDS') if passwds: def authenticate(identity): try: return identity.key == passwds[identity.login] except KeyError: return False def authorize(identity, request_action): return True auth = middleware.create_middleware( identify_with=[basicauth.Identifier], authenticate_with=external.Authenticator( external_authn_callable=authenticate), authorize_with=external.Authorizer( external_authz_callable=authorize)) middlewares.append(auth) crawled = Crawled(scheduler) request = Request(scheduler, settings('DEFAULT_REQS')) app = application = falcon.API(before=middlewares) app.add_route('/crawled', crawled) app.add_route('/request', request) key_path = settings('SSL_KEY') cert_path = settings('SSL_CERT') if (key_path and not cert_path) or (cert_path and not key_path): print(