passwordHandlers.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Set of handlers for secure password field in Angular components. They handle backward compatibility with
  3. * passwords stored in plain text fields.
  4. */
  5. import { SyntheticEvent } from 'react';
  6. export enum PasswordFieldEnum {
  7. Password = 'password',
  8. BasicAuthPassword = 'basicAuthPassword',
  9. }
  10. /**
  11. * Basic shape for settings controllers in at the moment mostly angular data source plugins.
  12. */
  13. export type Ctrl = {
  14. current: {
  15. secureJsonFields: {
  16. [key: string]: boolean;
  17. };
  18. secureJsonData?: {
  19. [key: string]: string;
  20. };
  21. password?: string;
  22. basicAuthPassword?: string;
  23. };
  24. };
  25. export const createResetHandler =
  26. (ctrl: Ctrl, field: PasswordFieldEnum) => (event: SyntheticEvent<HTMLInputElement>) => {
  27. event.preventDefault();
  28. // Reset also normal plain text password to remove it and only save it in secureJsonData.
  29. ctrl.current[field] = undefined;
  30. ctrl.current.secureJsonFields[field] = false;
  31. ctrl.current.secureJsonData = ctrl.current.secureJsonData || {};
  32. ctrl.current.secureJsonData[field] = '';
  33. };
  34. export const createChangeHandler =
  35. (ctrl: any, field: PasswordFieldEnum) => (event: SyntheticEvent<HTMLInputElement>) => {
  36. ctrl.current.secureJsonData = ctrl.current.secureJsonData || {};
  37. ctrl.current.secureJsonData[field] = event.currentTarget.value;
  38. };