complexPassword.js 2.6 KB

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