The middleware module for LitePolis
Project description
LitePolis Middleware Example: JWT Authentication
This repository was generated using the LitePolis command-line interface:
litepolis-cli create middleware litepolis-middleware-example
:warning: Naming Convention: Always keep the prefix "litepolis-middleware-" in your project directory name and "litepolis_middleware_" in your Python package directory name. This ensures the LitePolis package manager can recognize and integrate your middleware during deployment.
What is LitePolis Middleware?
Middleware in LitePolis acts like a gatekeeper or processor for incoming requests to your web application. It can perform tasks like:
- Authentication & Authorization (like this JWT example!)
- Logging requests
- Modifying request/response headers
- Data validation
- Rate limiting
- And much more!
By building custom middleware, you can encapsulate reusable logic and keep your main application code clean.
How to use
Add middleware to your LitePolis application:
litepolis-cli deploy add-deps litepolis-middleware-example
Add the middleware to FastAPI routes:
from fastapi import FastAPI, Depends, Request
from starlette.responses import JSONResponse
from starlette.authentication import requires
# Other necessary imports for your application logic
app = FastAPI() # Your FastAPI app instance managed by LitePolis
# ... other app setup
@app.get("/users/me")
@requires("authenticated") # Apply the decorator here
async def read_current_user(request: Request):
# This endpoint is now protected.
# Code here will only execute if the middleware successfully authenticates the request.
user_email = request.user.identity # Access the authenticated user's identity (email in this case)
# Or access other properties if the middleware returns a different user object type
# user_display_name = request.user.display_name
return JSONResponse({
"message": f"Hello, authenticated user!",
"email": user_email,
# ... return other relevant user data
})
@app.get("/public-data")
async def read_public_data():
# This endpoint is *not* protected by the @requires decorator
return {"message": "This data is available to everyone."}
# ... other endpoints
Tutorial: From Template to Middleware
Prerequisites: Install the LitePolis command-line tool:
pip install litepolis
Let's walk through the process of creating this middleware example.
Step 1: Create Your Middleware Project
# Replace 'your-middleware-name' with your desired name
litepolis-cli create middleware litepolis-middleware-example
cd litepolis-middleware-example
Step 2: Understand the Project Structure
Your generated project will have the following key files and directories:
setup.py: Configures your Python package (name, version, dependencies). You'll edit this before publishing.requirements.txt: Lists development/testing dependencies.litepolis_middleware_example/: This is your actual Python package directory.__init__.py: Makes the directory a Python package and exports key components (likeadd_middlewareandDEFAULT_CONFIG).core.py: The heart of your middleware. Contains the main logic, including theadd_middlewarefunction and default configuration (DEFAULT_CONFIG).utils.py: (Optional) For helper functions used by your middleware. In this example, it hasverify_user_credentials.
tests/: Contains unit and integration tests for your middleware.test_core.py: Tests the functionality defined incore.py.test_exports.py: Ensures necessary components are correctly exported, DO NOT EDIT.utils.py: Test utilities.
README.md: (This file!) Documentation for your middleware.LICENSE: Your project's license file.
Step 3: Implement Middleware Logic (core.py)
Open litepolis_middleware_example/core.py. This is where the magic happens.
add_middleware(app)function: This function is essential. LitePolis calls this function to add your middleware to a FastAPI application instance (app). You useapp.add_middleware(...)here.- In this example, it adds Starlette's
AuthenticationMiddlewareand provides our customJWTAuthbackend.
- In this example, it adds Starlette's
- Middleware Backend Class (e.g.,
JWTAuth): This class contains the core logic executed for each request.- It typically inherits from
starlette.authentication.AuthenticationBackend. - The
authenticate(self, request)method is crucial. It inspects the incomingrequest(e.g., checks headers for a token), validates credentials, and returnsAuthCredentialsand a user object (likeSimpleUser) upon success, orNoneotherwise. - Our example
JWTAuthlooks for aBearertoken in theAuthorizationheader, decodes it using a secret key, and extracts the user's email.
- It typically inherits from
Step 4: Define Configuration (core.py)
DEFAULT_CONFIGDictionary: Define default configuration values for your middleware here. These values are registered with LitePolis when your middleware is deployed.- Example:
DEFAULT_CONFIG = {"secret_key": "your-default-secret"}.
- Example:
- Accessing Configuration: Inside your middleware (like in
JWTAuth.authenticate), you need to access the active configuration, which might override the defaults. Use the pattern shown incore.pywhich checksos.environto determine if running under Pytest (useDEFAULT_CONFIG) or in a live environment (uselitepolis.get_config).
import os
# Define default values suitable for the testing environment
DEFAULT_TEST_CONFIG = {
"secret_key": "your-default-secret"
# Add other necessary default test config values here
}
# Check if running under Pytest
if "PYTEST_CURRENT_TEST" not in os.environ and "PYTEST_VERSION" not in os.environ:
# NOT running under Pytest: Fetch from live source (replace with actual logic)
print("Fetching configuration from live LitePolis...")
secret_key = get_config("litepolis_middleware_example", "secret_key")
else:
# Running under Pytest: Use default test values
print("Running under Pytest. Using default test configuration.")
secret_key = DEFAULT_TEST_CONFIG["secret_key"]
# Use the determined configuration values
print(f"UI Refresh Interval: {refresh_interval}")
print(f"Show Notifications: {show_notifications}")
Step 5: Write Tests (tests/test_core.py)
Testing is crucial! Open tests/test_core.py.
- Use
fastapi.testclient.TestClientto make simulated requests to a test FastAPI app instance that includes your middleware. - Write tests for different scenarios:
- Valid authentication (e.g., correct JWT).
- Invalid authentication (e.g., bad token, expired token).
- Missing credentials.
- Ensure your tests cover the logic in your middleware backend (
JWTAuth) and any helper functions (verify_user_credentials). - Run tests using
pytestin your terminal.
Step 6: Package Your Middleware (setup.py)
Before sharing, configure setup.py:
name: Changelitepolis-middleware-exampleto your unique package name (e.g.,litepolis-middleware-myauth). Remember the prefix!version: Set an initial version (e.g.,"0.1.0").author,url,description: Update these fields.install_requires: List runtime dependencies needed by your middleware (e.g.,pyjwt,litepolis).sqlmodelis listed in the example.
Step 7: Publish To make your middleware easily installable, publish it to the Python Package Index (PyPI). Follow standard Python packaging guides for this.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litepolis_middleware_example-0.0.2.tar.gz.
File metadata
- Download URL: litepolis_middleware_example-0.0.2.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e84fa76b4ead695ff3226b81ba4fac9fe7a48c2149aa5e60b0a45dd9da287bd9
|
|
| MD5 |
d7d1c77acfafd2dc65fe301b4347ee8b
|
|
| BLAKE2b-256 |
b0ef2299a3dff875123d1f736f305dc89d9516bf953953646106ab8885bab084
|
Provenance
The following attestation bundles were made for litepolis_middleware_example-0.0.2.tar.gz:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-middleware-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_middleware_example-0.0.2.tar.gz -
Subject digest:
e84fa76b4ead695ff3226b81ba4fac9fe7a48c2149aa5e60b0a45dd9da287bd9 - Sigstore transparency entry: 191673791
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-middleware-example@a92bd4995430649413b2638b98a1e919a826fe87 -
Branch / Tag:
refs/tags/example-fix - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a92bd4995430649413b2638b98a1e919a826fe87 -
Trigger Event:
release
-
Statement type:
File details
Details for the file litepolis_middleware_example-0.0.2-py3-none-any.whl.
File metadata
- Download URL: litepolis_middleware_example-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f19743c1ec885df6cfd013506fad7059b38bbd48d4d46990710d164dfa1f006
|
|
| MD5 |
f691a8db13118b6b5cc98efc66021ea8
|
|
| BLAKE2b-256 |
baa68f5b97be928d52a44ca83dbb904bfa5d805ee3cc0af6e4077b4021a8a5b5
|
Provenance
The following attestation bundles were made for litepolis_middleware_example-0.0.2-py3-none-any.whl:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-middleware-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_middleware_example-0.0.2-py3-none-any.whl -
Subject digest:
6f19743c1ec885df6cfd013506fad7059b38bbd48d4d46990710d164dfa1f006 - Sigstore transparency entry: 191673798
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-middleware-example@a92bd4995430649413b2638b98a1e919a826fe87 -
Branch / Tag:
refs/tags/example-fix - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a92bd4995430649413b2638b98a1e919a826fe87 -
Trigger Event:
release
-
Statement type: