Auth check and more plugins for fastApi
Project description
Auth check and more plugins for fastApi
Getting Started
provide SDK in the following programming languages:
- Python
Python
-
Install the
fastApi-contextpackage:pip install fastApi-context
-
Initialize your fastapi client
from fastapi import FastAPI app = FastAPI()
-
Configure plugins
- JwtAuthPlugin
pip install fastApi-context[jwt]
- for JWT authentication you can use the
JwtAuthPluginplugin. This plugin allows you to configure JWT authentication for your FastAPI application.from fastapi_context.config import JWTAuthPluginConfig from fastapi_context.plugins.auth_plugin import JwtAuthPlugin jwt_auth_plugin = JwtAuthPlugin( auth_plugin_config=JWTAuthPluginConfig( key="jwt payload key in context", url_white_list=["url white list"], user_class=None, # user class for decode jwt payload get_token="token", # get jwt token way, value is string or callable jwt_secret="Your App Jwt secret", jwt_algorithms=["HS256"], # Your App Jwt algorithms ) )
- RedisAuthPlugin
pip install fastApi-context[redis]
- for Redis authentication you can use the
RedisAuthPluginplugin. This plugin allows you to configure Redis authentication for your FastAPI application.from fastapi_context.config import RedisConfig, RedisAuthPluginConfig from fastapi_context.plugins.auth_plugin import RedisAuthPlugin redis_auth_plugin = RedisAuthPlugin( auth_plugin_config=RedisAuthPluginConfig( key="redis payload key in context", url_white_list=["url white list"], user_class=None, # user class for decode jwt payload get_token="token", # get jwt token way, value is string or callable redis_token_key_prefix="your redis token key prefix", # config redis client # redis_client_function or redis_config must be provided. At least one of these two parameters needs to be set in order for the configuration to work correctly. redis_config=RedisConfig( host=settings.Redis.HOST, port=settings.Redis.PORT, db=settings.Redis.TOKEN_DB_NUM, password=settings.Redis.PASSWORD, decode_responses=True, ), # sync or async both support, see redis.Redis or redis.asyncio.client.Redis, Prioritize async selection # redis_client_function=function, ) )
- JwtAuthPlugin
-
Register middleware
from fastapi_context.config import ContextConfig, JsonResponseConfig from fastapi_context.exceptions import ContextMiddlewareError from fastapi_context.context_middleware import FastAPiContextMiddleware from starlette.responses import JSONResponse from pydantic import BaseModel, Field # when you set `JSONResponse` for error_response, you need to define a custom response model or use default model `JsonResponseConfig`. # You can define your own BaseModel and implement a custom return type by overriding the create_model_by_error method. class MyJsonResponseConfig(BaseModel): status: int = Field(default=0, title="status") data: Any = Field(default=None, title="data") msg: str = Field(default="", title="message") @classmethod def create_model_by_error(cls, error: ContextMiddlewareError): """ create json data by context error """ return cls(status=error.error_code, data=error.data, msg=error.message) app.add_middleware( FastAPiContextMiddleware, context_config=ContextConfig( plugins=[ jwt_auth_plugin, # redis_auth_plugin, ], error_response=JSONResponse, json_data_class=MyJsonResponseConfig, ), )
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
fastapi_context-1.2.1.tar.gz
(11.4 kB
view details)
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 fastapi_context-1.2.1.tar.gz.
File metadata
- Download URL: fastapi_context-1.2.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b410f6256d78c6b1b87803b283f2084363a80151c610520a01b897c8088d316
|
|
| MD5 |
b03e59055ed88a30a691b0682b0d3cc2
|
|
| BLAKE2b-256 |
bba5d81cdf99d4b80844318027450bbd9fd2ed9b9c07a6a15a7772088ecf68da
|
File details
Details for the file fastapi_context-1.2.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_context-1.2.1-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa61fe38502fd2c4896a4999e908730076904eda8e768a79b32ddc696c8e740c
|
|
| MD5 |
02df6780ae09cba2d69da133ac17a748
|
|
| BLAKE2b-256 |
78e743e237a57bd909c7ca8c8a7d6cfdec5e614ec9e94a9810243418000f1d89
|