Commits

Gregory Petukhov  committed e8fd0bb

Add onetime feature

  • Participants
  • Parent commits 890b38f

Comments (0)

Files changed (5)

 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
+
+Each signal provides key and request arguments for your handlers.
+
+
+Middleware
+==========
+
+Main function of urlauth middleware is to authenticate user. If onetime property of loaded key is True then key is not deleted and stored in the request.authkey.

File urlauth/middleware.py

         except InvalidKey:
             return
 
-        key_loaded.send(sender=AuthKey, instance=key)
+        key_loaded.send(sender=AuthKey, key=key, request=request)
 
         try:
             user = User.objects.get(pk=key.uid)
         except User.DoesNotExist:
-            return
+            pass
+        else:
+            if user.is_active:
+                user.backend = 'django.contrib.auth.backends.ModelBackend'
+                auth.login(request, user)    
 
-        if user.is_active:
-            user.backend = 'django.contrib.auth.backends.ModelBackend'
-            auth.login(request, user)    
+            key_processed.send(sender=AuthKey, key=key, request=request)
 
-        key_processed.send(sender=AuthKey, instance=key)
+        if key.onetime:
+            key.delete()
+        else:
+            request.authkey = key

File urlauth/models.py

         else:
             key.expired = datetime.now() + timedelta(seconds=settings.URLAUTH_AUTHKEY_TIMEOUT)
 
+        if 'onetime' in kwargs:
+            key.onetime = kwargs['onetime']
+
         source = '%s%d%d' % (settings.SECRET_KEY, time.time(), id({}))
         key.id = sha1(source).hexdigest()
         key.import_data(**kwargs)
     uid = models.PositiveIntegerField()
     expired = models.DateTimeField()
     created = models.DateTimeField(auto_now_add=True)
+    onetime = models.BooleanField(blank=True, default=True)
     data = models.TextField()
 
     objects = AuthKeyManager()

File urlauth/signals.py

 from django.dispatch import Signal
 
-key_loaded = Signal(providing_args=['instance'])
-key_processed = Signal(providing_args=['instance'])
+key_loaded = Signal(providing_args=['key', 'request'])
+key_processed = Signal(providing_args=['key', 'request'])

File urlauth/tests.py

         resp = self.client.get(url, args)
         self.assertEqual(2, len(logs))
 
+    def test_onetime_feature(self):
+        self.client.logout()
+        expired = datetime.now() + timedelta(days=1)
+        url, args = self.process_url(self.test_url, uid=self.user.pk,
+                                     expired=expired)
+        hash = args['authkey'][0]
+        self.assertTrue(AuthKey.objects.filter(id=hash).count())
+        resp = self.client.get(url, args)
+        self.assertFalse(AuthKey.objects.filter(id=hash).count())
+
+
+        self.client.logout()
+        expired = datetime.now() + timedelta(days=1)
+        url, args = self.process_url(self.test_url, uid=self.user.pk,
+                                     expired=expired, onetime=False)
+        hash = args['authkey'][0]
+        self.assertTrue(AuthKey.objects.filter(id=hash).count())
+        resp = self.client.get(url, args)
+        self.assertTrue(AuthKey.objects.filter(id=hash).count())
+
+
 
 
 def test_view(request):