|
1 | 1 | import type { Document } from '../../bson';
|
2 | 2 | import { MongoRuntimeError } from '../../error';
|
3 |
| -import type { Callback, ClientMetadataOptions } from '../../utils'; |
| 3 | +import type { ClientMetadataOptions } from '../../utils'; |
4 | 4 | import type { HandshakeDocument } from '../connect';
|
5 | 5 | import type { Connection, ConnectionOptions } from '../connection';
|
6 | 6 | import type { MongoCredentials } from './mongo_credentials';
|
@@ -38,47 +38,40 @@ export class AuthContext {
|
38 | 38 | }
|
39 | 39 | }
|
40 | 40 |
|
41 |
| -export class AuthProvider { |
| 41 | +export abstract class AuthProvider { |
42 | 42 | /**
|
43 | 43 | * Prepare the handshake document before the initial handshake.
|
44 | 44 | *
|
45 | 45 | * @param handshakeDoc - The document used for the initial handshake on a connection
|
46 | 46 | * @param authContext - Context for authentication flow
|
47 | 47 | */
|
48 |
| - prepare( |
| 48 | + async prepare( |
49 | 49 | handshakeDoc: HandshakeDocument,
|
50 |
| - authContext: AuthContext, |
51 |
| - callback: Callback<HandshakeDocument> |
52 |
| - ): void { |
53 |
| - callback(undefined, handshakeDoc); |
| 50 | + _authContext: AuthContext |
| 51 | + ): Promise<HandshakeDocument> { |
| 52 | + return handshakeDoc; |
54 | 53 | }
|
55 | 54 |
|
56 | 55 | /**
|
57 | 56 | * Authenticate
|
58 | 57 | *
|
59 | 58 | * @param context - A shared context for authentication flow
|
60 |
| - * @param callback - The callback to return the result from the authentication |
61 | 59 | */
|
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>; |
66 | 61 |
|
67 | 62 | /**
|
68 | 63 | * Reauthenticate.
|
69 | 64 | * @param context - The shared auth context.
|
70 |
| - * @param callback - The callback. |
71 | 65 | */
|
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> { |
74 | 67 | if (context.reauthenticating) {
|
75 |
| - return callback(new MongoRuntimeError('Reauthentication already in progress.')); |
| 68 | + throw new MongoRuntimeError('Reauthentication already in progress.'); |
76 | 69 | }
|
77 |
| - context.reauthenticating = true; |
78 |
| - const cb: Callback = (error, result) => { |
| 70 | + try { |
| 71 | + context.reauthenticating = true; |
| 72 | + await this.auth(context); |
| 73 | + } finally { |
79 | 74 | context.reauthenticating = false;
|
80 |
| - callback(error, result); |
81 |
| - }; |
82 |
| - this.auth(context, cb); |
| 75 | + } |
83 | 76 | }
|
84 | 77 | }
|
0 commit comments