aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/authtoken/models.py
diff options
context:
space:
mode:
authorTom Christie2012-09-12 20:39:22 +0100
committerTom Christie2012-09-12 20:39:22 +0100
commit003a65f0e094e59b5462fcd0607bf290d80cc5aa (patch)
treec514790099c155e18897012cc4599a60f191832e /djangorestframework/authtoken/models.py
parentdac4cb9e8bf107f407ed8754bbef0ce97e79beb2 (diff)
downloaddjango-rest-framework-003a65f0e094e59b5462fcd0607bf290d80cc5aa.tar.bz2
Tweaks to Token auth
Diffstat (limited to 'djangorestframework/authtoken/models.py')
-rw-r--r--djangorestframework/authtoken/models.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/djangorestframework/authtoken/models.py b/djangorestframework/authtoken/models.py
new file mode 100644
index 00000000..fd47e6c7
--- /dev/null
+++ b/djangorestframework/authtoken/models.py
@@ -0,0 +1,23 @@
+import uuid
+import hmac
+from hashlib import sha1
+from django.db import models
+
+
+class Token(models.Model):
+ """
+ The default authorization token model.
+ """
+ key = models.CharField(max_length=40, primary_key=True)
+ user = models.ForeignKey('auth.User')
+ revoked = models.BooleanField(default=False)
+ created = models.DateTimeField(auto_now_add=True)
+
+ def save(self, *args, **kwargs):
+ if not self.key:
+ self.key = self.generate_key()
+ return super(Token, self).save(*args, **kwargs)
+
+ def generate_key(self):
+ unique = str(uuid.uuid4())
+ return hmac.new(unique, digestmod=sha1).hexdigest()