Skip to content

Commit

Permalink
fix(NODE-5035): change per suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 27, 2023
1 parent 1b1de47 commit 32b0490
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/cmap/auth/mongodb_oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ export class MongoDBOIDC extends AuthProvider {
if (error || !workflow) {
return callback(error);
}
workflow
.execute(connection, credentials)
.then(result => {
workflow.execute(connection, credentials).then(
result => {
return callback(undefined, result);
})
.catch(error => {
},
error => {
callback(error);
});
}
);
});
}

Expand All @@ -107,14 +107,14 @@ export class MongoDBOIDC extends AuthProvider {
if (error || !workflow) {
return callback(error);
}
workflow
.speculativeAuth()
.then(result => {
workflow.speculativeAuth().then(
result => {
return callback(undefined, { ...handshakeDoc, ...result });
})
.catch(error => {
},
error => {
callback(error);
});
}
);
});
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/cmap/auth/mongodb_oidc/callback_workflow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type Document, Binary, BSON } from 'bson';
import { promisify } from 'util';

import { MongoInvalidArgumentError } from '../../../error';
import { ns } from '../../../utils';
Expand All @@ -11,7 +10,7 @@ import { TokenEntryCache } from './token_entry_cache';
import type { Workflow } from './workflow';

/* 5 minutes in milliseconds */
const TIMEOUT = 300000;
const TIMEOUT_MS = 300000;

/**
* OIDC implementation of a callback based workflow.
Expand Down Expand Up @@ -72,7 +71,7 @@ export class CallbackWorkflow implements Workflow {
// If authentication errors when using a cached token we remove it from
// the cache.
this.cache.deleteEntry(connection.address, credentials.username || '');
return Promise.reject(error);
throw error;
}
} else {
// Remove the expired entry from the cache.
Expand All @@ -87,8 +86,7 @@ export class CallbackWorkflow implements Workflow {
}
} else {
// No entry means to start with the step one saslStart.
const executeCommand = promisify(connection.command.bind(connection));
const result = await executeCommand(
const result = await connection.commandAsync(
ns(credentials.source),
startCommandDocument(credentials),
undefined
Expand Down Expand Up @@ -117,7 +115,7 @@ export class CallbackWorkflow implements Workflow {
credentials.username,
stepOneResult,
tokenResult,
TIMEOUT
TIMEOUT_MS
);
// Cache a new entry and continue with the saslContinue.
this.cache.addEntry(connection.address, credentials.username || '', result, stepOneResult);
Expand All @@ -142,7 +140,7 @@ export class CallbackWorkflow implements Workflow {
// Always clear expired entries from the cache on each finish as cleanup.
this.cache.deleteExpiredEntries();
if (request) {
const tokenResult = await request(credentials.username, stepOneResult, TIMEOUT);
const tokenResult = await request(credentials.username, stepOneResult, TIMEOUT_MS);
// Cache a new entry and continue with the saslContinue.
this.cache.addEntry(
connection.address,
Expand Down Expand Up @@ -171,8 +169,7 @@ async function finishAuth(
credentials: MongoCredentials
): Promise<Document> {
// Execute the step two saslContinue.
const executeCommand = promisify(connection.command.bind(connection));
return executeCommand(
return connection.commandAsync(
ns(credentials.source),
continueCommandDocument(result.accessToken, conversationId),
undefined
Expand Down Expand Up @@ -206,6 +203,10 @@ function continueCommandDocument(token: string, conversationId?: number): Docume
payload: new Binary(BSON.serialize({ jwt: token }))
};
}
// saslContinue requires a conversationId in the command to be valid so in this
// case the server allows "step two" to actually be a saslStart with the token
// as the jwt since the use of the cached value has no correlating conversating
// on the particular connection.
return {
saslStart: 1,
mechanism: AuthMechanism.MONGODB_OIDC,
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/auth/mongodb_oidc/token_entry_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export class TokenEntryCache {
* Delete all expired entries from the cache.
*/
deleteExpiredEntries(): void {
this.entries.forEach((entry, key, entries) => {
for (const [key, entry] of this.entries) {
if (!entry.isValid()) {
entries.delete(key);
this.entries.delete(key);
}
});
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clearTimeout, setTimeout } from 'timers';
import { promisify } from 'util';

import type { BSONSerializeOptions, Document, ObjectId } from '../bson';
import {
Expand Down Expand Up @@ -159,6 +160,11 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
lastHelloMS?: number;
serverApi?: ServerApi;
helloOk?: boolean;
commandAsync: (
ns: MongoDBNamespace,
cmd: Document,
options: CommandOptions | undefined
) => Promise<Document>;

/**@internal */
[kDelayedTimeoutId]: NodeJS.Timeout | null;
Expand Down Expand Up @@ -198,6 +204,16 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {

constructor(stream: Stream, options: ConnectionOptions) {
super();

this.commandAsync = promisify(
(
ns: MongoDBNamespace,
cmd: Document,
options: CommandOptions | undefined,
callback: Callback
) => this.command(ns, cmd, options, callback as any)
);

this.id = options.id;
this.address = streamIdentifier(stream, options);
this.socketTimeoutMS = options.socketTimeoutMS ?? 0;
Expand Down

0 comments on commit 32b0490

Please sign in to comment.