passwordHandlers.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 datasource 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 = (ctrl: Ctrl, field: PasswordFieldEnum) => (
  26. event: SyntheticEvent<HTMLInputElement>
  27. ) => {
  28. event.preventDefault();
  29. // Reset also normal plain text password to remove it and only save it in secureJsonData.
  30. ctrl.current[field] = null;
  31. ctrl.current.secureJsonFields[field] = false;
  32. ctrl.current.secureJsonData = ctrl.current.secureJsonData || {};
  33. ctrl.current.secureJsonData[field] = '';
  34. };
  35. export const createChangeHandler = (ctrl: any, field: PasswordFieldEnum) => (
  36. event: SyntheticEvent<HTMLInputElement>
  37. ) => {
  38. ctrl.current.secureJsonData = ctrl.current.secureJsonData || {};
  39. ctrl.current.secureJsonData[field] = event.currentTarget.value;
  40. };