Skip to main content

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

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": "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 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 Current package version or metadata from the installed 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!

Project details


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.4.1.tar.gz (38.2 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.4.1-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mbkauthepy-1.4.1.tar.gz
Algorithm Hash digest
SHA256 055b7ca25f7d3fe5b5add71604b3851085a2007d047da40455203a7741c616f8
MD5 2e508f39d070f400d5c36a965d4fdbff
BLAKE2b-256 b1ed3fd8369eddf3fcc1b54dccb2f9c664a19484f9091d167632b4c2fbe9df75

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mbkauthepy-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce34bcb54d3e784cda61802ce1829ded2c51473ac8d2eb33295800a8816e4868
MD5 bf174c647a124444ffaa55e84a7bfc00
BLAKE2b-256 7cbe54c787f335659eb3925d05d56044b17df39ae7cdbc27b26f6e15f7590f40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page