Skip to main content

MBKAUTHEPY

MBKAUTHEPY Logo

PyPI License: MPL 2.0 Python Versions Downloads

mbkauthepy is a fully featured, secure, and extensible authentication system for Python Flask applications.
Ported from the Node.js version to provide seamless multi-language support for full-stack apps.


📚 Table of Contents


✨ Features

Feature Description
🧠 Multi-language Support Use in both Python (mbkauthe) and JavaScript (mbkauthe 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

🧠 Multi-language Support

This package is designed to work seamlessly with both Python and JavaScript applications.

  • The JavaScript version is available on npm as mbkauthe.
  • The Python version is available on PyPI as mbkauthepy.

Repositories:

Contact & Contributions:

Issues / PRs:

We welcome issues and pull requests! Feel free to contribute or ask any questions.


Note: This project is developed and maintained by Maaz Waheed and Muhammad Bin Khalid.

📦 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

3. Install mbkauthepy

pip install mbkauthepy

🚀 Quickstart Example

from flask import Flask, render_template, session
from dotenv import load_dotenv
from mbkauthepy import configure_mbkauthe, validate_session

load_dotenv()

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'

configure_mbkauthe(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

mbkautheVar='{
    "APP_NAME": "MBKAUTH_PYTHON_DEMO",
    "IS_DEPLOYED": "false",
    "LOGIN_DB": "postgresql://username:password@host:port/database",
    "MBKAUTH_TWO_FA_ENABLE": "false",
    "COOKIE_EXPIRE_TIME": "2", # In days
    "DOMAIN": "mbktechstudio.com", # Use your actual domain in production
    "Main_SECRET_TOKEN": "your-secret-token-for-terminate-api", # Added for terminateAllSessions auth
    "loginRedirectURL": "/",
    "EncryptedPassword": "False"
}'

✅ 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 src.mbkauthe 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_mbkauthe(app):

Method Endpoint Description
POST /mbkauthe/api/login Authenticate and create session
POST /mbkauthe/api/logout Terminate current session
POST /mbkauthe/api/terminateAllSessions Clears all sessions (admin only)
GET /mbkauthe/i or /mbkauthe/info or mbkauthe.mbkauthe_info Current package version or metadata from the installed package
GET mbkauthe.login_page or /mbkauthe/login login page in package

🗄️ Database Schema

👉 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.

Note: Encrypted password support is under development. Stay tuned for updates!


📜 License

Mozilla Public License 2.0
See LICENSE for full legal text.


🙋 Contact & Support

Developed by Maaz Waheed


Would you like me to generate:

  • ✅ A requirements.txt
  • ✅ The .env template
  • ✅ Diagrams (e.g., session flow, DB schema)
  • ✅ Frontend login template in HTML?

Let me know which extras you want!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mbkauthepy-1.6.8.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mbkauthepy-1.6.8-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

Details for the file mbkauthepy-1.6.8.tar.gz.

File metadata

  • Download URL: mbkauthepy-1.6.8.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for mbkauthepy-1.6.8.tar.gz
Algorithm Hash digest
SHA256 7f1b747e417b0c878f572746862ce187faa54c93510afc1af30e0d71ec0ca044
MD5 acd24d19586a5b4670fc315c53c8191c
BLAKE2b-256 875b4005aaa4b4fa835a7f4ad69a5aa3e3ddb3612eff06d14ac8b0ace586666e

See more details on using hashes here.

File details

Details for the file mbkauthepy-1.6.8-py3-none-any.whl.

File metadata

  • Download URL: mbkauthepy-1.6.8-py3-none-any.whl
  • Upload date:
  • Size: 48.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for mbkauthepy-1.6.8-py3-none-any.whl
Algorithm Hash digest
SHA256 00dbdda310e1f4a1bf6fff54e2aeae1004e0ef424022f784722c125249626a7d
MD5 befe4dccab5d9013c0c768947e59ee08
BLAKE2b-256 9444d6d291653932e20fd063247d33791088539734ab36f5ab6ab4ddc1c62b04

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.6.8 This release

2 files

1.6.7

2 files

1.6.6

2 files

1.6.5

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.0

2 files

1.4.1

2 files

1.4.0

2 files

1.3.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page