def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='luke@...', password='******' ) self.factory = RequestFactory() self.adapter = get_adapter() now = timezone.now() self.authlink = AuthLink.objects.create( user = self.user, ipaddress = '177.139.233.133', created = now, expires = now + datetime.timedelta(seconds=settings.AUTHLINK_TTL_SECONDS), url = '/some/url' )
def setUp(self): self.user = get_user_model().objects.create_user(username="******", email="luke@...", password="******") self.factory = RequestFactory() self.adapter = get_adapter() now = timezone.now() self.authlink = AuthLink.objects.create( user=self.user, ipaddress="177.139.233.133", created=now, expires=now + datetime.timedelta(seconds=settings.AUTHLINK_TTL_SECONDS), url="/some/url", )
from authlink.adapter import get_adapter from django.contrib.auth import BACKEND_SESSION_KEY from django.core.exceptions import ImproperlyConfigured adapter = get_adapter() class AuthLinkWhitelistMiddleware(object): """ Only allow access to whitelisted URLs for sessions that are established using the authlink authentication mechanism. Note: if you want the user to be able to access everything then don't use this middleware! """ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if not hasattr(request, 'session'): raise ImproperlyConfigured( 'Please ensure you place AuthLinkWhitelistMiddleware ' \ 'middleware after your session middlware.' ) backend = request.session.get(BACKEND_SESSION_KEY) if backend and backend == 'authlink.auth_backends.AuthLinkBackend': if not adapter.in_url_whitelist(request.path): return adapter.get_whitelist_failure_response(request) return self.get_response(request)
def test_get_adapter_configurable(self): adapter = get_adapter() self.assertEqual(TestAdapter, adapter.__class__)
def test_get_adapter_configurable(self): adapter = get_adapter() self.assertEqual(TestAdapter, adapter.__class__)
from django.contrib.auth import BACKEND_SESSION_KEY from django.core.exceptions import ImproperlyConfigured from authlink.adapter import get_adapter adapter = get_adapter() class AuthLinkWhitelistMiddleware(object): """ Only allow access to whitelisted URLs for sessions that are established using the authlink authentication mechanism. Note: if you want the user to be able to access everything then don't use this middleware! """ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if not hasattr(request, "session"): raise ImproperlyConfigured( "Please ensure you place AuthLinkWhitelistMiddleware " "middleware after your session middlware.") backend = request.session.get(BACKEND_SESSION_KEY) if backend and backend == "authlink.auth_backends.AuthLinkBackend": if not adapter.in_url_whitelist(request.path): return adapter.get_whitelist_failure_response(request) return self.get_response(request)