Skip to main content

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

  1. Install the fastApi-context package:

    pip install fastApi-context
    
  2. Initialize your fastapi client

    from fastapi import FastAPI
    
    app = FastAPI()
    
  3. Configure plugins

    1. JwtAuthPlugin
      pip install fastApi-context[jwt]
      
    • for JWT authentication you can use the JwtAuthPlugin plugin. 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
            )
        )
      
    1. RedisAuthPlugin
      pip install fastApi-context[redis]
      
    • for Redis authentication you can use the RedisAuthPlugin plugin. 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,
            )
      )
      
  4. 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


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.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_context-1.2.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_context-1.2.0.tar.gz.

File metadata

  • Download URL: fastapi_context-1.2.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_context-1.2.0.tar.gz
Algorithm Hash digest
SHA256 dda60173ce45de172d3b79753498e9969624d5df342fcd20cd9684d7e895f05f
MD5 49c211fa39880b8502d13234d9aa1282
BLAKE2b-256 7b5e3d75d0ce5387f863c8bf64544b984ca595e2fc5f850d8fb1a04fe224c662

See more details on using hashes here.

File details

Details for the file fastapi_context-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_context-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42da934165b0a1558ab0746f140cfd6faf369c6f7d8651e03f06378178663ce3
MD5 7fe2bd47813431940db9f0e3b15cdfc4
BLAKE2b-256 d3d2f3398e6e0afc5633d41618bd9ce844da51910b662423b6ed7e1326c091ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page