Skip to content

Using oauth_callbacΒ #2779

@sh-js

Description

@sh-js

Hi Chainlit team πŸ‘‹
I’m trying to implement two authentication methods in the same Chainlit app depending on how the app is opened:

βœ… Browser users β†’ OAuth authentication (oauth_callback)

βœ… Microsoft Teams tab users β†’ Header-based authentication (header_auth_callback)

Use case

When the app is opened in a normal browser, I want to use Azure AD OAuth redirect flow.

When the app is opened inside Microsoft Teams (as a tab app, not a bot), Teams injects an Azure AD JWT token in the request headers, so I want to validate it using header_auth_callback.

Current Implementation
In app.py I added:

@cl.oauth_callback
async def oauth_callback(
    provider_id: str,
    token: str,
    raw_user_data: dict[str, str],
    default_user: cl.User,
    id_token: str | None = None,
) -> cl.User | None:
    """
    Handle OAuth authentication for browser users.
    Called after Azure AD redirect with authorization code.
    Sets cookie that authenticates both HTTP and WebSocket requests.
    """
    return await oauth_callback_function(provider_id, token, raw_user_data, default_user, id_token)


@cl.header_auth_callback
async def header_auth_callback(headers: Dict) -> Optional[cl.User]:
    """
    Handle token-based authentication for Teams users.
    Validates Azure AD JWT token from Authorization header.
    Called on each HTTP request that includes: Authorization: Bearer <token>
    WebSocket authentication is handled by socket_patch.py (same validation logic).
    """
    return await header_auth_callback_function(headers)

Problem
When I open the app in a regular browser, Chainlit still triggers header_auth_callback, which causes authentication to fail because the browser request doesn’t include the Teams Authorization header.
It looks like header_auth_callback is executed on every request, even when OAuth should be used instead.

Questions

Is it currently supported to use both oauth_callback and header_auth_callback in the same app?

Is there a recommended way to conditionally choose authentication based on request context (e.g., presence of headers, Teams environment, etc.)?
Should header_auth_callback return None when no Authorization header exists, or is there a better pattern?
Is there any official example combining OAuth with header-based auth?
Goal
I want:
Browser β†’ OAuth login
Teams tab β†’ Header JWT validation
Shared session after authentication
Thanks in advance for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    authPertaining to authentication.needs-triagestaleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions