diff --git a/src/coulobre/hosting/invitations.py b/src/coulobre/hosting/invitations.py index e888c58..502352e 100644 --- a/src/coulobre/hosting/invitations.py +++ b/src/coulobre/hosting/invitations.py @@ -6,12 +6,12 @@ from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.forms import UsernameField from django.contrib.auth.mixins import AccessMixin -from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.contrib.auth.views import ( INTERNAL_RESET_SESSION_TOKEN, PasswordResetConfirmView, ) from django.core.mail import send_mail +from django.core.signing import BadSignature, Signer from django.db import transaction from django.shortcuts import get_object_or_404, redirect from django.template.loader import render_to_string @@ -70,22 +70,32 @@ def may_accept_invitation(user): ) -class InvitationToken(PasswordResetTokenGenerator): - key_salt = "coulobre.panel-invitation" +class InvitationToken: + @staticmethod + def payload(user): + revision = user.panel_invitation.revision + return f"{user.pk}:{revision}:{user.password}:{user.email}" - def check_token(self, user, token): - if user is None or not may_accept_invitation(user): - return False - invitation = getattr(user, "panel_invitation", None) - return ( - invitation is not None - and invitation.created_at + INVITATION_AGE > timezone.now() - and super().check_token(user, token) + def make_token(self, user): + return Signer(salt="coulobre.panel-invitation").signature( + self.payload(user) ) - def _make_hash_value(self, user, timestamp): - original = super()._make_hash_value(user, timestamp) - return f"{original}{user.panel_invitation.revision}" + def check_token(self, user, token): + if user is None or not token or not may_accept_invitation(user): + return False + invitation = getattr(user, "panel_invitation", None) + if ( + invitation is None + or invitation.created_at + INVITATION_AGE <= timezone.now() + ): + return False + signer = Signer(salt="coulobre.panel-invitation") + payload = self.payload(user) + try: + return signer.unsign(payload + signer.sep + token) == payload + except BadSignature: + return False invitation_token = InvitationToken() diff --git a/src/coulobre/hosting/test_panel_authentication.py b/src/coulobre/hosting/test_panel_authentication.py index b4361bc..e9629ba 100644 --- a/src/coulobre/hosting/test_panel_authentication.py +++ b/src/coulobre/hosting/test_panel_authentication.py @@ -14,7 +14,7 @@ from django_otp.oath import TOTP, totp from django_otp.plugins.otp_email.models import EmailDevice from django_otp.plugins.otp_totp.models import TOTPDevice -from .invitations import deliver_invitation, queue_invitation +from .invitations import deliver_invitation, invitation_token, queue_invitation from .models import Customer, HostingAccount, PanelRecoveryCode, Reseller from .sign_in_limits import client_address from .tasks import send_customer_password_reset @@ -133,6 +133,26 @@ class PanelAuthenticationTests(TestCase): next(line for line in body.splitlines() if "/welcome/" in line) ).path + def test_invitation_uses_its_own_expiration(self): + user = get_user_model().objects.create_user( + "welcome", email="welcome@example.test" + ) + Customer.objects.create(user=user) + invitation = queue_invitation(user) + token = invitation_token.make_token(user) + for hours, valid in ((47, True), (48, False)): + with ( + self.subTest(hours=hours), + patch( + "coulobre.hosting.invitations.timezone.now", + return_value=invitation.created_at + + timedelta(hours=hours), + ), + ): + self.assertEqual( + invitation_token.check_token(user, token), valid + ) + def test_first_password_login_enrolls_and_later_logins_require_a_factor( self, ):