Skip to content

Commit 46480ea

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

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
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/utils.py

Lines changed: 15 additions & 25 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):

0 commit comments

Comments
 (0)