/src/client/src/core/util/helper/password.ts

https://github.com/orangehrm/OrangeHRM · TypeScript · 48 lines · 29 code · 2 blank · 17 comment · 0 complexity · 077ef124c3794539fff925f20fc3b958 MD5 · raw file

  1. /**
  2. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  3. * all the essential functionalities required for any enterprise.
  4. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  5. *
  6. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  7. * the GNU General Public License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  11. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with this program;
  15. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA
  17. */
  18. import {translate as translatorFactory} from '@/core/plugins/i18n/translate';
  19. const translate = translatorFactory();
  20. export function getPassLevel(password: string): number[] {
  21. const level1 = new RegExp(/[a-z]/);
  22. const level2 = new RegExp(/[A-Z]/);
  23. const level3 = new RegExp(/[0-9]/);
  24. const level4 = new RegExp(/[@#\\/\-!$%^&*()_+|~=`{}[\]:";'<>?,.]/);
  25. return [level1, level2, level3, level4].map(level => {
  26. return level.test(password) ? 1 : 0;
  27. });
  28. }
  29. export function checkPassword(password: string): string | boolean {
  30. if (password.length >= 8) {
  31. const pwdLevel = getPassLevel(password);
  32. if (RegExp(/\s/).test(password)) {
  33. return translate('auth.your_password_should_not_contain_spaces');
  34. }
  35. if (pwdLevel.reduce((acc, curr) => acc + curr, 0) < 4) {
  36. return translate(
  37. 'auth.must_contain_lower_case_upper_case_digit_character_message',
  38. );
  39. } else {
  40. return true;
  41. }
  42. } else {
  43. return translate('auth.should_have_min_8_characters');
  44. }
  45. }