Python SDK for the Authlib
Project description
AuthLib
A Python authentication library that provides JWT, OAuth2, and API token authentication with PostgreSQL backend. This library is designed for seamless integration with Flask applications and provides a robust set of endpoints and utilities for user management, authentication, and API token handling.
Table of Contents
Installation
pip install -e .
Quick Start
from flask import Flask
from authlib import AuthManager
app = Flask(__name__)
auth = AuthManager(
app=app,
db_dsn="postgresql://user:pass@localhost/dbname",
jwt_secret="your-secret-key",
oauth_config={
"google": {
"client_id": "your-client-id",
"client_secret": "your-client-secret"
}
}
)
@app.route("/protected")
@auth.require_auth(roles=["admin"])
def protected_route():
return "Protected content"
Configuration
Required Parameters
app: Flask application instancedb_dsn: PostgreSQL connection stringjwt_secret: Secret key for JWT signing
Optional Parameters
oauth_config: Dictionary of OAuth provider configurations (see below)token_expiry: JWT token expiry time in seconds (default: 3600)refresh_token_expiry: Refresh token expiry time in seconds (default: 2592000)
Example oauth_config:
{
"google": {
"client_id": "...",
"client_secret": "..."
},
"github": {
"client_id": "...",
"client_secret": "..."
}
}
API Endpoints
Authentication
POST /v1/users/login- Login with username/password- Request:
{ "username": "string", "password": "string" } - Response:
{ "token": "jwt", "refresh_token": "jwt", "user": { ... } }
- Request:
POST /v1/users/login/oauth- Get OAuth redirect URL- Request:
{ "provider": "google|github|..." } - Response:
{ "redirect_url": "string" }
- Request:
GET /v1/users/login/oauth2callback- OAuth callback- Query Params:
code,state,provider - Response:
{ "token": "jwt", "refresh_token": "jwt", "user": { ... } }
- Query Params:
POST /v1/users/token-refresh- Refresh JWT token- Request:
{ "refresh_token": "jwt" } - Response:
{ "token": "jwt", "refresh_token": "jwt" }
- Request:
User Management
POST /v1/users/register- Register new user- Request:
{ "username": "string", "password": "string", "email": "string", ... } - Response:
{ "user": { ... }, "token": "jwt", "refresh_token": "jwt" }
- Request:
GET /v1/users/login/profile- Get user profile- Auth: Bearer JWT
- Response:
{ "user": { ... } }
GET /v1/users/roles- Get available roles- Response:
[ "admin", "user", ... ]
- Response:
API Tokens
POST /v1/users/{user}/api-tokens- Create API token- Request:
{ "name": "string", "scopes": [ ... ] } - Response:
{ "token": "string", "id": "uuid", ... }
- Request:
GET /v1/users/{user}/api-tokens- List API tokens- Response:
[ { "id": "uuid", "name": "string", ... } ]
- Response:
DELETE /v1/users/{user}/api-tokens/{token_id}- Delete API token- Response:
{ "success": true }
- Response:
Authentication Flow
- Login:
- User submits credentials to
/v1/users/login. - Receives JWT and refresh token.
- User submits credentials to
- Token Refresh:
- Use
/v1/users/token-refreshwith refresh token to get new JWT.
- Use
- OAuth:
- Get redirect URL from
/v1/users/login/oauth. - Complete OAuth flow via
/v1/users/login/oauth2callback.
- Get redirect URL from
- Protected Routes:
- Use
@auth.require_auth()decorator to protect Flask routes.
- Use
User Object
The user object returned by the API typically includes:
{
"id": "uuid",
"username": "string",
"email": "string",
"roles": ["user", "admin"],
"created_at": "timestamp",
"last_login": "timestamp"
}
Token Management
- JWT: Used for authenticating API requests. Include in
Authorization: Bearer <token>header. - Refresh Token: Used to obtain new JWTs without re-authenticating.
- API Tokens: Long-lived tokens for programmatic access, managed per user.
Development
Setup
- Clone the repository
- Create virtual environment:
python -m venv venv
venv\Scripts\activate
- Install dependencies:
pip install -e ".[dev]"
Database Setup
createdb authlib
python -m authlib.cli db init
Running Tests
pytest
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 the37lab_authlib-0.1.1750156111.tar.gz.
File metadata
- Download URL: the37lab_authlib-0.1.1750156111.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c061962b29944c9a3cf6f45dbc2a7cc7c54e0da65e774bd063a42165e2bf2fa
|
|
| MD5 |
c0bda44a38455b8a8b9ff33a2103411c
|
|
| BLAKE2b-256 |
d4f1d90a4c9ca503d53455511f736d99f3dd5452096ccebd64f9452911e464a8
|
File details
Details for the file the37lab_authlib-0.1.1750156111-py3-none-any.whl.
File metadata
- Download URL: the37lab_authlib-0.1.1750156111-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5c9deb840142a4cb73229f35c024ddf6b50b5c1570f1edb71fdd714dfde2a23
|
|
| MD5 |
0a46db204b355dbfe245a2dc643e7629
|
|
| BLAKE2b-256 |
3e495d95b1a036d1d8b492231c31fc6503f3efa0854c15599147ae4eb0c799d7
|