A fully featured, secure, and extensible authentication system for Python Flask applications.Originally ported from the Node.js version to provide multi-language support for full-stack apps.
Project description
๐ mbkauthepy (Python/Flask Version)
A fully featured, secure, and extensible authentication system for Python Flask applications.
Originally ported from the Node.js version to provide multi-language support for full-stack apps.
๐ Table of Contents
- โจ Features
- ๐ฆ Installation
- ๐ Quickstart
- โ๏ธ Configuration (.env)
- ๐งฉ Middleware & Decorators
- ๐งช API Endpoints
- ๐๏ธ Database Schema
- ๐ Security Notes
- ๐ License
- ๐ Contact & Support
โจ Features
| Feature | Description |
|---|---|
| ๐ง Multi-language Support | Use in both Python (mbkauthepy) and JavaScript (mbkauthepy via npm) |
| ๐ Secure Auth | Session-based authentication with secure cookies and optional 2FA |
| ๐งโ๐คโ๐ง Role-based Access | Decorators for validating roles and permissions on protected routes |
| ๐ 2FA Support | Time-based One-Time Password (TOTP) with pyotp |
| ๐ reCAPTCHA v2 Support | Protect login routes with Google reCAPTCHA |
| ๐ช Cookie Management | Secure session cookies with custom expiration, domain, etc. |
| ๐ PostgreSQL Integration | Optimized with connection pooling via psycopg2 |
| ๐ Password Security | Bcrypt hash support (or optional plaintext in dev/test mode) |
| ๐ง Profile Data Access | Built-in helper to fetch user profile details from DB |
๐ฆ Installation
1. Python & Virtual Environment
python -m venv venv
source venv/bin/activate # Linux/macOS
# .\venv\Scripts\activate # Windows
2. Install Dependencies
pip install -r requirements.txt
# OR manually:
pip install Flask Flask-Session psycopg2-binary python-dotenv bcrypt requests pyotp Flask-Cors SQLAlchemy
3. Install mbkauthepy
pip install -e ./mbkauthepy # Local dev
# OR if published:
# pip install mbkauthepy
๐ Quickstart Example
from flask import Flask, render_template, session
from dotenv import load_dotenv
from mbkauthepy import configure_mbkauthepy, validate_session
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
configure_mbkauthepy(app)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/dashboard')
@validate_session
def dashboard():
user = session['user']
return f"Welcome {user['username']}!"
if __name__ == '__main__':
app.run(debug=True)
โ๏ธ Configuration (.env)
FLASK_SECRET_KEY=my-flask-secret
mbkauthepyVar='{
"APP_NAME": "MyApp",
"RECAPTCHA_Enabled": "false",
"SESSION_SECRET_KEY": "super-long-random-secret",
"IS_DEPLOYED": "false",
"LOGIN_DB": "postgresql://user:pass@host:5432/mydb",
"MBKAUTH_TWO_FA_ENABLE": "false",
"COOKIE_EXPIRE_TIME": "7",
"DOMAIN": "localhost",
"Main_SECRET_TOKEN": "internal-api-secret-token",
"SESSION_TYPE": "sqlalchemy",
"SESSION_SQLALCHEMY_TABLE": "session",
"EncryptedPassword": "true"
}'
โ
You can override behavior by editing this JSON string directly in .env.
๐งฉ Middleware & Decorators
| Decorator | Purpose |
|---|---|
@validate_session |
Ensures valid session is active |
@check_role_permission("Role") |
Checks if user has required role |
@validate_session_and_role("Role") |
Shortcut for validating both |
@authenticate_token |
Verifies request via API token header |
Example:
from mbkauthepy import validate_session, check_role_permission, validate_session_and_role, authenticate_token
@app.route('/admin')
@validate_session_and_role("SuperAdmin")
def admin_panel():
return "Welcome to the admin panel"
@app.route('/dashboard')
@validate_session
def dashboard():
user = session['user']
return f"Welcome {user['username']}"
@app.route('/secured-admin')
@validate_session_and_role("SuperAdmin")
def secured_admin():
return "Secured Area"
@app.route('/terminate-sessions')
@authenticate_token
def terminate_sessions():
return {"success": True}
# Example of fetching user data
data = get_user_data("johndoe", ["FullName", "email"])
๐งช API Endpoints
These are available by default after calling configure_mbkauthepy(app):
| Method | Endpoint | Description |
|---|---|---|
| POST | /mbkauthepy/api/login |
Authenticate and create session |
| POST | /mbkauthepy/api/logout |
Terminate current session |
| POST | /mbkauthepy/api/terminateAllSessions |
Clears all sessions (admin only) |
| GET | /mbkauthepy/version |
Current package version |
| GET | /mbkauthepy/package |
Metadata from installed package |
| GET | /mbkauthepy/package-lock |
Dependency info (experimental) |
๐๏ธ Database Schema
| Table | Purpose |
|---|---|
Users |
Stores core user account info |
sess |
Tracks session info per user |
TwoFA |
Stores 2FA TOTP secrets |
profiledata |
(Optional) Extended profile fields |
๐ See docs/db.md for schema & setup scripts.
๐ Security Notes
- ๐ Set
EncryptedPassword: "true"for production use. - โ
Always use long random
SESSION_SECRET_KEY. - ๐ Use HTTPS in deployment (
IS_DEPLOYED: "true"). - ๐ซ Avoid plaintext passwords outside dev/testing.
Need to hash a password?
import bcrypt
hashed = bcrypt.hashpw(b"mypassword", bcrypt.gensalt())
๐ License
Mozilla Public License 2.0
See LICENSE for full legal text.
๐ Contact & Support
Developed by Maaz Waheed
- GitHub: @42Wor
- Issues / PRs welcome!
Would you like me to generate:
- โ
A
requirements.txt - โ
The
.envtemplate - โ Diagrams (e.g., session flow, DB schema)
- โ Frontend login template in HTML?
Let me know which extras you want!
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 mbkauthepy-1.3.0.tar.gz.
File metadata
- Download URL: mbkauthepy-1.3.0.tar.gz
- Upload date:
- Size: 37.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
229147299cc7b5f543154b5722615e4af1e52fe625500860112dc0e544aae77d
|
|
| MD5 |
45cd9ae4622ef27464ea5b57a85b3ad2
|
|
| BLAKE2b-256 |
6969e1299156d10964aadd033448c7e88154444a0fe07c9d819ef6120372eaf9
|
File details
Details for the file mbkauthepy-1.3.0-py3-none-any.whl.
File metadata
- Download URL: mbkauthepy-1.3.0-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
877b03af42c2a4d574e854bdf677d6ed2662327e4f8630f5f73509283f792f82
|
|
| MD5 |
e4deaf61913d93b4f3e1e3f33c1237ca
|
|
| BLAKE2b-256 |
c49ff31901f2bcd5899b020a0ff756ba328409a19518b41db4b05de8bd65c1b1
|