2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2020-05-12 10:23:53 -04:00
|
|
|
/* global PassField */
|
|
|
|
|
2019-01-11 09:15:21 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../base'
|
2021-09-14 04:55:09 -04:00
|
|
|
import '../vendor/libs/passfield'
|
2020-05-19 05:02:56 -04:00
|
|
|
App.directive('complexPassword', () => ({
|
|
|
|
require: ['^asyncForm', 'ngModel'],
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
link(scope, element, attrs, ctrl) {
|
|
|
|
PassField.Config.blackList = []
|
|
|
|
const defaultPasswordOpts = {
|
|
|
|
pattern: '',
|
|
|
|
length: {
|
|
|
|
min: 6,
|
2021-04-27 03:52:58 -04:00
|
|
|
max: 72,
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
allowEmpty: false,
|
|
|
|
allowAnyChars: false,
|
|
|
|
isMasked: true,
|
|
|
|
showToggle: false,
|
|
|
|
showGenerate: false,
|
|
|
|
showTip: false,
|
|
|
|
showWarn: false,
|
|
|
|
checkMode: PassField.CheckModes.STRICT,
|
|
|
|
chars: {
|
|
|
|
digits: '1234567890',
|
|
|
|
letters: 'abcdefghijklmnopqrstuvwxyz',
|
|
|
|
letters_up: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
2021-04-27 03:52:58 -04:00
|
|
|
symbols: '@#$%^&*()-_=+[]{};:<>/?!£€.,',
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const opts = _.defaults(
|
|
|
|
window.passwordStrengthOptions || {},
|
|
|
|
defaultPasswordOpts
|
|
|
|
)
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (opts.length.min === 1) {
|
|
|
|
// this allows basically anything to be a valid password
|
|
|
|
opts.acceptRate = 0
|
|
|
|
}
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (opts.length.max > 72) {
|
|
|
|
// there is a hard limit of 71 characters in the password at the backend
|
|
|
|
opts.length.max = 72
|
|
|
|
}
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (opts.length.max > 0) {
|
|
|
|
// PassField's notion of 'max' is non-inclusive
|
|
|
|
opts.length.max += 1
|
|
|
|
}
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const passField = new PassField.Field('passwordField', opts)
|
|
|
|
const [asyncFormCtrl, ngModelCtrl] = Array.from(ctrl)
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
ngModelCtrl.$parsers.unshift(function (modelValue) {
|
2020-05-19 05:02:56 -04:00
|
|
|
let isValid = passField.validatePass()
|
|
|
|
const email = asyncFormCtrl.getEmail() || window.usersEmail
|
2019-01-11 09:15:21 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (!isValid) {
|
|
|
|
scope.complexPasswordErrorMessage = passField.getPassValidationMessage()
|
|
|
|
} else if (typeof email === 'string' && email !== '') {
|
|
|
|
const startOfEmail = email.split('@')[0]
|
|
|
|
if (
|
|
|
|
modelValue.indexOf(email) !== -1 ||
|
|
|
|
modelValue.indexOf(startOfEmail) !== -1
|
|
|
|
) {
|
2019-01-11 09:15:21 -05:00
|
|
|
isValid = false
|
2020-05-19 05:02:56 -04:00
|
|
|
scope.complexPasswordErrorMessage =
|
|
|
|
'Password can not contain email address'
|
2019-01-11 09:15:21 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
if (opts.length.max != null && modelValue.length >= opts.length.max) {
|
|
|
|
isValid = false
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.complexPasswordErrorMessage = `Maximum password length ${
|
|
|
|
opts.length.max - 1
|
|
|
|
} exceeded`
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
if (opts.length.min != null && modelValue.length < opts.length.min) {
|
|
|
|
isValid = false
|
2020-12-15 05:23:54 -05:00
|
|
|
scope.complexPasswordErrorMessage = `Password too short, minimum ${opts.length.min}`
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
ngModelCtrl.$setValidity('complexPassword', isValid)
|
|
|
|
return modelValue
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}))
|