Skip to main content

Python (FastAPI) Integration Guide

This guide provides an asynchronous implementation example for integrating Mubarokah ID OAuth 2.0 into a Python application using the FastAPI framework. It covers Pydantic models for data validation, an OAuth service for interacting with Mubarokah ID, token management, and example API endpoints.

Prerequisites

  • Python 3.7+
  • FastAPI: pip install fastapi uvicorn
  • HTTPX for async HTTP requests: pip install httpx
  • Pydantic for data validation (comes with FastAPI).
  • Optional: redis for token storage: pip install redis
  • Optional: cryptography for encrypting tokens at rest: pip install cryptography

Project Structure (Conceptual)

Configuration (core/config.py)

.env file example:

Pydantic Models (models/oauth.py)

Mubarokah OAuth Service (services/oauth_service.py)

Token Service (services/token_service.py)

The TokenService uses Redis and Fernet encryption. Ensure Redis is running and TOKEN_ENCRYPTION_KEY is securely generated and stored.

Security Utilities (core/security.py)

Auth Routes (routers/auth.py)

Main Application Setup (main.py)

Security & Production Notes:
  • The APP_SECRET_KEY for SessionMiddleware must be strong and kept secret.
  • Ensure TOKEN_ENCRYPTION_KEY for TokenService is securely generated and managed.
  • In production, configure session cookies with secure=True (HTTPS only) and appropriate SameSite attributes.
This FastAPI example provides a robust, asynchronous integration with Mubarokah ID.