Skip to content

Commit cb13746

Browse files
authored
fix(NODE-6801): set token on connection from cache (#4438)
1 parent 44bc5a8 commit cb13746

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/cmap/auth/mongodb_oidc/machine_workflow.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ export abstract class MachineWorkflow implements Workflow {
9494
credentials: MongoCredentials
9595
): Promise<string> {
9696
if (this.cache.hasAccessToken) {
97-
return this.cache.getAccessToken();
97+
const token = this.cache.getAccessToken();
98+
// New connections won't have an access token so ensure we set here.
99+
if (!connection.accessToken) {
100+
connection.accessToken = token;
101+
}
102+
return token;
98103
} else {
99104
const token = await this.callback(credentials);
100105
this.cache.put({ accessToken: token.access_token, expiresInSeconds: token.expires_in });

test/unit/cmap/auth/mongodb_oidc/gcp_machine_workflow.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,28 @@ describe('GCPMachineFlow', function () {
1919
});
2020
});
2121
});
22+
23+
describe('#getTokenFromCacheOrEnv', function () {
24+
context('when the cache has a token', function () {
25+
const connection = sinon.createStubInstance(Connection);
26+
const credentials = sinon.createStubInstance(MongoCredentials);
27+
28+
context('when the connection has no token', function () {
29+
let cache;
30+
let workflow;
31+
32+
this.beforeEach(function () {
33+
cache = new TokenCache();
34+
cache.put({ accessToken: 'test', expiresInSeconds: 7200 });
35+
workflow = new GCPMachineWorkflow(cache);
36+
});
37+
38+
it('sets the token on the connection', async function () {
39+
const token = await workflow.getTokenFromCacheOrEnv(connection, credentials);
40+
expect(token).to.equal('test');
41+
expect(connection.accessToken).to.equal('test');
42+
});
43+
});
44+
});
45+
});
2246
});

0 commit comments

Comments
 (0)