Skip to content

I need a way to refresh the token when it expires on the datasource for azure-active-directory-access-token type of authentication #10223

@stelianabudai

Description

@stelianabudai

Feature Description

Hi,
I am using managed identities from Azure for authenticating to Sql server database.
I am creating a datasource:
new DataSource({
type: 'mssql',
host: ...
port: ...
authentication: {
type: "azure-active-directory-access-token",
options: {
token: await secretsService.getDbAuthToken(),
},
},

the datasource it is created at the start of the application, however the token from Azure expires after 24 hours, and I need a way to refresh,
I have tried to use the setOptions method from the Datasource but it doesn' t have any effect on the token already set on the connection pool.

The only way I could refresh the token is by getting the connection pool and refreshing it periodically

setInterval(async() => {
const connectionPool= await dataSource.driver.obtainMasterConnection()
connectionPool.config.authentication.options.token=

The Solution

is it possible to offer a way to refresh the token at a specific interval, maybe token can be a callback?

for example some tedius implemented that tediousjs/tedious#1144

mssql has that

const sql = require('mssql');
const { PublicClientApplication } = require('@azure/msal-node');

const config = {
server: 'teststeliana.database.windows.net',
database: 'teststeliana',
authentication: {
type: 'azure-active-directory-default',
options: {
tokenProvider: async () => {
const pca = new PublicClientApplication({
auth: {
},
});
const tokenRequest = {
scopes: ['https://database.windows.net/.default'],
};
const response = await pca.acquireTokenByDeviceCode(tokenRequest);
return response.accessToken;
},
},
},
options: {
encrypt: true,
},
};

Considered Alternatives

Currently I am using something like this:

setInterval(async() => {
const connectionPool= await dataSource.driver.obtainMasterConnection()
connectionPool.config.authentication.options.token=

And I manipulate the connection pool configuration directly

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions