Skip to main content

A reusable authentication module for Python applications

Project description

Auth Module

A reusable authentication module for Python applications, supporting multiple authentication methods (email, phone, MFA, etc.).

Overview

This module provides a flexible and secure authentication system for Python projects. It supports user registration, login, multi-factor authentication (MFA), and verification via email, phone, or third-party services (e.g., WeChat, DingTalk). The module is designed to be modular, configurable, and reusable across different projects.

Features

  • User registration and login with password hashing (using Argon2).
  • Multi-factor authentication (MFA) support.
  • Verification via email, phone (SMS), or third-party login.
  • JWT-based token authentication.
  • Extensible storage interfaces for databases or other storage systems.
  • Logging and auditing for security.

Installation

To install the auth_module, use pip:

pip install auth_module
Alternatively, clone this repository and install locally:
bash
git clone https://github.com/drunksoul2021/auth_module.git
cd auth_module
pip install .
Requirements
Python 3.8 or higher
Dependencies: pyjwt==2.8.0, python-dotenv==1.0.0, argon2-cffi==23.1.0
Usage
Here’s a simple example to use the authentication module with a Flask application:
python
from flask import Flask, request, jsonify
from auth_module import init_auth

app = Flask(__name__)
auth = init_auth('.env')  # Load configuration from .env file

# Example storage implementation (using a mock for demonstration)
class MockStorage:
    def find_by_username(self, username):
        return {"user_id": 1, "username": username, "password_hash": "hashed", "salt": "salt"}
    def save_user(self, user_data): pass
    def update_user(self, user_id, updates): pass
    def log_login(self, user_id, login_type, ip, status, fail_reason=None): pass

# Set up storage
auth.set_storage(MockStorage())

@app.route('/auth/register', methods=['POST'])
def register():
    data = request.json
    result = auth.register(data['username'], data['password'])
    return jsonify(result)

@app.route('/auth/login', methods=['POST'])
def login():
    data = request.json
    result = auth.login(data['username'], data['password'], request.remote_addr)
    return jsonify(result)

if __name__ == '__main__':
    app.run(debug=True)
Configuration
The module uses environment variables or a .env file for configuration. Create a .env file in the root directory with the following structure:
plaintext
JWT_SECRET=your_jwt_secret_here
JWT_EXPIRES=86400  # Token expiration in seconds (24 hours)
AUTH_METHODS=email,phone,mfa
DB_HOST=localhost
DB_PORT=3306
DB_NAME=auth_db
DB_USER=root
DB_PASSWORD=your_db_password
SMS_PROVIDER=twilio
SMS_API_KEY=your_sms_api_key
SMS_API_SECRET=your_sms_api_secret
WECHAT_APP_ID=your_wechat_app_id
WECHAT_APP_SECRET=your_wechat_app_secret
DINGTALK_APP_KEY=your_dingtalk_app_key
DINGTALK_APP_SECRET=your_dingtalk_app_secret
An example .env.example is provided in the repository.
API Reference
The module exposes the following endpoints via the AuthController class:
POST /auth/register  
Payload: {"username": "string", "password": "string"}  
Response: {"code": int, "message": "string", "data": {"username": "string", "user_id": int}}
POST /auth/login  
Payload: {"username": "string", "password": "string"}  
Response: {"code": int, "message": "string", "data": {"token": "string"}}
POST /auth/send_verify_code  
Payload: {"user_id": int, "verify_type": "string", "content": "string"}  
Response: {"code": int, "message": "string", "data": {"code": "string"}}
POST /auth/verify_code  
Payload: {"content": "string", "code": "string"}  
Response: {"code": int, "message": "string", "data": null}
For more details, see the docs/ directory or the source code.
Testing
Run the tests using pytest:
bash
pytest tests/ -v
Contributing
Contributions are welcome! Please fork the repository, make changes, and submit a pull request. Ensure tests pass and add new tests if necessary.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
Based on best practices from Python packaging and authentication standards.
Thanks to the open-source community for tools like pytest, pyjwt, and argon2-cffi.

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

auth_module-0.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

auth_module-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file auth_module-0.1.0.tar.gz.

File metadata

  • Download URL: auth_module-0.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for auth_module-0.1.0.tar.gz
Algorithm Hash digest
SHA256 30b57e78d353ae0643617ba6317950c8822c618b2a14b35632499951378621be
MD5 dc673f7bc860d4b00495a4d2618b85fa
BLAKE2b-256 c93b8eec2fb35d08ddd559cf85322ad883f3283eecd39014006dee6e46fa650d

See more details on using hashes here.

File details

Details for the file auth_module-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: auth_module-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for auth_module-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a7b3ec863296e68e71f7aa3f107e028825eb5f3e14f130beefabbef40cf9e60
MD5 afc78f004bbde14fb3a4cda8b77a0eee
BLAKE2b-256 7d4785c12a4c520e1280f69bfba48576944812352293945225265b994b8162d8

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