Skip to content

Commit e2b56e5

Browse files
committed
Introduce a way to override how auth tokens are created
This creates a new setting PASSWORDLESS_AUTH_TOKEN_CREATOR. This is a string representing the function used to construct an authentication token after receiving a valid passwordless token.
1 parent 8117c75 commit e2b56e5

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ DEFAULTS = {
290290
291291
# Automatically send verification email or sms when a user changes their alias.
292292
'PASSWORDLESS_AUTO_SEND_VERIFICATION_TOKEN': False,
293+
294+
# What function is called to construct an authentication tokens when
295+
# exchanging a passwordless token for a real user auth token. This function
296+
# should take a user and return a tuple of two values. The first value is
297+
# the token itself, the second is a boolean value representating whether
298+
# the token was newly created.
299+
'PASSWORDLESS_AUTH_TOKEN_CREATOR': 'drfpasswordless.utils.create_authentication_token'
293300
}
294301
```
295302

drfpasswordless/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
# Automatically send verification email or sms when a user changes their alias.
7070
'PASSWORDLESS_AUTO_SEND_VERIFICATION_TOKEN': False,
7171

72+
# What function is called to construct an authentication tokens when
73+
# exchanging a passwordless token for a real user auth token.
74+
'PASSWORDLESS_AUTH_TOKEN_CREATOR': 'drfpasswordless.utils.create_authentication_token'
7275
}
7376

7477
# List of settings that may be in string import notation.

drfpasswordless/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.core.mail import send_mail
66
from django.template import loader
77
from django.utils import timezone
8+
from rest_framework.authtoken.models import Token
89
from drfpasswordless.models import CallbackToken
910
from drfpasswordless.settings import api_settings
1011

@@ -184,3 +185,8 @@ def send_sms_with_callback_token(user, mobile_token, **kwargs):
184185
"Number entered was {}".format(user.id, getattr(user, api_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME)))
185186
logger.debug(e)
186187
return False
188+
189+
190+
def create_authentication_token(user):
191+
""" Default way to create an authentication token"""
192+
return Token.objects.get_or_create(user=user)

drfpasswordless/views.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2+
from django.utils.module_loading import import_string
23
from rest_framework import parsers, renderers, status
3-
from rest_framework.authtoken.models import Token
44
from rest_framework.response import Response
55
from rest_framework.permissions import AllowAny, IsAuthenticated
66
from rest_framework.views import APIView
@@ -130,7 +130,8 @@ def post(self, request, *args, **kwargs):
130130
serializer = self.serializer_class(data=request.data)
131131
if serializer.is_valid(raise_exception=True):
132132
user = serializer.validated_data['user']
133-
token, created = Token.objects.get_or_create(user=user)
133+
token_creator = import_string(api_settings.PASSWORDLESS_AUTH_TOKEN_CREATOR)
134+
token, created = token_creator(user)
134135

135136
if created:
136137
# Initially set an unusable password if a user is created through this.

0 commit comments

Comments
 (0)