1. Gregory Petukhov
  2. django-urlauth

Commits

Gregory Petukhov  committed 890b38f

Add signals

  • Participants
  • Parent commits ce0d873
  • Branches default

Comments (0)

Files changed (4)

File README

View file
 
 Django-urlauth allows you to build link which will authenticate the user which opens it. This is acheaved with special code in the querystring part of the link. Example of link http://somedomain.com/path/?authkey=SOME_KEY.
 
-Example of usage:
+Usage example:
 
 >>> from urlauth.util import wrap_url
 >>> wrap_url('http://domain.com/path/', uid=10)
 http://domain.com/path/?authkey=404787ca65ea256e120af7e5e3c217170b1755ad'
+
+
+Extra data
+==========
+
+It is possible to store in AuthKey instance extra data. All arguments of wrap_url function except uid and expired will be saved in the key record and will be accessible later via export_data method.
+
+Usage example:
+
+>>> from urlauth.util import wrap_url, load_key
+>>> url = wrap_url('http://google.com', uid=13, foo='bar', baz=100)
+>>> url
+'http://google.com?authkey=b68b4e38c0356c3eeb7a7ec6849a2dfc86902a10'
+>>> hash = url.split('=')[1]
+>>> key = load_key(hash)
+>>> key.export_data()
+{u'foo': u'bar', u'baz': 100}
+
+
+Signals
+=======
+
+urlauth middleware generates two signals:
+ * `key_loaded` - when AuthKey instance was found for hash in url
+ * `key_processed` - when User was found for the user ID stored in the key

File urlauth/middleware.py

View file
 from django.conf import settings
 
 from urlauth.util import load_key, InvalidKey
+from urlauth.models import AuthKey
+from urlauth.signals import key_processed, key_loaded
 
 
 class AuthKeyMiddleware(object):
         except InvalidKey:
             return
 
+        key_loaded.send(sender=AuthKey, instance=key)
+
         try:
             user = User.objects.get(pk=key.uid)
         except User.DoesNotExist:
         if user.is_active:
             user.backend = 'django.contrib.auth.backends.ModelBackend'
             auth.login(request, user)    
+
+        key_processed.send(sender=AuthKey, instance=key)

File urlauth/signals.py

View file
+from django.dispatch import Signal
+
+key_loaded = Signal(providing_args=['instance'])
+key_processed = Signal(providing_args=['instance'])

File urlauth/tests.py

View file
 from django.conf import settings
 from django.contrib.auth import login, authenticate
 from django.contrib.auth.models import User
+import django.dispatch
 
 from urlauth.util import wrap_url, load_key, InvalidKey
 from urlauth.models import AuthKey
+from urlauth.signals import key_processed, key_loaded
 
 class UrlauthTestCase(TestCase):
     urls = 'urlauth.tests'
 
 
 class AuthKeyMiddlewareTestCase(UrlauthTestCase):
+    test_url = '/urlauth_test_view/'
 
-    def testActivation(self): 
-        def process_url(url, **kwargs):
-           url = wrap_url(url, **kwargs)
-           return url.split('?')[0], cgi.parse_qs(url.split('?')[1])
+    def process_url(self, url, **kwargs):
+       url = wrap_url(url, **kwargs)
+       return url.split('?')[0], cgi.parse_qs(url.split('?')[1])
 
-        test_url = '/urlauth_test_view/'
+    def test_activation(self): 
         expired = datetime.now() + timedelta(days=1)
-        resp = self.client.get(test_url)
+        resp = self.client.get(self.test_url)
 
         # Guest is not authenticated
         self.client.logout()
 
         ## Simple authorization
         self.client.logout()
-        url, args = process_url(test_url, uid=self.user.pk, expired=expired)
+        url, args = self.process_url(self.test_url, uid=self.user.pk, expired=expired)
         resp = self.client.get(url, args)
         self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
 
         ## Baned user can't authorize
         self.client.logout()
-        url, args = process_url(test_url, uid=self.ban_user.pk, expired=expired)
+        url, args = self.process_url(self.test_url, uid=self.ban_user.pk, expired=expired)
         resp = self.client.get(url, args)
         self.assertFalse('_auth_user_id' in self.client.session)
 
         ## Expired auth key does not work
         self.client.logout()
         expired = datetime.now() - timedelta(seconds=1)
-        url, args = process_url(url, uid=self.user.pk, expired=expired)
+        url, args = self.process_url(url, uid=self.user.pk, expired=expired)
         resp = self.client.get(url, args)
         self.assertFalse('_auth_user_id' in self.client.session)
 
 
+    def test_signals(self):
+        logs = []
+
+        handler1 = lambda **k: logs.append('L')
+        handler2 = lambda **k: logs.append('P')
+
+        key_processed.connect(handler1)
+        key_processed.connect(handler2)
+
+        self.assertEqual(0, len(logs))
+        self.client.logout()
+        expired = datetime.now() + timedelta(days=1)
+        url, args = self.process_url(self.test_url, uid=self.user.pk,
+                                     expired=expired)
+        resp = self.client.get(url, args)
+        self.assertEqual(2, len(logs))
+
+
+
 def test_view(request):
     return HttpResponse('')