Skip to content

Commit

Permalink
refactor(NODE-5136): make authentication use async / await (#3607)
Browse files Browse the repository at this point in the history
Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
  • Loading branch information
baileympearson and nbbeeken authored Mar 24, 2023
1 parent 054872d commit 6537e27
Show file tree
Hide file tree
Showing 18 changed files with 1,086 additions and 1,345 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"global"
],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/require-await": "off",
"no-restricted-imports": [
"error",
{
Expand Down Expand Up @@ -120,6 +121,12 @@
"selector": "BinaryExpression[operator=/[=!]==?/] Literal[value='undefined']",
"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)"
}
],
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
]
},
"overrides": [
Expand Down Expand Up @@ -228,6 +235,7 @@
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/require-await": "off",
"no-return-await": "off",
"@typescript-eslint/return-await": [
"error",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
"check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js",
"check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
"check:aws": "nyc mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
"check:oidc": "mocha --config test/manual/mocharc.json test/manual/mongodb_oidc.prose.test.ts",
"check:ocsp": "mocha --config test/manual/mocharc.json test/manual/ocsp_support.test.js",
"check:kerberos": "mocha --config test/manual/mocharc.json test/manual/kerberos.test.js",
"check:kerberos": "nyc mocha --config test/manual/mocharc.json test/manual/kerberos.test.ts",
"check:tls": "mocha --config test/manual/mocharc.json test/manual/tls_support.test.js",
"check:ldap": "mocha --config test/manual/mocharc.json test/manual/ldap.test.js",
"check:ldap": "nyc mocha --config test/manual/mocharc.json test/manual/ldap.test.js",
"check:socks5": "mocha --config test/manual/mocharc.json test/manual/socks5.test.ts",
"check:csfle": "mocha --config test/mocha_mongodb.json test/integration/client-side-encryption",
"check:snappy": "mocha test/unit/assorted/snappy.test.js",
Expand Down
35 changes: 14 additions & 21 deletions src/cmap/auth/auth_provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Document } from '../../bson';
import { MongoRuntimeError } from '../../error';
import type { Callback, ClientMetadataOptions } from '../../utils';
import type { ClientMetadataOptions } from '../../utils';
import type { HandshakeDocument } from '../connect';
import type { Connection, ConnectionOptions } from '../connection';
import type { MongoCredentials } from './mongo_credentials';
Expand Down Expand Up @@ -38,47 +38,40 @@ export class AuthContext {
}
}

export class AuthProvider {
export abstract class AuthProvider {
/**
* Prepare the handshake document before the initial handshake.
*
* @param handshakeDoc - The document used for the initial handshake on a connection
* @param authContext - Context for authentication flow
*/
prepare(
async prepare(
handshakeDoc: HandshakeDocument,
authContext: AuthContext,
callback: Callback<HandshakeDocument>
): void {
callback(undefined, handshakeDoc);
_authContext: AuthContext
): Promise<HandshakeDocument> {
return handshakeDoc;
}

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

/**
* Reauthenticate.
* @param context - The shared auth context.
* @param callback - The callback.
*/
reauth(context: AuthContext, callback: Callback): void {
// If we are already reauthenticating this is a no-op.
async reauth(context: AuthContext): Promise<void> {
if (context.reauthenticating) {
return callback(new MongoRuntimeError('Reauthentication already in progress.'));
throw new MongoRuntimeError('Reauthentication already in progress.');
}
context.reauthenticating = true;
const cb: Callback = (error, result) => {
try {
context.reauthenticating = true;
await this.auth(context);
} finally {
context.reauthenticating = false;
callback(error, result);
};
this.auth(context, cb);
}
}
}
Loading

0 comments on commit 6537e27

Please sign in to comment.