Skip to content

42Wor/mbkauthepy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

38 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MBKAUTHEPY

PyPI License: MPL 2.0 Python Versions

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.


Current Version: 1.6.8 (Python) - Recommended for compatibility with mbkauthe (JavaScript) 1.3.1

Note: This project is actively maintained. We welcome contributions and feedback to improve its features and stability.


πŸ“š 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
@authapi(required_role=None) Decorator for API authentication with optional role enforcement
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 function Description
POST /mbkauthe/api/login mbkauthe.login() Authenticate and create session
GET /mbkauthe/api/logout mbkauthe.logout() Terminate current session
POST /mbkauthe/api/terminateAllSessions mbkauthe.terminate_all_sessions() Clears all sessions (admin only)
GET /mbkauthe/i or /mbkauthe/info mbkauthe.mbkauthe_info() Current package version or metadata from the installed package
GET mbkauthe.login_page() /mbkauthe/login login page in package
GET /mbkauthe/2fa mbkauthe.two_fa_page() 2FA verification page
POST /mbkauthe/api/verify-2fa mbkauthe.verify_2fa() Verify 2FA code

πŸ—„οΈ 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


🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature-branch).
  6. Create a new Pull Request.

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!

About

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.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors