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:
-
🍴 Fork the repository
-
🛠️ Create a new branch (
git checkout -b feature/your-feature) -
💾 Make your changes
-
🧪 Write or update tests if needed
-
📩 Commit your changes and push (git push origin feature/your-feature)
-
📝 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.
Metadata
Release files for django-otp-keygen 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_otp_keygen-0.1.1.tar.gz | 9.6 kB | Without provenance | Details for django_otp_keygen-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_otp_keygen-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for django_otp_keygen-0.1.1-py3-none-any.whl |
Total release size: 19.6 kB
Release files / django_otp_keygen-0.1.1.tar.gz
| Download URL | django_otp_keygen-0.1.1.tar.gz |
|---|---|
| Size | 9.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
256b3ceaaf0fa49ba238374d64022a28f7123574e9908c1276a4d93dee917cdb
|
|
BLAKE2b-256 checksum How to use checksums |
d44753321459fb6854a9634b12f306b153198a57a7d910b16d0daa53acc4baff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|
Release files / django_otp_keygen-0.1.1-py3-none-any.whl
| Download URL | django_otp_keygen-0.1.1-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4601653018a7c9f9a9230d50929546543275b536ee4b12cbcc3a6cce5bbf04ba
|
|
BLAKE2b-256 checksum How to use checksums |
a845e0140fc0177448ce8659f7b5854d2b1cbb936c9097246f055b9f54c0cce3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|
Security for django-otp-keygen 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.