Using built-in middlewares for authentication
FastAPI can use Starlette middleware such as AuthenticationMiddleware to implement any custom authentication. It needs AuthenticationBackend to implement the scheme for our app’s security model. The following custom AuthenticationBackend checks whether the Authorization credential is a Bearer class and verifies whether the username token is equivalent to a fixed username credential provided by the middleware:
class UsernameAuthBackend(AuthenticationBackend):     def __init__(self, username):         self.username = username                async def authenticate(self, request):         if "Authorization" not in request.headers:             return       ...