This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
models.py
51 lines (41 loc) · 1.71 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
import hashlib
import os
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
class LoginCode(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='login_codes',
editable=False, verbose_name=_('user'), on_delete=models.CASCADE)
code = models.CharField(max_length=20, editable=False, verbose_name=_('code'))
timestamp = models.DateTimeField(editable=False)
next = models.TextField(editable=False, blank=True)
def __str__(self):
return "%s - %s" % (self.user, self.timestamp)
def save(self, *args, **kwargs):
self.timestamp = timezone.now()
if not self.next:
self.next = '/'
super(LoginCode, self).save(*args, **kwargs)
@classmethod
def create_code_for_user(cls, user, next=None):
if not user.is_active:
return None
code = cls.generate_code(length=getattr(settings, 'NOPASSWORD_CODE_LENGTH', 20))
login_code = LoginCode(user=user, code=code)
if next is not None:
login_code.next = next
login_code.save()
return login_code
@classmethod
def generate_code(cls, length=20):
hash_algorithm = getattr(settings, 'NOPASSWORD_HASH_ALGORITHM', 'sha256')
m = getattr(hashlib, hash_algorithm)()
m.update(getattr(settings, 'SECRET_KEY', None).encode('utf-8'))
m.update(os.urandom(16))
if getattr(settings, 'NOPASSWORD_NUMERIC_CODES', False):
hashed = str(int(m.hexdigest(), 16))[-length:]
else:
hashed = m.hexdigest()[:length]
return hashed