Skip to content

Commit e386c87

Browse files
committed
feat: clear oidc cache when pool clears
1 parent 7ef6edd commit e386c87

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/cmap/auth/mongodb_oidc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type EnvironmentName = 'test' | 'azure' | 'gcp' | 'k8s' | undefined;
9393

9494
/** @internal */
9595
export interface Workflow {
96+
cache: TokenCache;
97+
9698
/**
9799
* All device workflows must implement this method in order to get the access
98100
* token and then call authenticate with it.

src/cmap/connection_pool.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
WaitQueueTimeoutError
6262
} from './errors';
6363
import { ConnectionPoolMetrics } from './metrics';
64+
import { MongoDBOIDC } from './auth/mongodb_oidc';
6465

6566
/** @public */
6667
export interface ConnectionPoolOptions extends Omit<ConnectionOptions, 'id' | 'generation'> {
@@ -428,6 +429,19 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
428429
return;
429430
}
430431

432+
// If we are clearing the connnection pool when using OIDC, we need to remove the access token
433+
// from the cache so we dont' try to use the same token again for initial auth on a new connection
434+
// when the token may have expired.
435+
const clientState = this.server.topology.client.s;
436+
const credentials = clientState.options.credentials;
437+
if (credentials?.mechanism === 'MONGODB-OIDC') {
438+
const provider = this.server.topology.client.s.authProviders.getOrCreateProvider(
439+
credentials.mechanism,
440+
credentials.mechanismProperties
441+
) as MongoDBOIDC;
442+
provider.workflow.cache.removeAccessToken();
443+
}
444+
431445
// handle load balanced case
432446
if (this.loadBalanced) {
433447
const { serviceId } = options;

0 commit comments

Comments
 (0)