complexPassword.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* global PassField */
  2. /* eslint-disable
  3. max-len
  4. */
  5. import App from '../base'
  6. import 'libs/passfield'
  7. App.directive('complexPassword', () => ({
  8. require: ['^asyncForm', 'ngModel'],
  9. link(scope, element, attrs, ctrl) {
  10. PassField.Config.blackList = []
  11. const defaultPasswordOpts = {
  12. pattern: '',
  13. length: {
  14. min: 6,
  15. max: 72
  16. },
  17. allowEmpty: false,
  18. allowAnyChars: false,
  19. isMasked: true,
  20. showToggle: false,
  21. showGenerate: false,
  22. showTip: false,
  23. showWarn: false,
  24. checkMode: PassField.CheckModes.STRICT,
  25. chars: {
  26. digits: '1234567890',
  27. letters: 'abcdefghijklmnopqrstuvwxyz',
  28. letters_up: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  29. symbols: '@#$%^&*()-_=+[]{};:<>/?!£€.,'
  30. }
  31. }
  32. const opts = _.defaults(
  33. window.passwordStrengthOptions || {},
  34. defaultPasswordOpts
  35. )
  36. if (opts.length.min === 1) {
  37. // this allows basically anything to be a valid password
  38. opts.acceptRate = 0
  39. }
  40. if (opts.length.max > 72) {
  41. // there is a hard limit of 71 characters in the password at the backend
  42. opts.length.max = 72
  43. }
  44. if (opts.length.max > 0) {
  45. // PassField's notion of 'max' is non-inclusive
  46. opts.length.max += 1
  47. }
  48. const passField = new PassField.Field('passwordField', opts)
  49. const [asyncFormCtrl, ngModelCtrl] = Array.from(ctrl)
  50. ngModelCtrl.$parsers.unshift(function(modelValue) {
  51. let isValid = passField.validatePass()
  52. const email = asyncFormCtrl.getEmail() || window.usersEmail
  53. if (!isValid) {
  54. scope.complexPasswordErrorMessage = passField.getPassValidationMessage()
  55. } else if (typeof email === 'string' && email !== '') {
  56. const startOfEmail = email.split('@')[0]
  57. if (
  58. modelValue.indexOf(email) !== -1 ||
  59. modelValue.indexOf(startOfEmail) !== -1
  60. ) {
  61. isValid = false
  62. scope.complexPasswordErrorMessage =
  63. 'Password can not contain email address'
  64. }
  65. }
  66. if (opts.length.max != null && modelValue.length >= opts.length.max) {
  67. isValid = false
  68. scope.complexPasswordErrorMessage = `Maximum password length ${opts
  69. .length.max - 1} exceeded`
  70. }
  71. if (opts.length.min != null && modelValue.length < opts.length.min) {
  72. isValid = false
  73. scope.complexPasswordErrorMessage = `Password too short, minimum ${
  74. opts.length.min
  75. }`
  76. }
  77. ngModelCtrl.$setValidity('complexPassword', isValid)
  78. return modelValue
  79. })
  80. }
  81. }))