Integrate JWT into FastAPI using TP Servers authentication.
Project description
fastapi-tj-tpauth-jwt
Integrate JWT into FastAPI using TP Servers authentication.
Installation
pip install fastapi-tj-tpauth-jwt
Usage
Basic Example
from datetime import timedelta
from typing import Annotated
from fastapi import FastAPI, Depends
from tj_tpauth import TJTPAuth, TPAuthData
from fastapi_tj_tpauth_jwt.tpauth_jwt import TPAuthJWT
app = FastAPI()
tpauth = TJTPAuth(
host="localhost:8080"
)
tpauth_jwt = TPAuthJWT(
tp_auth=tpauth,
secret_key="<SECRET_KEY>",
refresh_secret_key="<SECRET_KEY>",
access_token_expires_in=timedelta(minutes=60),
refresh_token_expires_in=timedelta(minutes=120),
algorithm='HS256'
)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/login")
async def login(
login_res: Annotated[
tpauth_jwt.provide_login_handler(), Depends()
]
):
return login_res
@app.post("/refresh")
async def refresh(
refresh_res: Annotated[
tpauth_jwt.provide_refresh_token_handler(), Depends()
]
):
return refresh_res
@app.get("/secret_data")
async def secret_data(
payload: Annotated[
tpauth_jwt.provide_require_jwt(), Depends()
]
):
if not isinstance(payload, TPAuthData):
return payload
payload: TPAuthData
return {
"payload": payload.id,
}
Custom Unauthorized Error Response
def unauthorized_response_provider(tpauth_status: TPAuthStatus):
return {
"error": "Unauthorized",
"status": tpauth_status.error.value
}
tpauth_jwt.unauthorized_response_provider = unauthorized_response_provider
Custom JWT Response
def jwt_provider(access_token: str, refresh_token: str, token_type: str):
return {
"access_token": access_token,
"refresh_token": refresh_token,
"token_type": token_type
}
tpauth_jwt.jwt_response_provider = jwt_provider
License
This library is released under the MIT License.
Contact
If you have any questions or issues, please open an issue on GitHub or email us at duynguyen02.dev@gmail.com.
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
File details
Details for the file fastapi_tj_tpauth_jwt-1.0.0.tar.gz
.
File metadata
- Download URL: fastapi_tj_tpauth_jwt-1.0.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-41-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01c9b2c678c0517b0f45affcbf884748e310fe662a9f253cede4515d683d397d |
|
MD5 | 630db9c81a506429e726f59cb09f74c2 |
|
BLAKE2b-256 | 34d7adac3525faa29d16a284390f8d383ef51554fc0af91dcd0c3e403083ab14 |
File details
Details for the file fastapi_tj_tpauth_jwt-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_tj_tpauth_jwt-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-41-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 418c28e71ae5fc9ca362801e1c3dde2522b85fc112ed78ec2ff22fe96c49a67a |
|
MD5 | 30d0e2a1504e39e8f9f4ba6d1d92612f |
|
BLAKE2b-256 | 826b43629f2f82107d6b35339622cf40ee0b0f73eb83606306da076c449fb472 |