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

Azure SQL Server Database AD token - Django #83

Open
shakthifuture opened this issue Nov 5, 2020 · 0 comments
Open

Azure SQL Server Database AD token - Django #83

shakthifuture opened this issue Nov 5, 2020 · 0 comments

Comments

@shakthifuture
Copy link

shakthifuture commented Nov 5, 2020

I have successfully connected the Azure SQL Server using AccessToken in the pyodbc. Here I didn't use a username or password to connect to the database. Instead of that, I used attrs_before for pass token. Here I am generating the token automatically.

Token Generation:

identity_endpoint = os.environ["IDENTITY_ENDPOINT"]
identity_header = os.environ["IDENTITY_HEADER"]

def get_bearer_token(resource_uri): #Automattically token will generate
    token_auth_uri = f"{identity_endpoint}?resource={resource_uri}&api-version=2019-08-01"
    head_msi = {'X-IDENTITY-HEADER': identity_header}

    resp = requests.get(token_auth_uri, headers=head_msi)
    access_token = resp.json()['access_token']
    return access_token

Pyodbc Connection:

accessToken = bytes(get_bearer_token("https://database.windows.net/"), 'utf-8');
exptoken = b"";
for i in accessToken:
    exptoken += bytes({i});
    exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;

conn = pyodbc.connect(
      "Driver={ODBC Driver 17 for SQL Server};Server=yoursqlserver.database.windows.net,1433;Database=dbName",
      attrs_before={1256: bytearray(tokenstruct)});

Now the problem is how to use the same configuration using this package to connecting DB? We can't have a username or password to connect the database when using the azure token.

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

1 participant