-
Notifications
You must be signed in to change notification settings - Fork 403
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
1 parent
ecb9104
commit 5e47fb2
Showing
4 changed files
with
89 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
from typing import Any, Dict | ||
|
||
from django.conf import settings | ||
from pyotp import TOTP | ||
from rest_framework.response import Response | ||
from trench.backends.application import ApplicationMessageDispatcher | ||
from trench.models import MFAMethod | ||
|
||
|
||
class CustomApplicationBackend: | ||
def __init__(self, mfa_method: MFAMethod, config: Dict[str, Any]) -> None: | ||
self._mfa_method = mfa_method | ||
self._config = config | ||
self._totp = TOTP(self._mfa_method.secret) | ||
|
||
class CustomApplicationBackend(ApplicationMessageDispatcher): | ||
def dispatch_message(self): | ||
original_message = super().dispatch_message() | ||
qr_link = self._totp.provisioning_uri( | ||
self._mfa_method.user.email, settings.TRENCH_AUTH["APPLICATION_ISSUER_NAME"] | ||
) | ||
data = { | ||
"qr_link": original_message.data["details"], | ||
"qr_link": qr_link, | ||
"secret": self._mfa_method.secret, | ||
} | ||
return Response(data) | ||
|
||
def confirm_activation(self, code: str) -> None: | ||
pass | ||
|
||
def validate_confirmation_code(self, code: str) -> bool: | ||
return self.validate_code(code) | ||
|
||
def validate_code(self, code: str) -> bool: | ||
return self._get_otp().verify(otp=code, valid_window=20) | ||
return self._totp.verify(otp=code, valid_window=20) |
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,57 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth.tokens import PasswordResetTokenGenerator | ||
from django.utils.crypto import constant_time_compare, salted_hmac | ||
from django.utils.http import base36_to_int, int_to_base36 | ||
|
||
|
||
class UserTokenGenerator(PasswordResetTokenGenerator): | ||
""" | ||
Custom token generator: | ||
- user pk in token | ||
- expires after 15 minutes | ||
- longer hash (40 instead of 20) | ||
""" | ||
|
||
KEY_SALT = "django.contrib.auth.tokens.PasswordResetTokenGenerator" | ||
SECRET = settings.SECRET_KEY | ||
EXPIRY_TIME = 60 * 15 | ||
|
||
def make_token(self, user: User) -> str: | ||
return self._make_token_with_timestamp(user, int(datetime.now().timestamp())) | ||
|
||
def check_token(self, user: User, token: str) -> Optional[User]: | ||
user_model = get_user_model() | ||
if not token: | ||
return None | ||
try: | ||
token = str(token) | ||
user_pk, ts_b36, token_hash = token.rsplit("-", 2) | ||
ts = base36_to_int(ts_b36) | ||
user = user_model._default_manager.get(pk=user_pk) | ||
except (ValueError, TypeError, user_model.DoesNotExist): | ||
return None | ||
|
||
if (datetime.now().timestamp() - ts) > self.EXPIRY_TIME: | ||
return None # pragma: no cover | ||
|
||
if not constant_time_compare(self._make_token_with_timestamp(user, ts), token): | ||
return None # pragma: no cover | ||
|
||
return user | ||
|
||
def _make_token_with_timestamp(self, user: User, timestamp: int, **kwargs) -> str: | ||
ts_b36 = int_to_base36(timestamp) | ||
token_hash = salted_hmac( | ||
self.KEY_SALT, | ||
self._make_hash_value(user, timestamp), | ||
secret=self.SECRET, | ||
).hexdigest() | ||
return f"{user.pk}-{ts_b36}-{token_hash}" | ||
|
||
|
||
user_token_generator = UserTokenGenerator() |
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