Skip to content

extending to support oauth2.0  #107

Open

Description

@knail2

I really like your project. the architecture is elegant, and it is using the components I need for a production level deployment (langchain, and fastapi). your code is also clean and easy to understand, and your videos helped grok it too.

two questions for you:

  1. I don't see where I can store and changed the basic auth capabilities on the fastapi server side. Atleast its not clear in the code or in the readme. can you clarify?
  2. I am considering extending your fastapi service to support oauth2.0, as I will need that capability to push to production. however that does require some storage capabilities, like secrets, token, scopes. there are going to be two target platforms I'll need to support. These are Heroku and Snowflake containers. After researching, the most optimum place to store state for Heroku would be in its postgreSQL service and for snowflake would obviously be in its own database. Please let me know what you think on this oauth2.0 extension. would you be interested in me sending you a pull request, or shall I fork off your code and maintain all this on my side?

thank you and keep up the great work
Omer

Activity

  1. JoshuaC215 commented on Nov 30, 2024

    @JoshuaC215
    Owner

    Hi Omer, glad you like the repo and thanks for your kind words!

    Re: updating the current auth - the simple ENV-configured header secret is set up in verify_bearer(), which is a dependency on the router that all the key routes are attached to.

    I don't know if I'd want to add Heroku or Snowflake containers specific implementations directly in the core service since it's intended to be relatively generic. If there's a way to add it as an example or something on the side I would be interested! Also if it could be set up in a generic way in the core service, I might be interested. Certainly if you post any public fork with this code please do share it back as I'm sure others will be interested.

    Just a note maybe worth considering whether OAuth2 or OIDC is the best to use. In some of my recent projects I was looking more at OIDC as being a bit more modern (e.g. see discussion here)

  2. xytian315 commented on May 12, 2025

    @xytian315
    Contributor

    FYI: I did implemented Azure Auth in my application, which is really durable. I used https://github.com/intility/fastapi-azure-auth library. And I added a few code in service.py to achieve this.

    here is some sample code I added in service.py, maybe it is helpful for other people.

    AzureScheme = SingleTenantAzureAuthorizationCodeBearer(
        app_client_id=settings.APP_CLIENT_ID,
        tenant_id=settings.TENANT_ID,
        scopes=settings.SCOPES,
    )
    
    async def load_config() -> None:
        """
        Load OpenID config on startup.
        """
        await AzureScheme.openid_config.load_config()
    
    async def startup_event():
        """ Load OpenID config on startup """
        await load_config()
    
    app = FastAPI(
        lifespan=lifespan,
        version="0.0.1",
        servers=[
            {
                "url": "http://localhost:8080",
                "description": "Local Development Server"
            },
        ],
        swagger_ui_oauth2_redirect_url='/',
        swagger_ui_init_oauth={
           'usePkceWithAuthorizationCodeGrant': True,
           'clientId': settings.OPENAPI_CLIENT_ID, 
           'scopes': settings.SCOPE_NAME,
        },
    )
    router = APIRouter(dependencies=[Security(AzureScheme)])
    
    async def get_current_user(user: User = Security(AzureScheme)) -> User:
        """get current user"""
        return user
    
    @router.get("/info")
    async def info(user: User = Depends(get_current_user) ) -> ServiceMetadata:
        """Get service metadata."""
        logger.debug("User INFO: %s", user)
        models = list(settings.AVAILABLE_MODELS)
        models.sort()
        return ServiceMetadata(
            agents=get_all_agent_info(),
            models=models,
            default_agent=DEFAULT_AGENT,
            default_model=settings.DEFAULT_MODEL,
            user_display_name=user.name,
        )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions