Skip to content

Commit

Permalink
Add mfa notification events
Browse files Browse the repository at this point in the history
  • Loading branch information
varun kumar committed Nov 19, 2023
1 parent b65ab8f commit 6cdbddf
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions allauth/mfa/adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.utils.translation import gettext_lazy as _

from allauth import app_settings as allauth_settings
from allauth.account.adapter import get_adapter as get_account_adapter
from allauth.account.utils import user_email, user_username
from allauth.core import context
from allauth.mfa import app_settings
Expand Down Expand Up @@ -62,6 +63,9 @@ def decrypt(self, encrypted_text: str) -> str:
text = encrypted_text
return text

def send_notification_mail(self, *args, **kwargs):
return get_account_adapter().send_notification_mail(*args, **kwargs)


def get_adapter():
return import_attribute(app_settings.ADAPTER)()
2 changes: 1 addition & 1 deletion allauth/mfa/migrations/0002_authenticator_timestamps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 3.2.22 on 2023-11-06 12:04

from django.db import migrations, models
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
5 changes: 1 addition & 4 deletions allauth/mfa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def wrap(self):
from allauth.mfa.recovery_codes import RecoveryCodes
from allauth.mfa.totp import TOTP

return {
self.Type.TOTP: TOTP,
self.Type.RECOVERY_CODES: RecoveryCodes,
}[
return {self.Type.TOTP: TOTP, self.Type.RECOVERY_CODES: RecoveryCodes,}[
self.type
](self)

Expand Down
7 changes: 7 additions & 0 deletions allauth/mfa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def form_valid(self, form):
adapter.add_message(
self.request, messages.SUCCESS, "mfa/messages/totp_activated.txt"
)
adapter.send_notification_mail(
"mfa/email/totp_activated", self.request.user, {}
)
return super().form_valid(form)


Expand Down Expand Up @@ -176,6 +179,9 @@ def form_valid(self, form):
adapter.add_message(
self.request, messages.SUCCESS, "mfa/messages/totp_deactivated.txt"
)
adapter.send_notification_mail(
"mfa/email/totp_deactivated", self.request.user, {}
)
return super().form_valid(form)


Expand All @@ -200,6 +206,7 @@ def form_valid(self, form):
signals.authenticator_reset.send(
sender=Authenticator, user=self.request.user, authenticator=rc_auth.instance
)
adapter.send_notification_mail("mfa/email/totp_reset", self.request.user, {})
return super().form_valid(form)

def get_context_data(self, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_activated_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Totp has been activated.{% endblocktrans %}{% endblock notification_message %}
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_activated_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Totp activated.{% endblocktrans %}
{% endautoescape %}
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_deactivated_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Totp has been deactivated.{% endblocktrans %}{% endblock notification_message %}
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_deactivated_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Totp decativated{% endblocktrans %}
{% endautoescape %}
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_reset_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Totp has been reset.{% endblocktrans %}{% endblock notification_message %}
4 changes: 4 additions & 0 deletions allauth/templates/mfa/email/totp_reset_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Totp reset{% endblocktrans %}
{% endautoescape %}

0 comments on commit 6cdbddf

Please sign in to comment.