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.
- OTP generation and validation
- Easy integration with Django projects
- Secure and extensible
pip install django-otp-keygen
Add the following to your settings.py
:
OTP_MODEL = "user.Otp" # Required. Raises ValueError if not configured
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
Create your custom model using AbstractOtp
:
from django_otp_keygen.models import AbstractOtp
class Otp(AbstractOtp):
pass
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
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)
-
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
-
otp_instance.is_expired
-
otp_instance.is_verified
-
otp_instance.is_pending
-
otp_instance.is_active
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 |
django_otp_keygen/
├── models.py
├── services.py
├── otp.py
├── utils.py
├── constants.py
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
This reposiitory is under MIT License.
If you find this useful, consider sponsoring or donating.