Skip to content

Commit 6537e27

Browse files
refactor(NODE-5136): make authentication use async / await (#3607)
Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
1 parent 054872d commit 6537e27

18 files changed

+1086
-1345
lines changed

.eslintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"global"
8888
],
8989
"@typescript-eslint/no-explicit-any": "off",
90+
"@typescript-eslint/require-await": "off",
9091
"no-restricted-imports": [
9192
"error",
9293
{
@@ -120,6 +121,12 @@
120121
"selector": "BinaryExpression[operator=/[=!]==?/] Literal[value='undefined']",
121122
"message": "Do not strictly check typeof undefined (NOTE: currently this rule only detects the usage of 'undefined' string literal so this could be a misfire)"
122123
}
124+
],
125+
"@typescript-eslint/no-unused-vars": [
126+
"error",
127+
{
128+
"argsIgnorePattern": "^_"
129+
}
123130
]
124131
},
125132
"overrides": [
@@ -228,6 +235,7 @@
228235
"@typescript-eslint/no-unsafe-call": "off",
229236
"@typescript-eslint/restrict-plus-operands": "off",
230237
"@typescript-eslint/restrict-template-expressions": "off",
238+
"@typescript-eslint/require-await": "off",
231239
"no-return-await": "off",
232240
"@typescript-eslint/return-await": [
233241
"error",

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@
126126
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
127127
"check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js",
128128
"check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
129-
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
129+
"check:aws": "nyc mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
130130
"check:oidc": "mocha --config test/manual/mocharc.json test/manual/mongodb_oidc.prose.test.ts",
131131
"check:ocsp": "mocha --config test/manual/mocharc.json test/manual/ocsp_support.test.js",
132-
"check:kerberos": "mocha --config test/manual/mocharc.json test/manual/kerberos.test.js",
132+
"check:kerberos": "nyc mocha --config test/manual/mocharc.json test/manual/kerberos.test.ts",
133133
"check:tls": "mocha --config test/manual/mocharc.json test/manual/tls_support.test.js",
134-
"check:ldap": "mocha --config test/manual/mocharc.json test/manual/ldap.test.js",
134+
"check:ldap": "nyc mocha --config test/manual/mocharc.json test/manual/ldap.test.js",
135135
"check:socks5": "mocha --config test/manual/mocharc.json test/manual/socks5.test.ts",
136136
"check:csfle": "mocha --config test/mocha_mongodb.json test/integration/client-side-encryption",
137137
"check:snappy": "mocha test/unit/assorted/snappy.test.js",

src/cmap/auth/auth_provider.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Document } from '../../bson';
22
import { MongoRuntimeError } from '../../error';
3-
import type { Callback, ClientMetadataOptions } from '../../utils';
3+
import type { ClientMetadataOptions } from '../../utils';
44
import type { HandshakeDocument } from '../connect';
55
import type { Connection, ConnectionOptions } from '../connection';
66
import type { MongoCredentials } from './mongo_credentials';
@@ -38,47 +38,40 @@ export class AuthContext {
3838
}
3939
}
4040

41-
export class AuthProvider {
41+
export abstract class AuthProvider {
4242
/**
4343
* Prepare the handshake document before the initial handshake.
4444
*
4545
* @param handshakeDoc - The document used for the initial handshake on a connection
4646
* @param authContext - Context for authentication flow
4747
*/
48-
prepare(
48+
async prepare(
4949
handshakeDoc: HandshakeDocument,
50-
authContext: AuthContext,
51-
callback: Callback<HandshakeDocument>
52-
): void {
53-
callback(undefined, handshakeDoc);
50+
_authContext: AuthContext
51+
): Promise<HandshakeDocument> {
52+
return handshakeDoc;
5453
}
5554

5655
/**
5756
* Authenticate
5857
*
5958
* @param context - A shared context for authentication flow
60-
* @param callback - The callback to return the result from the authentication
6159
*/
62-
auth(context: AuthContext, callback: Callback): void {
63-
// TODO(NODE-3483): Replace this with MongoMethodOverrideError
64-
callback(new MongoRuntimeError('`auth` method must be overridden by subclass'));
65-
}
60+
abstract auth(context: AuthContext): Promise<void>;
6661

6762
/**
6863
* Reauthenticate.
6964
* @param context - The shared auth context.
70-
* @param callback - The callback.
7165
*/
72-
reauth(context: AuthContext, callback: Callback): void {
73-
// If we are already reauthenticating this is a no-op.
66+
async reauth(context: AuthContext): Promise<void> {
7467
if (context.reauthenticating) {
75-
return callback(new MongoRuntimeError('Reauthentication already in progress.'));
68+
throw new MongoRuntimeError('Reauthentication already in progress.');
7669
}
77-
context.reauthenticating = true;
78-
const cb: Callback = (error, result) => {
70+
try {
71+
context.reauthenticating = true;
72+
await this.auth(context);
73+
} finally {
7974
context.reauthenticating = false;
80-
callback(error, result);
81-
};
82-
this.auth(context, cb);
75+
}
8376
}
8477
}

0 commit comments

Comments
 (0)