Skip to main content

Django users authentication through SMS code

Project description

Django rest sms auth

  • Authentication users in Django through SMS code
  • Change user phone number with confirmation

Requirements

  • Python >= 3.0
  • Django >= 2.0
  • Celery
  • Djangorestframework
  • Django-phonenumber-field

Concept

  1. Client side send phone number (web/ios/android)
  2. smsauth validate phone number and create sms code with life time
  3. smsauth send sms code (through sms provider)
  4. User got sms code. Send it
  5. smsauth validate {sms code + phone number}
  6. Send to client info (jwt token)

Notes

  • Library use celery. Instruction
  • To use twilio install extra library
  • You may add your own provider inherit from SMSProvider

Installation

pip install django-rest-sms-auth

If you want to use twilio:

pip install twilio

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ...
    'sms_auth',  # you have to add this
    'sms_auth.providers.twilio' # if twilio provider
    'sms_auth.providers.megafon' # if megafon provider
]

SMS_AUTH_SETTINGS = {
    "SMS_CELERY_FILE_NAME": "run_celery", # your system celery file,
    "SMS_AUTH_SUCCESS_KEY": "jwt_token", # property from user model
    "SMS_AUTH_PROVIDER_FROM": "ex: +7542222222", # sms signature

    # If twilio
    "SMS_AUTH_ACCOUNT_SID": "Twilio SID"
    "SMS_AUTH_AUTH_TOKEN": "Twilio token"

    # If another provider
    "SMS_AUTH_PROVIDER_LOGIN":"SMS provider login"
    "SMS_AUTH_PROVIDER_PASSWORD": "SMS provider password"
}

Add celery configuration file: Instruction

run migrations:

python manage.py makemigrations sms_auth && python manage.py migrate

urls.py

path('auth/', include('sms_auth.api.urls'))

Library is ready to use.

Usage

  1. Sign-in / sign-up:
POST /auth/sign-in/
body: {
    "phone_number":"user phone number"
}
result: 200/400 response
  1. Code validation and get token:
POST /auth/auth/
body: {
    "phone_number":"user phone number",
    "code":sms_code
}
result: 200/400 response (with token)
  1. Change phone number:
POST /auth/change-phonenumber/
body: {
    "new_phone_number":"user new phone number"
}
result: 200/400 response

After your call previous endpoint: /auth/auth and send new phone number with code.

Extra

To clear all expired sms codes

python manage.py clear_expired

Additional settings:

"SMS_AUTH_CODE_LEN": int (default: 4)
"SMS_DEBUG": bool (default: False)
"SMS_DEBUG_CODE": int (when debug, default 1111)
"SMS_USER_FIELD": "username" 
"SMS_TIMELIFE": 60 # life time of each sms code
"SMS_CODE_NOT_FOUND": "Some text when code not found"
"SMS_WAIT_TIME": "Some text when sms was sended"
"SMS_REQUEST_SUCCESS": "Some text when success phone validatioin and sms sended to user"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-rest-sms-auth-0.1.17.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

django_rest_sms_auth-0.1.17-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file django-rest-sms-auth-0.1.17.tar.gz.

File metadata

  • Download URL: django-rest-sms-auth-0.1.17.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

File hashes

Hashes for django-rest-sms-auth-0.1.17.tar.gz
Algorithm Hash digest
SHA256 031f0629fb72e7ff0f063fe982f05f0d6d1859c1878388dd9ba0c1575788697f
MD5 d04240bd8bc5652d541bbed6c27c0f66
BLAKE2b-256 85250fcfac22d50e38bdb20b6fe270e09ee18471ffa7270e57eedb63ece618f9

See more details on using hashes here.

Provenance

File details

Details for the file django_rest_sms_auth-0.1.17-py3-none-any.whl.

File metadata

  • Download URL: django_rest_sms_auth-0.1.17-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

File hashes

Hashes for django_rest_sms_auth-0.1.17-py3-none-any.whl
Algorithm Hash digest
SHA256 aad8358cf754eb796f7ee0e9b49170e2525c4ad5e0e464247c9cda17b15daf1e
MD5 41036628fa1c850f2d5315cb259e62a7
BLAKE2b-256 00e80eca1fcebaeb4632dcbfc838cb7013bd42e3c5d901be995e5e11611b921a

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page