auth_switch.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const Packets = require('../packets/index.js');
  3. const caching_sha2_password = require('../auth_plugins/caching_sha2_password.js');
  4. const mysql_native_password = require('../auth_plugins/mysql_native_password.js');
  5. const standardAuthPlugins = {
  6. caching_sha2_password: caching_sha2_password({}),
  7. mysql_native_password: mysql_native_password({})
  8. };
  9. function warnLegacyAuthSwitch() {
  10. console.warn(
  11. 'WARNING! authSwitchHandler api is deprecated, please use new authPlugins api'
  12. );
  13. }
  14. function authSwitchRequest(packet, connection, command) {
  15. const { pluginName, pluginData } = Packets.AuthSwitchRequest.fromPacket(
  16. packet
  17. );
  18. let authPlugin =
  19. connection.config.authPlugins && connection.config.authPlugins[pluginName];
  20. // legacy plugin api don't allow to override mysql_native_password
  21. // if pluginName is mysql_native_password it's using standard auth4.1 auth
  22. if (
  23. connection.config.authSwitchHandler &&
  24. pluginName !== 'mysql_native_password'
  25. ) {
  26. const legacySwitchHandler = connection.config.authSwitchHandler;
  27. warnLegacyAuthSwitch();
  28. legacySwitchHandler({ pluginName, pluginData }, (err, data) => {
  29. if (err) {
  30. connection.emit('error', err);
  31. return;
  32. }
  33. connection.writePacket(new Packets.AuthSwitchResponse(data).toPacket());
  34. });
  35. return;
  36. }
  37. if (!authPlugin) {
  38. authPlugin = standardAuthPlugins[pluginName];
  39. }
  40. if (!authPlugin) {
  41. throw new Error(
  42. `Server requests authentication using unknown plugin ${pluginName}. See ${'TODO: add plugins doco here'} on how to configure or author authentication plugins.`
  43. );
  44. }
  45. connection._authPlugin = authPlugin({ connection, command });
  46. Promise.resolve(connection._authPlugin(pluginData)).then(data => {
  47. if (data) {
  48. connection.writePacket(new Packets.AuthSwitchResponse(data).toPacket());
  49. }
  50. });
  51. }
  52. function authSwitchRequestMoreData(packet, connection) {
  53. const { data } = Packets.AuthSwitchRequestMoreData.fromPacket(packet);
  54. if (connection.config.authSwitchHandler) {
  55. const legacySwitchHandler = connection.config.authSwitchHandler;
  56. warnLegacyAuthSwitch();
  57. legacySwitchHandler({ pluginData: data }, (err, data) => {
  58. if (err) {
  59. connection.emit('error', err);
  60. return;
  61. }
  62. connection.writePacket(new Packets.AuthSwitchResponse(data).toPacket());
  63. });
  64. return;
  65. }
  66. if (!connection._authPlugin) {
  67. throw new Error(
  68. 'AuthPluginMoreData received but no auth plugin instance found'
  69. );
  70. }
  71. Promise.resolve(connection._authPlugin(data)).then(data => {
  72. if (data) {
  73. connection.writePacket(new Packets.AuthSwitchResponse(data).toPacket());
  74. }
  75. });
  76. }
  77. module.exports = {
  78. authSwitchRequest,
  79. authSwitchRequestMoreData
  80. };