complexPassword.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import _ from 'lodash'
  2. /* global PassField */
  3. /* eslint-disable
  4. max-len
  5. */
  6. import App from '../base'
  7. import '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 ${opts
  70. .length.max - 1} exceeded`
  71. }
  72. if (opts.length.min != null && modelValue.length < opts.length.min) {
  73. isValid = false
  74. scope.complexPasswordErrorMessage = `Password too short, minimum ${opts.length.min}`
  75. }
  76. ngModelCtrl.$setValidity('complexPassword', isValid)
  77. return modelValue
  78. })
  79. }
  80. }))