change_user.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // This file was modified by Oracle on September 21, 2021.
  2. // The changes involve saving additional authentication factor passwords
  3. // in the command scope and enabling multi-factor authentication in the
  4. // client-side when the server supports it.
  5. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  6. 'use strict';
  7. const Command = require('./command.js');
  8. const Packets = require('../packets/index.js');
  9. const ClientConstants = require('../constants/client');
  10. const ClientHandshake = require('./client_handshake.js');
  11. const CharsetToEncoding = require('../constants/charset_encodings.js');
  12. class ChangeUser extends Command {
  13. constructor(options, callback) {
  14. super();
  15. this.onResult = callback;
  16. this.user = options.user;
  17. this.password = options.password;
  18. // "password1" is an alias of "password"
  19. this.password1 = options.password;
  20. this.password2 = options.password2;
  21. this.password3 = options.password3;
  22. this.database = options.database;
  23. this.passwordSha1 = options.passwordSha1;
  24. this.charsetNumber = options.charsetNumber;
  25. this.currentConfig = options.currentConfig;
  26. this.authenticationFactor = 0;
  27. }
  28. start(packet, connection) {
  29. const newPacket = new Packets.ChangeUser({
  30. flags: connection.config.clientFlags,
  31. user: this.user,
  32. database: this.database,
  33. charsetNumber: this.charsetNumber,
  34. password: this.password,
  35. passwordSha1: this.passwordSha1,
  36. authPluginData1: connection._handshakePacket.authPluginData1,
  37. authPluginData2: connection._handshakePacket.authPluginData2
  38. });
  39. this.currentConfig.user = this.user;
  40. this.currentConfig.password = this.password;
  41. this.currentConfig.database = this.database;
  42. this.currentConfig.charsetNumber = this.charsetNumber;
  43. connection.clientEncoding = CharsetToEncoding[this.charsetNumber];
  44. // clear prepared statements cache as all statements become invalid after changeUser
  45. connection._statements.clear();
  46. connection.writePacket(newPacket.toPacket());
  47. // check if the server supports multi-factor authentication
  48. const multiFactorAuthentication = connection.serverCapabilityFlags & ClientConstants.MULTI_FACTOR_AUTHENTICATION;
  49. if (multiFactorAuthentication) {
  50. // if the server supports multi-factor authentication, we enable it in
  51. // the client
  52. this.authenticationFactor = 1;
  53. }
  54. return ChangeUser.prototype.handshakeResult;
  55. }
  56. }
  57. ChangeUser.prototype.handshakeResult =
  58. ClientHandshake.prototype.handshakeResult;
  59. ChangeUser.prototype.calculateNativePasswordAuthToken =
  60. ClientHandshake.prototype.calculateNativePasswordAuthToken;
  61. module.exports = ChangeUser;