Skip to content

Commit 352b7ea

Browse files
authored
chore: bring back token missing fix (#4548)
1 parent 5186fb1 commit 352b7ea

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class AutomatedCallbackWorkflow extends CallbackWorkflow {
3434
// If the server fails for any other reason, do not clear the cache.
3535
if (this.cache.hasAccessToken) {
3636
const token = this.cache.getAccessToken();
37+
if (!connection.accessToken) {
38+
connection.accessToken = token;
39+
}
3740
try {
3841
return await this.finishAuthentication(connection, credentials, token);
3942
} catch (error) {

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export * from '../src/cmap/auth/gssapi';
119119
export * from '../src/cmap/auth/mongo_credentials';
120120
export * from '../src/cmap/auth/mongodb_aws';
121121
export * from '../src/cmap/auth/mongodb_oidc';
122+
export * from '../src/cmap/auth/mongodb_oidc/automated_callback_workflow';
122123
export * from '../src/cmap/auth/mongodb_oidc/azure_machine_workflow';
123124
export * from '../src/cmap/auth/mongodb_oidc/callback_workflow';
124125
export * from '../src/cmap/auth/plain';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
3+
4+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
5+
import { callback } from '../../../../../src/cmap/auth/mongodb_oidc/gcp_machine_workflow';
6+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
7+
import { TokenCache } from '../../../../../src/cmap/auth/mongodb_oidc/token_cache';
8+
import {
9+
AutomatedCallbackWorkflow,
10+
CallbackWorkflow,
11+
Connection,
12+
MongoCredentials
13+
} from '../../../../mongodb';
14+
15+
describe('AutomatedCallbackWorkflow', function () {
16+
describe('#execute', function () {
17+
context('when the cache has a token', function () {
18+
const sandbox = sinon.createSandbox();
19+
20+
// See NODE-6801 and corresponding PR: https://github.com/mongodb/node-mongodb-native/pull/4438
21+
// This is a test to ensure that we do not regress on the above issue. Do NOT remove this test.
22+
context('when the connection has no token', function () {
23+
const cache = new TokenCache();
24+
const connection = sandbox.createStubInstance(Connection);
25+
const credentials = sandbox.createStubInstance(MongoCredentials);
26+
sandbox.stub(CallbackWorkflow.prototype, 'finishAuthentication').resolves();
27+
const workflow = new AutomatedCallbackWorkflow(cache, callback);
28+
29+
beforeEach(function () {
30+
cache.put({ accessToken: 'test', expiresInSeconds: 7200 });
31+
workflow.execute(connection, credentials);
32+
});
33+
34+
afterEach(function () {
35+
sandbox.restore();
36+
});
37+
38+
it('sets the token on the connection', async function () {
39+
expect(connection.accessToken).to.equal('test');
40+
});
41+
});
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)