Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for MS SQL Server Token Authentication. #569

Open
akelloway opened this issue Jan 26, 2024 · 1 comment
Open

Support for MS SQL Server Token Authentication. #569

akelloway opened this issue Jan 26, 2024 · 1 comment

Comments

@akelloway
Copy link

I would like to be able to access an Azure SQL Server using the token based authentication method.

Using pyodbc this would look like this:

def get_conn():
    connection_string = 'Driver={ODBC Driver 18 for SQL Server};Server=tcp:<SERVER_ADDRESS>,1433;Database=<DATABASE_NAME>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'    
    credential = DefaultAzureCredential(exclude_interactive_browser_credential=False)
    token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
    token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
    SQL_COPT_SS_ACCESS_TOKEN = 1256  # This connection option is defined by microsoft in msodbcsql.h
    conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
    return conn

which used SQL_COPT_SS_ACCESS_TOKEN further described here in the attrs_before parameter - described as "attrs_before: Set low-level connection attributes before a connection is attempted."

I wonder if this is possible today using connectorX or if this could be added at a later date? (I'm no Rust dev but I see that tiberius does support AADToken auth - perhaps this can be leveraged to enable this feature in connectorX?)

Thanks!

@MuhammadPathan
Copy link

@akelloway Was there a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants