gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
 
/**
 * Context used during authentication
 *
 * @property {Connection} connection The connection to authenticate
 * @property {MongoCredentials} credentials The credentials to use for authentication
 * @property {object} options The options passed to the `connect` method
 * @property {object?} response The response of the initial handshake
 * @property {Buffer?} nonce A random nonce generated for use in an authentication conversation
 */
class AuthContext {
  constructor(connection, credentials, options) {
    this.connection = connection;
    this.credentials = credentials;
    this.options = options;
  }
}
 
class AuthProvider {
  constructor(bson) {
    this.bson = bson;
  }
 
  /**
   * Prepare the handshake document before the initial handshake.
   *
   * @param {object} handshakeDoc The document used for the initial handshake on a connection
   * @param {AuthContext} authContext Context for authentication flow
   * @param {function} callback
   */
  prepare(handshakeDoc, context, callback) {
    callback(undefined, handshakeDoc);
  }
 
  /**
   * Authenticate
   *
   * @param {AuthContext} context A shared context for authentication flow
   * @param {authResultCallback} callback The callback to return the result from the authentication
   */
  auth(context, callback) {
    callback(new TypeError('`auth` method must be overridden by subclass'));
  }
}
 
/**
 * This is a result from an authentication provider
 *
 * @callback authResultCallback
 * @param {error} error An error object. Set to null if no error present
 * @param {boolean} result The result of the authentication process
 */
 
module.exports = { AuthContext, AuthProvider };