From 88fa89b6068ae3d491150719f09104285d778f58 Mon Sep 17 00:00:00 2001 From: hugh_armitage-at-470578934303 Date: Wed, 12 Feb 2020 19:30:59 +1000 Subject: [PATCH 1/3] add request and email_user to send_email_with_callback_token --- drfpasswordless/utils.py | 5 +++-- drfpasswordless/views.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/drfpasswordless/utils.py b/drfpasswordless/utils.py index 2bdc106..17353c6 100644 --- a/drfpasswordless/utils.py +++ b/drfpasswordless/utils.py @@ -125,13 +125,14 @@ def send_email_with_callback_token(user, email_token, **kwargs): api_settings.PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME) # Inject context if user specifies. - context = inject_template_context({'callback_token': email_token.key, }) + user_email = getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME) + context = inject_template_context({'callback_token': email_token.key, 'user_email':user_email, 'request': kwargs['request']}) html_message = loader.render_to_string(email_html, context,) send_mail( email_subject, email_plaintext % email_token.key, api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS, - [getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME)], + [user_email], fail_silently=False, html_message=html_message,) diff --git a/drfpasswordless/views.py b/drfpasswordless/views.py index 6692ac8..d2d14e6 100644 --- a/drfpasswordless/views.py +++ b/drfpasswordless/views.py @@ -2,7 +2,7 @@ from django.utils.module_loading import import_string from rest_framework import parsers, renderers, status from rest_framework.response import Response -from rest_framework.permissions import AllowAny, IsAuthenticated +from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.views import APIView from drfpasswordless.models import CallbackToken from drfpasswordless.settings import api_settings @@ -53,6 +53,7 @@ def post(self, request, *args, **kwargs): # Validate - user = serializer.validated_data['user'] # Create and send callback token + self.message_payload['request'] = request success = TokenService.send_token(user, self.alias_type, self.token_type, **self.message_payload) # Respond With Success Or Failure of Sent From 8f5fd09c1271c496f8dfe5fce4b09e09b5bcf2df Mon Sep 17 00:00:00 2001 From: hugh_armitage-at-470578934303 Date: Fri, 14 Feb 2020 20:44:33 +1000 Subject: [PATCH 2/3] add additional context to email plaintext --- drfpasswordless/settings.py | 3 +++ drfpasswordless/utils.py | 12 ++++++++++-- tests/test_settings.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/drfpasswordless/settings.py b/drfpasswordless/settings.py index e640253..0de2716 100644 --- a/drfpasswordless/settings.py +++ b/drfpasswordless/settings.py @@ -66,6 +66,9 @@ # A plaintext verification email message overridden by the html message. Takes one string. 'PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE': "Enter this verification code: %s", + # Insert multiple variables into PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE, eg ["kwargs['request'].get_host()", 'user_email', 'email_token.key'] + 'PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT' : False, + # The verification email template name. 'PASSWORDLESS_EMAIL_VERIFICATION_TOKEN_HTML_TEMPLATE_NAME': "passwordless_default_verification_token_email.html", diff --git a/drfpasswordless/utils.py b/drfpasswordless/utils.py index 17353c6..fde8d10 100644 --- a/drfpasswordless/utils.py +++ b/drfpasswordless/utils.py @@ -119,18 +119,26 @@ def send_email_with_callback_token(user, email_token, **kwargs): # Get email subject and message email_subject = kwargs.get('email_subject', api_settings.PASSWORDLESS_EMAIL_SUBJECT) + user_email = getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME) + email_plaintext = kwargs.get('email_plaintext', api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE) + if api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT: + string_options = tuple() + for x in api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT: + string_options = string_options + (eval(x),) + else: + email_plaintext = email_plaintext % email_token.key + email_html = kwargs.get('email_html', api_settings.PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME) # Inject context if user specifies. - user_email = getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME) context = inject_template_context({'callback_token': email_token.key, 'user_email':user_email, 'request': kwargs['request']}) html_message = loader.render_to_string(email_html, context,) send_mail( email_subject, - email_plaintext % email_token.key, + email_plaintext, api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS, [user_email], fail_silently=False, diff --git a/tests/test_settings.py b/tests/test_settings.py index a6799d3..7e251b3 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -6,6 +6,7 @@ from django.urls import reverse from drfpasswordless.settings import api_settings, DEFAULTS from drfpasswordless.utils import CallbackToken +from drfpasswordless.views import ObtainEmailCallbackToken User = get_user_model() @@ -75,6 +76,15 @@ def test_mobile_only_auth_enabled(self): mobile_response = self.client.post(self.mobile_url, self.mobile_data) self.assertEqual(mobile_response.status_code, status.HTTP_200_OK) + def test_email_plaintext_ordered_context(self): + api_settings.PASSWORDLESS_AUTH_TYPES = ['EMAIL'] + api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS = 'email@example.com' + api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT = ["kwargs['request'].get_host()", 'user_email', 'email_token.key'] + api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE = 'click on link to activate https://%s/login/%s/%s' + + email_response = self.client.post(self.email_url, self.email_data) + self.assertEqual(email_response.data['detail'], ObtainEmailCallbackToken.success_response) + def tearDown(self): api_settings.PASSWORDLESS_AUTH_TYPES = DEFAULTS['PASSWORDLESS_AUTH_TYPES'] api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS = DEFAULTS['PASSWORDLESS_EMAIL_NOREPLY_ADDRESS'] From 8dad95ab0b2ce33d0e9fee9455391893929ec0cf Mon Sep 17 00:00:00 2001 From: hugh_armitage-at-470578934303 Date: Sun, 23 Feb 2020 10:16:23 +1000 Subject: [PATCH 3/3] string replacement for email_plaintext multiple variables --- drfpasswordless/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/drfpasswordless/utils.py b/drfpasswordless/utils.py index fde8d10..5c0388e 100644 --- a/drfpasswordless/utils.py +++ b/drfpasswordless/utils.py @@ -127,6 +127,7 @@ def send_email_with_callback_token(user, email_token, **kwargs): string_options = tuple() for x in api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT: string_options = string_options + (eval(x),) + email_plaintext = email_plaintext % string_options else: email_plaintext = email_plaintext % email_token.key