fromtypingimportTuple,ListimportuvicornfromfastapiimportFastAPIfromstarlette.authenticationimportrequiresfromstarlette.requestsimportRequestfromfastapi_auth_middlewareimportAuthMiddleware,FastAPIUser# The method you have to providedefverify_authorization_header(auth_header:str)->Tuple[List[str],FastAPIUser]:user=FastAPIUser(first_name="Code",last_name="Specialist",user_id=1)# Usually you would decode the JWT here and verify its signature to extract the 'sub'scopes=["admin"]# You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewherereturnscopes,userapp=FastAPI()app.add_middleware(AuthMiddleware,verify_authorization_header=verify_authorization_header)# Add the middleware with your verification method to the whole application@app.get('/home')# Sample endpoint (secured)@requires("admin")# Requires the role 'admin' (Will succeed)defhome(request:Request):returnrequest.user# Returns the user object that is injected into the request. The FastAPIUser in this case@app.get('/poweruser')# Sample endpoint (secured)@requires(["admin","poweruser"])# Requires the roles 'admin' and 'poweruser' (Will fail)defpoweruser(request:Request):returnrequest.user# Returns the user object that is injected into the request. The FastAPIUser in this caseif__name__=='__main__':uvicorn.run('simple_with_scopes:app',host="0.0.0.0",port=8080)# Starts the uvicorn ASGI