Skip to content

Middleware Example

Get { Authorization } from the { Cookies } and inject it to the { Headers }.


MIDDLEWARE (Starlette)

You can create your own middleware by using the base module.

The BaseMiddleware included is just a wrapper/rename for BaseHTTPMiddleware from Starlette

Import your Basics

# -*- coding: utf-8 -*-
""" [Middleware]
    Get { Authorization } from the { Cookies } and inject it to the { Headers }.
"""

from fastberry import BaseMiddleware

Create your FastAPI / Starlette / Fastberry Middleware

For more information about custom middleware go to starlette: BaseHTTPMiddleware

class AuthenticatedCookieMiddleware(BaseMiddleware):
    """Get Authorization From Cookie"""

    async def dispatch(self, request, call_next):
        """Process Request and Inject Header"""

        if (
            "Authorization" not in request.headers
            and "Authorization" in request.cookies
        ):
            access_token = request.cookies.get("Authorization")
            request.headers.__dict__["_list"].append(
                (
                    "authorization".encode(),
                    f"Bearer {access_token}".encode(),
                )
            )
        response = await call_next(request)
        return response
middleware.py
# -*- coding: utf-8 -*-
""" [Middleware]
    Get { Authorization } from the { Cookies } and inject it to the { Headers }.
"""

from fastberry import BaseMiddleware


class AuthenticatedCookieMiddleware(BaseMiddleware):
    """Get Authorization From Cookie"""

    async def dispatch(self, request, call_next):
        """Process Request and Inject Header"""

        if (
            "Authorization" not in request.headers
            and "Authorization" in request.cookies
        ):
            access_token = request.cookies.get("Authorization")
            request.headers.__dict__["_list"].append(
                (
                    "authorization".encode(),
                    f"Bearer {access_token}".encode(),
                )
            )
        response = await call_next(request)
        return response