Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional context to send_email_with_callback_token template and plaintext #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions drfpasswordless/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
16 changes: 13 additions & 3 deletions drfpasswordless/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,29 @@ 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),)
email_plaintext = email_plaintext % string_options
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.
context = inject_template_context({'callback_token': email_token.key, })
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,
[getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME)],
[user_email],
fail_silently=False,
html_message=html_message,)

Expand Down
3 changes: 2 additions & 1 deletion drfpasswordless/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 protected]'
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']
Expand Down