chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Support for Azure SQL Access Token Authentication in SQLAlchemyDataLayer

Open marco-bellini opened this issue 4 months ago • 0 comments
trafficstars

Currently, SQLAlchemyDataLayer does not natively support Azure AD access token authentication for SQL Server in a flexible, production-ready, and testable way.

Specific issues:

  • Only user and password authentication are currently allowed.
  • There’s no standard way to supply a function that fetches a fresh access token for each connection (required for Azure AD tokens, which expire).

Proposed Solution

Add a get_access_token callable argument to the SQLAlchemyDataLayer constructor.

This function will be called for every new connection, so the token is always fresh.

Example usage:

  from azure.identity import DefaultAzureCredential
  
  def get_token():
      cred = DefaultAzureCredential()
      return cred.get_token("https://database.windows.net/.default").token
  
  layer = SQLAlchemyDataLayer(
      conninfo="mssql+pyodbc:///?odbc_connect=...",
      odbc_str="Driver={ODBC Driver 18 for SQL Server};...",
      use_token_auth=True,
      get_access_token=get_token,
  )

marco-bellini avatar Jul 10 '25 21:07 marco-bellini