diff --git a/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts b/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts index 3ecb7c3f7d..1f4947d89a 100644 --- a/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +++ b/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts @@ -34,6 +34,9 @@ export class AutomatedCallbackWorkflow extends CallbackWorkflow { // If the server fails for any other reason, do not clear the cache. if (this.cache.hasAccessToken) { const token = this.cache.getAccessToken(); + if (!connection.accessToken) { + connection.accessToken = token; + } try { return await this.finishAuthentication(connection, credentials, token); } catch (error) { diff --git a/test/mongodb.ts b/test/mongodb.ts index d9ffc4c0a1..45e6a6679c 100644 --- a/test/mongodb.ts +++ b/test/mongodb.ts @@ -119,6 +119,7 @@ export * from '../src/cmap/auth/gssapi'; export * from '../src/cmap/auth/mongo_credentials'; export * from '../src/cmap/auth/mongodb_aws'; export * from '../src/cmap/auth/mongodb_oidc'; +export * from '../src/cmap/auth/mongodb_oidc/automated_callback_workflow'; export * from '../src/cmap/auth/mongodb_oidc/azure_machine_workflow'; export * from '../src/cmap/auth/mongodb_oidc/callback_workflow'; export * from '../src/cmap/auth/plain'; diff --git a/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts new file mode 100644 index 0000000000..33d37e593d --- /dev/null +++ b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts @@ -0,0 +1,44 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; + +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { callback } from '../../../../../src/cmap/auth/mongodb_oidc/gcp_machine_workflow'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { TokenCache } from '../../../../../src/cmap/auth/mongodb_oidc/token_cache'; +import { + AutomatedCallbackWorkflow, + CallbackWorkflow, + Connection, + MongoCredentials +} from '../../../../mongodb'; + +describe('AutomatedCallbackWorkflow', function () { + describe('#execute', function () { + context('when the cache has a token', function () { + const sandbox = sinon.createSandbox(); + + // See NODE-6801 and corresponding PR: https://github.com/mongodb/node-mongodb-native/pull/4438 + // This is a test to ensure that we do not regress on the above issue. Do NOT remove this test. + context('when the connection has no token', function () { + const cache = new TokenCache(); + const connection = sandbox.createStubInstance(Connection); + const credentials = sandbox.createStubInstance(MongoCredentials); + sandbox.stub(CallbackWorkflow.prototype, 'finishAuthentication').resolves(); + const workflow = new AutomatedCallbackWorkflow(cache, callback); + + beforeEach(function () { + cache.put({ accessToken: 'test', expiresInSeconds: 7200 }); + workflow.execute(connection, credentials); + }); + + afterEach(function () { + sandbox.restore(); + }); + + it('sets the token on the connection', async function () { + expect(connection.accessToken).to.equal('test'); + }); + }); + }); + }); +});