-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Labels
Description
Write unit tests for...
- nonce_is_valid function
django-siwe-auth/siwe_auth/backend.py
Lines 28 to 39 in 12c6d95
def _nonce_is_valid(nonce: str) -> bool: """ Check if given nonce exists and has not yet expired. :param nonce: The nonce string to validate. :return: True if valid else False. """ n = Nonce.objects.get(value=nonce) is_valid = False if n is not None and n.expiration > datetime.datetime.now(tz=pytz.UTC): is_valid = True n.delete() return is_valid - Nonce model itself
django-siwe-auth/siwe_auth/models.py
Lines 74 to 79 in 12c6d95
class Nonce(models.Model): value = models.CharField(max_length=24, primary_key=True) expiration = models.DateTimeField() def __str__(self): return self.value - Nonce scrubbing logic (with freezegun?)
django-siwe-auth/siwe_auth/views.py
Lines 53 to 68 in 12c6d95
@ratelimit(key='ip', rate='5/m') @require_http_methods(["GET"]) def nonce(request): now = datetime.now(tz=pytz.UTC) _scrub_nonce() n = Nonce(value=secrets.token_hex(12), expiration=now + timedelta(hours=12)) n.save() return JsonResponse({"nonce": n.value}) def _scrub_nonce(): # Delete all expired nonce's for n in Nonce.objects.filter(expiration__lte=datetime.now(tz=pytz.UTC)): n.delete()