Skip to content

Commit

Permalink
support test accounts (#7)
Browse files Browse the repository at this point in the history
`export EMAIL_TEST_ACCOUNT_a_at_test_com=123456` then (pretend) send
email to `[email protected]`, verify token using the preset value `123456`.
  • Loading branch information
xyb committed Feb 16, 2023
1 parent d172adc commit a382aaa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 5 additions & 4 deletions drf_passwordless_jwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
if getenv('OTP_EMAIL_HTML'):
PASSWORDLESS_AUTH['PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME'] = getenv('OTP_EMAIL_HTML')

OTP_TOKEN_CLEAN_SECONDS = int(getenv('OTP_TOKEN_CLEAN_SECONDS', 60 * 60 * 30))
OTP_TOKEN_CLEAN_SECONDS = int(getenv('OTP_TOKEN_CLEAN_SECONDS', 3600 * 24 * 30))
JWT_EXPIRE_SECONDS = int(getenv('JWT_EXPIRE_SECONDS', 3600 * 24 * 30))
JWT_SECRET = getenv('JWT_SECRET', 'a long long secret string')

if getenv('EMAIL_BACKEND_TEST'):
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Expand All @@ -89,9 +91,8 @@
EMAIL_WHITE_LIST = getenv('EMAIL_WHITE_LIST', r'.*')
EMAIL_WHITE_LIST_MESSAGE = getenv('EMAIL_WHITE_LIST_MESSAGE',
'email address not in white list')

JWT_SECRET = getenv('JWT_SECRET', 'your secret key')
JWT_EXPIRE_SECONDS = int(getenv('JWT_EXPIRE_SECONDS', 60 * 60 * 24 * 30))
EMAIL_TEST_ACCOUNT_PREFIX = getenv('EMAIL_TEST_ACCOUNT_PREFIX',
'EMAIL_TEST_ACCOUNT_')

if getenv('CORS_ALLOWED_ORIGINS'):
CORS_ALLOWED_ORIGINS = getenv('CORS_ALLOWED_ORIGINS').split(',')
Expand Down
11 changes: 11 additions & 0 deletions drf_passwordless_jwt/testaccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.conf import settings
from os import getenv

def get_test_account_token(email):
name = email.replace('@', '_at_').replace('.', '_')
env = '{}{}'.format(settings.EMAIL_TEST_ACCOUNT_PREFIX, name)
return getenv(env)


def exists_test_account(email):
return bool(get_test_account_token(email))
25 changes: 23 additions & 2 deletions drf_passwordless_jwt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,31 @@

from .utils import generate_jwt
from .serializers import EmailAuthWhiteListSerializer, JWTSerializer
from .testaccount import get_test_account_token, exists_test_account


class ObtainEmailTokenView(ObtainEmailCallbackToken):
serializer_class = EmailAuthWhiteListSerializer
def post(self, request, *args, **kwargs):
email = request.data['email']
if exists_test_account(email):
return Response({'detail':
f'test account email {email!r} available'})

return super(ObtainEmailTokenView, self).post(request, *args, **kwargs)


class ObtainJWTView(ObtainAuthTokenFromCallbackToken):
def post(self, request, *args, **kwargs):
email = request.data['email']
resp = super(ObtainJWTView, self).post(request, *args,
**kwargs)
if exists_test_account(email):
if request.data['token'] == get_test_account_token(email):
return Response({
'email': email,
'token': generate_jwt(email),
})

resp = super(ObtainJWTView, self).post(request, *args, **kwargs)
token = generate_jwt(email)
resp.data['email'] = email
resp.data['token'] = token
Expand All @@ -41,6 +55,13 @@ class VerifyJWTView(APIView):
serializer_class = JWTSerializer

def post(self, request, *args, **kwargs):
email = request.data['email']
if exists_test_account(email):
return Response({
'email': email,
'exp': '9999-12-31T23:59:59',
})

serializer = self.serializer_class(data=request.data,
context={'request': request})
if serializer.is_valid(raise_exception=False):
Expand Down

0 comments on commit a382aaa

Please sign in to comment.