Skip to content

chore: bring back token missing fix #4548

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 2 commits into from
Jun 2, 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
3 changes: 3 additions & 0 deletions src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});
});