Skip to main content

Django OTP key generator and validator

Project description

django-otp-keygen

A Django package to generate and manage One-Time Passwords (OTP) for user authentication. A configurable, pluggable, and extensible OTP generator and validator for Django.

Features

  • OTP generation and validation
  • Easy integration with Django projects
  • Secure and extensible

Installation

Link to PyPi project page: link

pip install django-otp-keygen

Demo

https://djangootpkeygen.pythonanywhere.com/docs/

🔧 Required Configuration

Add the following to your settings.py:

OTP_MODEL = "user.Otp"  # Required. Raises ValueError if not configured

⚙️ Optional Settings

Add the following optional settings to your settings.py:

OTP_TYPE_CHOICES = [
    ("email", _("Email Verification OTP")),
    ("phone", _("Phone Verification OTP")),
    ("forgot-password", _("Forgot Password OTP")),
    ("reset-password", _("Reset Password OTP")),
    ("two-factor-authentication", _("Two Factor Authentication OTP")),
]
OTP_GENERATION_INTERVAL = 10  # in seconds, default is 30
OTP_LENGTH = 6  # default is 6
GENERATE_ALPHANUMERIC_OTP = True  # default is False

🧱 Abstract Model Example

Create your custom model using AbstractOtp:

from django_otp_keygen.models import AbstractOtp

class Otp(AbstractOtp):
    pass

Extend Admin

Create your custom admin using AbstractOtpAdmin:

from django_otp_keygen.admin import AbstractOtpAdmin


class OtpAdmin(AbstractOtpAdmin):
    """
    Admin interface for managing OTPs.
    Inherits from UserAdmin to provide a user-friendly interface.
    """

    pass

🔐 Usage: OTP Service

from django_otp_keygen.services import OtpService

otp_service = OtpService(user, "email")
otp_value = otp_service.generate_otp()

# To verify
is_valid = otp_service.verify_otp(input_otp)

✅ OTP Validation Logic

  • Ensures OTP is not expired (expired_at)

  • Prevents re-verification

  • Automatically sets new expiration on generation

  • OTP format is digit or alphanumeric based on settings

🧪 Model Properties

  • otp_instance.is_expired

  • otp_instance.is_verified

  • otp_instance.is_pending

  • otp_instance.is_active

🔍 Settings Reference

Setting Type Default Description
OTP_MODEL str Required. Points to your model
OTP_TYPE_CHOICES list of tuples predefined Custom OTP purpose types
OTP_GENERATION_INTERVAL int 30 OTP valid window in seconds
OTP_LENGTH int 6 OTP length
GENERATE_ALPHANUMERIC_OTP bool False Use mixed alphanumeric format

📁 Directory Structure

django_otp_keygen/
├── models.py
├── services.py
├── otp.py
├── utils.py
├── constants.py

Contributing

Contributions, issues, and feature requests are welcome!

If you'd like to help improve django-otp-keygen, please follow these steps:

  1. 🍴 Fork the repository

  2. 🛠️ Create a new branch (git checkout -b feature/your-feature)

  3. 💾 Make your changes

  4. 🧪 Write or update tests if needed

  5. 📩 Commit your changes and push (git push origin feature/your-feature)

  6. 📝 Open a pull request describing your changes

License

This reposiitory is under MIT Licence File

Donations

If you find this useful, consider sponsoring or donating.

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_otp_keygen-0.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_otp_keygen-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file django_otp_keygen-0.1.1.tar.gz.

File metadata

  • Download URL: django_otp_keygen-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for django_otp_keygen-0.1.1.tar.gz
Algorithm Hash digest
SHA256 256b3ceaaf0fa49ba238374d64022a28f7123574e9908c1276a4d93dee917cdb
MD5 5d9101fc13ae5778a897826de76b3a65
BLAKE2b-256 d44753321459fb6854a9634b12f306b153198a57a7d910b16d0daa53acc4baff

See more details on using hashes here.

File details

Details for the file django_otp_keygen-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_otp_keygen-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4601653018a7c9f9a9230d50929546543275b536ee4b12cbcc3a6cce5bbf04ba
MD5 18db58983fabe33872d4d820246f0ce6
BLAKE2b-256 a845e0140fc0177448ce8659f7b5854d2b1cbb936c9097246f055b9f54c0cce3

See more details on using hashes here.

Supported by

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