Skip to main content

📦 xUserAuth

A modular, production-ready authentication and user management library for FastAPI.

Supports:

  • JWT-based auth (access, refresh, email verification, password reset)
  • Role-based access control (RBAC)
  • Password hashing
  • Google OAuth integration
  • Custom user model support

📌 Installation

pip install xuserauth

Or for development:

git clone https://github.com/yourusername/xuserauth.git
cd xuserauth
pip install -e .

🧠 Core Components

Module Purpose
auth_manager.py Central class for managing auth workflows
jwt_utils.py JWT encoding/decoding helpers
hashing.py Password hashing & verification
roles.py Role checking utilities
exceptions.py Standardized auth errors
schemas.py Pydantic base user schemas
social/google.py Google OAuth2 login/callback

🛠 Setup & Configuration

✅ 1. Define your user model

# myapp/models.py
class User:
    def __init__(self, id, email, password, is_active=True, roles=["user"], email_verified=False):
        self.id = id
        self.email = email
        self.password = password
        self.is_active = is_active
        self.roles = roles
        self.email_verified = email_verified

✅ 2. Define a user loader

async def get_user_by_id(user_id: str):
    # Replace with your DB query logic
    return fake_user_db.get(user_id)

✅ 3. Initialize AuthManager

from xuserauth import AuthManager
from myapp.models import User

auth = AuthManager(
    user_model=User,
    jwt_secret="your_secret_key_here",
    user_loader=get_user_by_id
)

🔐 Usage Examples

🧪 Register / Hash Password

hashed = auth.hash_password("mypassword")

🔐 Login

if auth.verify_password("mypassword", user.password):
    access_token = auth.generate_token(user)
    refresh_token = auth.generate_refresh_token(user)

🔄 Refresh Token

new_token = await auth.refresh_access_token(refresh_token)

🛡 Protect Routes (Auth + Role)

@app.get("/me")
@auth.require_authenticated
async def get_profile(user):
    return {"email": user.email, "roles": user.roles}
@app.get("/admin")
@auth.require_role("admin")
async def get_admin_panel(user):
    return {"message": "Welcome Admin"}

📬 Token Types

Type Use
access Short-lived access token (default)
refresh Refresh token for session renewal
email Email verification token
reset Password reset token

🧪 Google OAuth Login

Redirect to Google:

@app.get("/login/google")
async def google_login(request: Request):
    return await login_with_google(request)

Google Callback:

@app.get("/auth/google/callback")
async def google_callback(request: Request):
    user_info = await auth_google_callback(request)
    # Link or register user in your DB

🔍 Testing

Tests included for:

  • JWT creation/verification
  • Password hashing
  • Role-based access
  • Google OAuth
  • Error handling

Run tests:

pytest test/

⚠️ Exception Classes

  • InvalidToken
  • PermissionDenied
  • UserNotFound
  • AuthError

✅ Schema Examples (Pydantic)

from xuserauth.schemas import UserCreate, UserRead

user = UserCreate(email="a@a.com", password="secure123")

📎 Example Folder Structure

yourapp/
├── main.py
├── models.py
├── routes.py
├── auth/
│   └── auth_manager.py
├── utils/
│   └── hashing.py

🧩 Roadmap

  • ✅ Google login
  • ✅ RBAC
  • ⏳ Facebook login (planned)
  • ⏳ Refresh token rotation
  • ⏳ Database adapters (SQLModel, Tortoise, Prisma)

📝 License

MIT License © 2025 Aliyu Abdulbasit Ayinde


Metadata

Release files for xuserauth 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xuserauth 1.0.2
File Size Uploaded
xuserauth-1.0.2.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xuserauth 1.0.2
File Interpreter ABI Platform
xuserauth-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / xuserauth-1.0.2.tar.gz

Download URL xuserauth-1.0.2.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4acdb6e39db88701eb7439bc09eeb4d7e9a5b4a0eb70fe86c83f67a4c7c9dbed
BLAKE2b-256 checksum
How to use checksums
27fe5ff058d6bc7d99e388373b3fb64b3b96a6cb9d5d0ef0b09dfb1d4e00b07d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / xuserauth-1.0.2-py3-none-any.whl

Download URL xuserauth-1.0.2-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff4e1ea629a65300dcc87c3646b2a897c06da49942a5d7395f265f91d9050dda
BLAKE2b-256 checksum
How to use checksums
0e48452f98b6241fd9b71ee34729845f0864b47e08fcb1f4023178599f1b8138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release 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