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
pip install django-otp-keygen
🔧 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_otp_keygen-0.1.0.tar.gz.
File metadata
- Download URL: django_otp_keygen-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c2c54721e1b636ebf985d7a65e54a81773d72c6f558bcb199cade9e203696e0
|
|
| MD5 |
d3c3954466ca1e77f885b2cdbf4f687c
|
|
| BLAKE2b-256 |
b0549c3a69275fa83e5e98b520583299ec5729635a913857bf00680aa56d50dc
|
File details
Details for the file django_otp_keygen-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_otp_keygen-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cd1a9be0f4d23ed7c5a07df5b274f5b868d853344ff3ed2531f1c90137a4582
|
|
| MD5 |
4d04f342fe7b9bfb663a039509a1a172
|
|
| BLAKE2b-256 |
c1146277d47bcea559d3773025234a7d69aae7adb9fee30e8dc1914b5a9e504e
|