Monday, 11 September 2023

Adding OAuth2 token based authentication in my fast api code and swagger

I am having a fastapi python application with a few get requests. I am having a ping-sso based identity provider. Now in all the apis in header Bearer token is being passed. How to do authentication and authorization(not required but will be informational) with the bearer token passed in the header.

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from feast import FeatureStore


app = FastAPI()
store=FeatureStore()

origins = [
"http://localhost",
"http://localhost:8080",
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)




@app.get("/")
async def main():
    return {"message": "Hello World"}

@app.get('/list_data_sources')
async def list_data_sources():
    data_sources = store.list_data_sources()
    data_source_names = [ds.name for ds in data_sources]
    return (data_source_names)

@app.get('/list_entities')
def list_entities():
    entities = store.list_entities()
    entity_names = [entity.name for entity in entities]
    return (entity_names)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=5000)

Now once the token validation is implemented i need my swagger page also to be updated with Bearer token Authorization. swagger

I am using Ping-SSO OpenId.



from Adding OAuth2 token based authentication in my fast api code and swagger

No comments:

Post a Comment