Skip to content

fix(NODE-6801): set token on connection from cache #4438

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

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cmap/auth/mongodb_oidc/machine_workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export abstract class MachineWorkflow implements Workflow {
credentials: MongoCredentials
): Promise<string> {
if (this.cache.hasAccessToken) {
return this.cache.getAccessToken();
const token = this.cache.getAccessToken();
// New connections won't have an access token so ensure we set here.
if (!connection.accessToken) {
connection.accessToken = token;
}
return token;
} else {
const token = await this.callback(credentials);
this.cache.put({ accessToken: token.access_token, expiresInSeconds: token.expires_in });
Expand Down
24 changes: 24 additions & 0 deletions test/unit/cmap/auth/mongodb_oidc/gcp_machine_workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,28 @@ describe('GCPMachineFlow', function () {
});
});
});

describe('#getTokenFromCacheOrEnv', function () {
context('when the cache has a token', function () {
const connection = sinon.createStubInstance(Connection);
const credentials = sinon.createStubInstance(MongoCredentials);

context('when the connection has no token', function () {
let cache;
let workflow;

this.beforeEach(function () {
cache = new TokenCache();
cache.put({ accessToken: 'test', expiresInSeconds: 7200 });
workflow = new GCPMachineWorkflow(cache);
});

it('sets the token on the connection', async function () {
const token = await workflow.getTokenFromCacheOrEnv(connection, credentials);
expect(token).to.equal('test');
expect(connection.accessToken).to.equal('test');
});
});
});
});
});