TOTP (Time-based One-Time Password) authentication for Django REST Framework
Project description
DRF-TOTP
TOTP (Time-based One-Time Password) authentication for Django REST Framework.
Features
- Generate TOTP secrets for users
- Verify TOTP tokens
- Enable/disable TOTP authentication
- Check TOTP status
- Validate TOTP tokens
Installation
pip install drf-totp
Quick Start
- Add "drf_totp" to your INSTALLED_APPS setting:
INSTALLED_APPS = [
...
'rest_framework',
'drf_totp',
]
- Include the TOTP URLconf in your project urls.py:
path('auth/', include('drf_totp.urls')),
- Run migrations:
python manage.py migrate
Settings
Add these to your Django settings:
# Optional: Set your TOTP issuer name (defaults to "drftotp")
TOTP_ISSUER_NAME = "Your App Name"
API Endpoints
POST /auth/otp/generate/
: Generate new TOTP secretPOST /auth/otp/verify/
: Verify and enable TOTPGET /auth/otp/status/
: Get TOTP statusPOST /auth/otp/disable/
: Disable TOTPPOST /auth/otp/validate/
: Validate TOTP token
Usage Example
import axios from 'axios';
// Generate TOTP
export async function generateTotp() {
try {
const response = await axios.post('/auth/otp/generate/');
const { secret, otpauth_url } = response.data;
return { secret, otpauth_url };
} catch (error) {
console.error('Error generating TOTP:', error);
throw error;
}
}
// Verify TOTP
export async function verifyTotp(token) {
try {
const response = await axios.post('/auth/otp/verify/', { token });
return response.data;
} catch (error) {
console.error('Error verifying TOTP:', error);
throw error;
}
}
// Check Status
export async function checkStatus() {
try {
const response = await axios.get('/auth/otp/status/');
return response.data;
} catch (error) {
console.error('Error checking status:', error);
throw error;
}
}
// Validate TOTP
export async function validateTotp(token) {
try {
const response = await axios.post('/auth/otp/validate/', { token });
return response.data;
} catch (error) {
console.error('Error validating TOTP:', error);
throw error;
}
}
License
MIT License - see LICENSE file for details.
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
drf_totp-0.1.4.tar.gz
(6.2 kB
view details)
Built Distribution
File details
Details for the file drf_totp-0.1.4.tar.gz
.
File metadata
- Download URL: drf_totp-0.1.4.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d7c5223beaf86767e7383c79f60e514409440e0a5b1d8b3e7910fb9e4dc8eff |
|
MD5 | b694a4c5c2d879a37be54f847bf84ad2 |
|
BLAKE2b-256 | 329fbae81a0c31635d2caaa85c8cc3775be639fc01f02d23b5fcb70370f11b4c |
File details
Details for the file drf_totp-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: drf_totp-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b466ac4e269c98a0d956d035224b584f7bb8ac27d156ea21372657f43f18d4b |
|
MD5 | 8c062adb7d5479373ed2f15e20374f17 |
|
BLAKE2b-256 | 221e9e900fd01606e8239f7cf0b472483b49f548838e626f487e83957209b7be |