chenyc
2025-05-29 92f69c57b920cf62ecc9f15f9ed196fa26dbf2ac
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
'use strict';
const AuthProvider = require('./auth_provider').AuthProvider;
 
class X509 extends AuthProvider {
  prepare(handshakeDoc, authContext, callback) {
    const credentials = authContext.credentials;
    Object.assign(handshakeDoc, {
      speculativeAuthenticate: x509AuthenticateCommand(credentials)
    });
 
    callback(undefined, handshakeDoc);
  }
 
  auth(authContext, callback) {
    const connection = authContext.connection;
    const credentials = authContext.credentials;
    const response = authContext.response;
    if (response.speculativeAuthenticate) {
      return callback();
    }
 
    connection.command('$external.$cmd', x509AuthenticateCommand(credentials), callback);
  }
}
 
function x509AuthenticateCommand(credentials) {
  const command = { authenticate: 1, mechanism: 'MONGODB-X509' };
  if (credentials.username) {
    Object.assign(command, { user: credentials.username });
  }
 
  return command;
}
 
module.exports = X509;