Skip to content

Commit b029d64

Browse files
committed
add settings for demo users email and mobile
1 parent 36592ea commit b029d64

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ DEFAULTS = {
314314
# A dictionary of demo user's primary key mapped to their static pin
315315
'PASSWORDLESS_DEMO_USERS': {},
316316
317+
# A dictionary of demo user's email mapped to their static pin
318+
'PASSWORDLESS_DEMO_USERS_EMAIL': {},
319+
320+
# A dictionary of demo user's mobile mapped to their static pin
321+
'PASSWORDLESS_DEMO_USERS_MOBILE': {},
322+
317323
# configurable function for sending email
318324
'PASSWORDLESS_EMAIL_CALLBACK': 'drfpasswordless.utils.send_email_with_callback_token',
319325

drfpasswordless/services.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
from django.utils.module_loading import import_string
2+
23
from drfpasswordless.settings import api_settings
3-
from drfpasswordless.utils import (
4-
create_callback_token_for_user,
5-
)
4+
from drfpasswordless.utils import create_callback_token_for_user
65

76

87
class TokenService(object):
98
@staticmethod
109
def send_token(user, alias_type, token_type, **message_payload):
11-
token = create_callback_token_for_user(user, alias_type, token_type)
10+
alias_type_u = alias_type.upper()
11+
to_alias_field = getattr(
12+
api_settings, f"PASSWORDLESS_USER_{alias_type_u}_FIELD_NAME"
13+
)
14+
to_alias = getattr(user, to_alias_field)
15+
token = create_callback_token_for_user(user, alias_type, token_type, to_alias)
1216
send_action = None
1317

14-
if user.pk in api_settings.PASSWORDLESS_DEMO_USERS.keys():
18+
if user.pk in api_settings.PASSWORDLESS_DEMO_USERS or to_alias in getattr(
19+
api_settings, f"PASSWORDLESS_DEMO_USERS_{alias_type_u}"
20+
):
1521
return True
1622
if alias_type == 'email':
1723
send_action = import_string(api_settings.PASSWORDLESS_EMAIL_CALLBACK)

drfpasswordless/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585

8686
# A dictionary of demo user's primary key mapped to their static pin
8787
'PASSWORDLESS_DEMO_USERS': {},
88+
# A dictionary of demo user's email/mobile mapped to their static pin
89+
'PASSWORDLESS_DEMO_USERS_EMAIL': {},
90+
'PASSWORDLESS_DEMO_USERS_MOBILE': {},
91+
8892
'PASSWORDLESS_EMAIL_CALLBACK': 'drfpasswordless.utils.send_email_with_callback_token',
8993
'PASSWORDLESS_SMS_CALLBACK': 'drfpasswordless.utils.send_sms_with_callback_token',
9094

drfpasswordless/signals.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import logging
22
from django.contrib.auth import get_user_model
33
from django.core.exceptions import ValidationError
4-
from django.dispatch import receiver
54
from django.db.models import signals
6-
from drfpasswordless.models import CallbackToken
7-
from drfpasswordless.models import generate_numeric_token
8-
from drfpasswordless.settings import api_settings
5+
from django.dispatch import receiver
6+
from drfpasswordless.models import CallbackToken, generate_numeric_token
97
from drfpasswordless.services import TokenService
8+
from drfpasswordless.settings import api_settings
109

1110
logger = logging.getLogger(__name__)
1211

@@ -17,7 +16,9 @@ def invalidate_previous_tokens(sender, instance, created, **kwargs):
1716
Invalidates all previously issued tokens of that type when a new one is created, used, or anything like that.
1817
"""
1918

20-
if instance.user.pk in api_settings.PASSWORDLESS_DEMO_USERS.keys():
19+
if instance.user.pk in api_settings.PASSWORDLESS_DEMO_USERS or instance.to_alias in getattr(
20+
api_settings, f"PASSWORDLESS_DEMO_USERS_{instance.to_alias_type}"
21+
):
2122
return
2223

2324
if isinstance(instance, CallbackToken):

drfpasswordless/utils.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime
12
import logging
23
import os
34
from django.contrib.auth import get_user_model
@@ -35,34 +36,25 @@ def authenticate_by_token(callback_token):
3536
return None
3637

3738

38-
def create_callback_token_for_user(user, alias_type, token_type):
39-
token = None
39+
def create_callback_token_for_user(user, alias_type, token_type, to_alias):
4040
alias_type_u = alias_type.upper()
41-
to_alias_field = getattr(api_settings, f'PASSWORDLESS_USER_{alias_type_u}_FIELD_NAME')
42-
if user.pk in api_settings.PASSWORDLESS_DEMO_USERS.keys():
43-
token = CallbackToken.objects.filter(user=user).first()
44-
if token:
45-
return token
46-
else:
47-
return CallbackToken.objects.create(
48-
user=user,
49-
key=api_settings.PASSWORDLESS_DEMO_USERS[user.pk],
50-
to_alias_type=alias_type_u,
51-
to_alias=getattr(user, to_alias_field),
52-
type=token_type
53-
)
54-
55-
token = CallbackToken.objects.create(user=user,
56-
to_alias_type=alias_type_u,
57-
to_alias=getattr(user, to_alias_field),
58-
type=token_type)
59-
60-
61-
62-
if token is not None:
41+
demo_key = api_settings.PASSWORDLESS_DEMO_USERS.get(user.pk) or getattr(
42+
api_settings, f"PASSWORDLESS_DEMO_USERS_{alias_type_u}"
43+
).get(to_alias)
44+
if demo_key:
45+
token, _ = CallbackToken.objects.update_or_create(
46+
user=user,
47+
key=demo_key,
48+
to_alias_type=alias_type_u,
49+
to_alias=to_alias,
50+
type=token_type,
51+
defaults={"is_active": True, "created_at": datetime.now()})
6352
return token
6453

65-
return None
54+
return CallbackToken.objects.create(user=user,
55+
to_alias_type=alias_type_u,
56+
to_alias=to_alias,
57+
type=token_type)
6658

6759

6860
def validate_token_age(callback_token):
@@ -74,7 +66,9 @@ def validate_token_age(callback_token):
7466
token = CallbackToken.objects.get(key=callback_token, is_active=True)
7567
seconds = (timezone.now() - token.created_at).total_seconds()
7668
token_expiry_time = api_settings.PASSWORDLESS_TOKEN_EXPIRE_TIME
77-
if token.user.pk in api_settings.PASSWORDLESS_DEMO_USERS.keys():
69+
if token.user.pk in api_settings.PASSWORDLESS_DEMO_USERS or token.to_alias in getattr(
70+
api_settings, f"PASSWORDLESS_DEMO_USERS_{token.to_alias_type}"
71+
):
7872
return True
7973
if seconds <= token_expiry_time:
8074
return True

0 commit comments

Comments
 (0)