11import { setTimeout } from 'timers/promises' ;
22
33import { type Document } from '../../../bson' ;
4+ import { type MongoClient } from '../../../mongo_client' ;
45import { ns } from '../../../utils' ;
56import type { Connection } from '../../connection' ;
67import type { MongoCredentials } from '../mongo_credentials' ;
@@ -21,7 +22,10 @@ export interface AccessToken {
2122}
2223
2324/** @internal */
24- export type OIDCTokenFunction = ( credentials : MongoCredentials ) => Promise < AccessToken > ;
25+ export type OIDCTokenFunction = (
26+ credentials : MongoCredentials ,
27+ client : MongoClient
28+ ) => Promise < AccessToken > ;
2529
2630/**
2731 * Common behaviour for OIDC machine workflows.
@@ -31,11 +35,13 @@ export abstract class MachineWorkflow implements Workflow {
3135 cache : TokenCache ;
3236 callback : OIDCTokenFunction ;
3337 lastExecutionTime : number ;
38+ client : MongoClient ;
3439
3540 /**
3641 * Instantiate the machine workflow.
3742 */
38- constructor ( cache : TokenCache ) {
43+ constructor ( client : MongoClient , cache : TokenCache ) {
44+ this . client = client ;
3945 this . cache = cache ;
4046 this . callback = this . withLock ( this . getToken . bind ( this ) ) ;
4147 this . lastExecutionTime = Date . now ( ) - THROTTLE_MS ;
@@ -101,7 +107,7 @@ export abstract class MachineWorkflow implements Workflow {
101107 }
102108 return token ;
103109 } else {
104- const token = await this . callback ( credentials ) ;
110+ const token = await this . callback ( credentials , connection . client ) ;
105111 this . cache . put ( { accessToken : token . access_token , expiresInSeconds : token . expires_in } ) ;
106112 // Put the access token on the connection as well.
107113 connection . accessToken = token . access_token ;
@@ -129,7 +135,7 @@ export abstract class MachineWorkflow implements Workflow {
129135 await setTimeout ( THROTTLE_MS - difference ) ;
130136 }
131137 this . lastExecutionTime = Date . now ( ) ;
132- return await callback ( credentials ) ;
138+ return await callback ( credentials , this . client ) ;
133139 } ) ;
134140 return await lock ;
135141 } ;
@@ -138,5 +144,5 @@ export abstract class MachineWorkflow implements Workflow {
138144 /**
139145 * Get the token from the environment or endpoint.
140146 */
141- abstract getToken ( credentials : MongoCredentials ) : Promise < AccessToken > ;
147+ abstract getToken ( credentials : MongoCredentials , client : MongoClient ) : Promise < AccessToken > ;
142148}
0 commit comments