Skip to content

Commit 81a8882

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

File tree

5 files changed

+45
-36
lines changed

5 files changed

+45
-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: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,24 @@ def authenticate_by_token(callback_token):
3535
return None
3636

3737

38-
def create_callback_token_for_user(user, alias_type, token_type):
39-
token = None
38+
def create_callback_token_for_user(user, alias_type, token_type, to_alias):
4039
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:
40+
demo_key = api_settings.PASSWORDLESS_DEMO_USERS.get(user.pk) or getattr(
41+
api_settings, f"PASSWORDLESS_DEMO_USERS_{alias_type_u}"
42+
).get(to_alias)
43+
if demo_key:
44+
token, _ = CallbackToken.objects.get_or_create(
45+
user=user,
46+
key=demo_key,
47+
to_alias_type=alias_type_u,
48+
to_alias=to_alias,
49+
type=token_type)
6350
return token
6451

65-
return None
52+
return CallbackToken.objects.create(user=user,
53+
to_alias_type=alias_type_u,
54+
to_alias=to_alias,
55+
type=token_type)
6656

6757

6858
def validate_token_age(callback_token):
@@ -74,7 +64,9 @@ def validate_token_age(callback_token):
7464
token = CallbackToken.objects.get(key=callback_token, is_active=True)
7565
seconds = (timezone.now() - token.created_at).total_seconds()
7666
token_expiry_time = api_settings.PASSWORDLESS_TOKEN_EXPIRE_TIME
77-
if token.user.pk in api_settings.PASSWORDLESS_DEMO_USERS.keys():
67+
if token.user.pk in api_settings.PASSWORDLESS_DEMO_USERS or token.to_alias in getattr(
68+
api_settings, f"PASSWORDLESS_DEMO_USERS_{token.to_alias_type}"
69+
):
7870
return True
7971
if seconds <= token_expiry_time:
8072
return True

0 commit comments

Comments
 (0)