complexPassword.js 2.7 KB

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