Package: djangopasswordlessknox
djangopasswordlessknox is a quick way to integrate ‘passwordless’ auth into
your Django Rest Framework project using a user’s email address or
mobile number only (herein referred to as an alias).Built knox TokenAuthentication system, it sends the
user a 6-digit callback token to a given email address or a mobile
number. The user sends it back correctly and they’re given an
authentication token.Callback tokens by default expire after 15 minutes.Example Usage:curl-XPOST-d“email=lijoev@example.com”localhost:8000/auth/email/Email tolijoev@example.com:...
<h1>Your login token is 815381.</h1>
...Return Stagecurl-XPOST-d"token=815381"localhost:8000/callback/auth/

>HTTP/1.0200OK
>{"token":"76be2d9ecfaf5fa4226d722bzdd8a4fff207ed0e”}RequirementsPython (3.6+)Django (2.0+)Django Rest Framework + AuthToken (3.6+)django-rest-knox (4.1.0)Python-Twilio (Optional, for mobile.)InstallInstall djangopasswordlessknoxpipenv install djangopasswordlessknoxAdd Django Rest Framework’s Token Authentication to your Django Rest
Framework project.REST_FRAMEWORK={'DEFAULT_AUTHENTICATION_CLASSES':('knox.auth.TokenAuthentication',)}INSTALLED_APPS=[...'rest_framework','knox','djangopasswordlessknox',...]REST_KNOX={'SECURE_HASH_ALGORITHM':'cryptography.hazmat.primitives.hashes.SHA512','AUTH_TOKEN_CHARACTER_LENGTH':64,'TOKEN_TTL':timedelta(hours=10),'USER_SERIALIZER':'knox.serializers.UserSerializer','TOKEN_LIMIT_PER_USER':None,'AUTO_REFRESH':False,# 'EXPIRY_DATETIME_FORMAT': api_settings.DATETME_FORMAT,}And runpythonmanage.pymigrateSet which types of contact points are allowed for auth in your
Settings.py. The available options areEMAILandMOBILE.PASSWORDLESS_AUTH={..'PASSWORDLESS_AUTH_TYPES':['EMAIL','MOBILE'],..}By default drfpasswordless looks for fields namedemailormobileon the User model. If an alias provided doesn’t belong to any given user,
a new user is created.3a. If you’re usingemail, see the Configuring Email section
below.3b. If you’re usingmobile, see the Configuring Email section
below.Addknoxpasswordlessdrf.urlsto your urls.pyurlpatterns=[..path('',include('djangopasswordlessknox.urls')),..]You can now POST to either of the endpoints:curl-XPOST-d"email=lijoev@example.com"localhost:8000/auth/email/

//OR

curl-XPOST-d"mobile=+15552143912"localhost:8000/auth/mobile/A 6 digit callback token will be sent to the contact point.The client has 15 minutes to use the 6 digit callback token
correctly. If successful, they get an authorization token in exchange
which the client can then use with Django Rest Framework’s
TokenAuthentication scheme.curl-XPOST-d"token=815381"localhost:8000/callback/auth/

>HTTP/1.0200OK
>{"token":"76be2d9ecfaf5fa4226d722bzdd8a4fff207ed0e”}Configuring EmailsSpecify the email address you’d like to send the callback token from
with thePASSWORDLESS_EMAIL_NOREPLY_ADDRESSsetting.You’ll also need to set up an SMTP server to send emails (See Django Docs <https://docs.djangoproject.com/en/1.10/topics/email/>), but for
development you can set up a dummy development smtp server to test
emails. Sent emails will print to the console.Read more here. <https://docs.djangoproject.com/en/1.10/topics/email/#configuring-email-for-development># Settings.py…EMAIL_HOST='localhost'EMAIL_PORT=1025Then run the following:python-msmtpd-n-cDebuggingServerlocalhost:1025Configuring MobileYou’ll need to have the python twilio module installedpipenvinstalltwilioand set theTWILIO_ACCOUNT_SIDandTWILIO_AUTH_TOKENenvironment
variables.You’ll also need to specify the number you send the token from with thePASSWORDLESS_MOBILE_NOREPLY_NUMBERsetting.TemplatesIf you’d like to use a custom email template for your email callback
token, specify your template name with this setting:PASSWORDLESS_AUTH={...'PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME':"mytemplate.html"}The template renders a single variable{{ callback_token }}which is
the 6 digit callback token being sent.Contact Point ValidationEndpoints can automatically mark themselves as validated when a user
logs in with a token sent to a specific endpoint. They can also
automatically mark themselves as invalid when a user changes a contact
point.This is off by default but can be turned on withPASSWORDLESS_USER_MARK_EMAIL_VERIFIEDorPASSWORDLESS_USER_MARK_MOBILE_VERIFIED. By default when these are
enabled they look for the User model fieldsemail_verifiedormobile_verified.You can also use/validate/email/or/validate/mobile/which will
automatically send a token to the endpoint attached to the currentrequest.user's email or mobile if available.You can then send that token to/callback/verify/which will double-check
that the endpoint belongs to the request.user and mark the alias as verified.RegistrationAll unrecognized emails and mobile numbers create new accounts by
default. New accounts are automatically set withset_unusable_password()but it’s recommended that admins have some
type of password.This can be turned off with thePASSWORDLESS_REGISTER_NEW_USERSsetting.Other SettingsHere’s a full list of the configurable defaults.DEFAULTS={# Allowed auth types, can be EMAIL, MOBILE, or both.'PASSWORDLESS_AUTH_TYPES':['EMAIL'],# Amount of time that tokens last, in seconds'PASSWORDLESS_TOKEN_EXPIRE_TIME':15*60,# The user's email field name'PASSWORDLESS_USER_EMAIL_FIELD_NAME':'email',# The user's mobile field name'PASSWORDLESS_USER_MOBILE_FIELD_NAME':'mobile',# Marks itself as verified the first time a user completes auth via token.# Automatically unmarks itself if email is changed.'PASSWORDLESS_USER_MARK_EMAIL_VERIFIED':False,'PASSWORDLESS_USER_EMAIL_VERIFIED_FIELD_NAME':'email_verified',# Marks itself as verified the first time a user completes auth via token.# Automatically unmarks itself if mobile number is changed.'PASSWORDLESS_USER_MARK_MOBILE_VERIFIED':False,'PASSWORDLESS_USER_MOBILE_VERIFIED_FIELD_NAME':'mobile_verified',# The email the callback token is sent from'PASSWORDLESS_EMAIL_NOREPLY_ADDRESS':None,# The email subject'PASSWORDLESS_EMAIL_SUBJECT':"Your Login Token",# A plaintext email message overridden by the html message. Takes one string.'PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE':"Enter this token to sign in:%s",# The email template name.'PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME':"passwordless_default_token_email.html",# Your twilio number that sends the callback tokens.'PASSWORDLESS_MOBILE_NOREPLY_NUMBER':None,# The message sent to mobile users logging in. Takes one string.'PASSWORDLESS_MOBILE_MESSAGE':"Use this code to log in:%s",# Registers previously unseen aliases as new users.'PASSWORDLESS_REGISTER_NEW_USERS':True,# Suppresses actual SMS for testing'PASSWORDLESS_TEST_SUPPRESSION':False,# Context Processors for Email Template'PASSWORDLESS_CONTEXT_PROCESSORS':[],# The verification email subject'PASSWORDLESS_EMAIL_VERIFICATION_SUBJECT':"Your Verification Token",# A plaintext verification email message overridden by the html message. Takes one string.'PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE':"Enter this verification code:%s",# The verification email template name.'PASSWORDLESS_EMAIL_VERIFICATION_TOKEN_HTML_TEMPLATE_NAME':"passwordless_default_verification_token_email.html",# The message sent to mobile users logging in. Takes one string.'PASSWORDLESS_MOBILE_VERIFICATION_MESSAGE':"Enter this verification code:%s",# Automatically send verification email or sms when a user changes their alias.'PASSWORDLESS_AUTO_SEND_VERIFICATION_TOKEN':False,}To Dogithub.io project pageAdd MkDocs -http://www.mkdocs.org/Support non-US mobile numbersCustom URLsChange bad settings to 500'sPull requests are encouraged!LicenseThe MIT License (MIT)Copyright (c) 2018 LijoPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.