-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import typing as t | ||
from itertools import groupby | ||
|
||
from django.contrib.auth.hashers import check_password, make_password | ||
from django.core.exceptions import ValidationError | ||
from django.core.validators import MinLengthValidator | ||
from django.db import models | ||
|
||
from . import user | ||
|
||
|
||
class BackupToken(models.Model): | ||
max_count = 10 | ||
max_count_validation_error = ValidationError( | ||
f"Exceeded max count of {max_count}" | ||
) | ||
|
||
class Manager(models.Manager["BackupToken"]): | ||
def create(self, token: str, **kwargs): | ||
return super().create(token=make_password(token), **kwargs) | ||
|
||
def bulk_create( | ||
self, | ||
backup_tokens: t.List["BackupToken"], | ||
*args, | ||
**kwargs, | ||
): | ||
def key(backup_token: BackupToken): | ||
return backup_token.user.id | ||
|
||
backup_tokens.sort(key=key) | ||
for user_id, group in groupby(backup_tokens, key=key): | ||
if ( | ||
len(list(group)) | ||
+ BackupToken.objects.filter(user_id=user_id).count() | ||
> BackupToken.max_count | ||
): | ||
raise BackupToken.max_count_validation_error | ||
|
||
for backup_token in backup_tokens: | ||
backup_token.token = make_password(backup_token.token) | ||
|
||
return super().bulk_create(backup_tokens, *args, **kwargs) | ||
|
||
objects: Manager = Manager() | ||
|
||
user: "user.User" = models.ForeignKey( | ||
"user.User", | ||
related_name="backup_tokens", | ||
on_delete=models.CASCADE, | ||
) | ||
|
||
token = models.CharField( | ||
max_length=8, | ||
validators=[MinLengthValidator(8)], | ||
) | ||
|
||
class Meta: | ||
unique_together = ["user", "token"] | ||
|
||
def save(self, *args, **kwargs): | ||
if self.id is None: | ||
if ( | ||
BackupToken.objects.filter(user=self.user).count() | ||
>= BackupToken.max_count | ||
): | ||
raise BackupToken.max_count_validation_error | ||
|
||
return super().save(*args, **kwargs) | ||
|
||
def check_token(self, token: str): | ||
if check_password(token, self.token): | ||
self.delete() | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from django.contrib.auth.hashers import check_password | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
from django.utils.crypto import get_random_string | ||
|
||
from ...models import BackupToken, User | ||
|
||
|
||
class TestBackupToken(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.get(id=2) | ||
|
||
def test_bulk_create(self): | ||
token = get_random_string(8) | ||
backup_tokens = BackupToken.objects.bulk_create( | ||
[BackupToken(user=self.user, token=token)] | ||
) | ||
|
||
assert check_password(token, backup_tokens[0].token) | ||
with self.assertRaises(ValidationError): | ||
BackupToken.objects.bulk_create( | ||
[ | ||
BackupToken( | ||
user=self.user, | ||
token=get_random_string(8), | ||
) | ||
for _ in range(BackupToken.max_count) | ||
] | ||
) | ||
|
||
def test_create(self): | ||
token = get_random_string(8) | ||
backup_token = BackupToken.objects.create(user=self.user, token=token) | ||
|
||
assert check_password(token, backup_token.token) | ||
|
||
BackupToken.objects.bulk_create( | ||
[ | ||
BackupToken( | ||
user=self.user, | ||
token=get_random_string(8), | ||
) | ||
for _ in range(BackupToken.max_count - 1) | ||
] | ||
) | ||
|
||
with self.assertRaises(ValidationError): | ||
BackupToken.objects.create( | ||
user=self.user, | ||
token=get_random_string(8), | ||
) | ||
|
||
def test_check_token(self): | ||
token = get_random_string(8) | ||
backup_token = BackupToken.objects.create(user=self.user, token=token) | ||
|
||
assert backup_token.check_token(token) | ||
assert backup_token.id is None | ||
with self.assertRaises(BackupToken.DoesNotExist): | ||
BackupToken.objects.get( | ||
user=backup_token.user, | ||
token=backup_token.token, | ||
) |