codemation / easyauth

Create a centralized Authentication and Authorization token server. Easily secure FastAPI endpoints based on Users, Groups, Roles or Permissions with very little database usage.
https://easyauth.readthedocs.io/en/latest/
MIT License
553 stars 52 forks source link

How to add routes of APIRouter to easy auth api router #99

Open aghure opened 1 year ago

aghure commented 1 year ago

I am trying to use easy auth with fast api class base view. To do the same , i need to have APIRouter.routes type property to easyAuthAPIRouter. Is there any way to add the same to EasyAuthAPIRouter.

codemation commented 1 year ago

Hello @aghure , every EasyAuthAPIRouter has an api_router attribute which is a direct reference to the APIRouter instance created, perhaps you can reference the routes property here? If that does not work for you, I would need a more substantial code example of what you are trying to accomplish along with additional context.

aghure commented 1 year ago

Hi @codemation I am trying to use the EasyAuthAPIRouter with class base views. Adding example below about what i am trying to do and adding sample output below.


import logging
from sqlalchemy.orm import  Session
from fastapi import APIRouter, Request, Body ,Depends
from fastapi.responses import JSONResponse

from fastapi_utils.cbv import cbv
from ml_app.core import  get_db
from easyauth.router import  EasyAuthAPIRouter
logger = logging.getLogger(__name__)
test_cbv_router = EasyAuthAPIRouter.create(prefix='/finance', tags=['finance'])
@cbv(test_cbv_router.server)  # Step 2: Create and decorate a class to hold the endpoints
class TestCBV:

    db_session : Session = Depends(get_db)
    @test_cbv_router.post("/test")
    def test_url(self,request: Request):
        return  JSONResponse({'status':'ok'})
@test_cbv_router.get("/existing")
def existing_contact(request: Request,db_session: Session = Depends(get_db)):

    result = dict()
    result["result_data"] = ["contact_list"]
    result["tmpl_name"] = "existing.html"
    return  result

image

The @cbv is class base view router of https://github.com/dmontagu/fastapi-utils, it needs APIRouter() instance to add the routes in the app. We want to use EasyAuthAPIRouter with class base views. So with EasyAuthAPIRouter.server we get APIRouter() , but CBV is adding it in base app . Here I have highlighted the URL issues happening because of the same.