diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/error.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/error.js index e4f0108f24076a27c0a70bb4b3b5cfed9084c70e..3095e5f72573bddb56a33f3a6c0897c07061f201 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/error.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/error.js @@ -11,6 +11,10 @@ const ERROR = { PASSWORD_ERROR_EXCEED_LIMIT: 'uni-id-password-error-exceed-limit', INVALID_USERNAME: 'uni-id-invalid-username', INVALID_PASSWORD: 'uni-id-invalid-password', + INVALID_PASSWORD_SUPER: 'uni-id-invalid-password-super', + INVALID_PASSWORD_STRONG: 'uni-id-invalid-password-strong', + INVALID_PASSWORD_MEDIUM: 'uni-id-invalid-password-medium', + INVALID_PASSWORD_WEAK: 'uni-id-invalid-password-weak', INVALID_MOBILE: 'uni-id-invalid-mobile', INVALID_EMAIL: 'uni-id-invalid-email', INVALID_NICKNAME: 'uni-id-invalid-nickname', diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js index 03ce938fd6a6fc930adb16bb01d57c5d5063f73b..7e0e33a304fa56c18923a365f26a6a305bc1b790 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js @@ -29,21 +29,6 @@ baseValidator.username = function (username) { } } -baseValidator.password = function (password) { - const errCode = ERROR.INVALID_PASSWORD - if (!isValidString(password)) { - return { - errCode - } - } - if (password.length < 6) { - // 密码长度不能小于6 - return { - errCode - } - } -} - baseValidator.mobile = function (mobile) { const errCode = ERROR.INVALID_MOBILE if (!isValidString(mobile)) { @@ -245,10 +230,62 @@ function isMatchArrayType (arr, rule) { return true } +// 特殊符号 https://www.ibm.com/support/pages/password-strength-rules ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/ +// const specialChar = '~!@#$%^&*_-+=`|\(){}[]:;"\'<>,.?/' +// const specialCharRegExp = /^[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]$/ +// for (let i = 0, arr = specialChar.split(''); i < arr.length; i++) { +// const char = arr[i] +// if (!specialCharRegExp.test(char)) { +// throw new Error('check special character error: ' + char) +// } +// } + +// 密码强度表达式 +const passwordRules = { + // 密码必须包含大小写字母、数字和特殊符号 + super: /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/, + // 密码必须包含字母、数字和特殊符号 + strong: /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/, + // 密码必须为字母、数字和特殊符号任意两种的组合 + medium: /^(?![0-9]+$)(?![a-zA-Z]+$)(?![~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/, + // 密码必须包含字母和数字 + weak: /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/ +} + +function createPasswordVerifier ({ + passwordStrength = 'medium' +} = {}) { + return function (password) { + const passwordRegExp = passwordRules[passwordStrength] + if (!passwordRegExp) { + throw new Error('Invalid password strength config: ' + passwordStrength) + } + const errCode = ERROR.INVALID_PASSWORD + if (!isValidString(password)) { + return { + errCode + } + } + if (!passwordRegExp.test(password)) { + return { + errCode: errCode + '-' + passwordStrength + } + } + } +} + class Validator { - constructor () { + constructor ({ + passwordStrength = 'medium' + } = {}) { this.baseValidator = baseValidator this.customValidator = Object.create(null) + this.mixin( + 'password', + createPasswordVerifier({ + passwordStrength + }) + ) } mixin (type, handler) { diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js index 4045ae6942c1bd7c2d7e13f970872f5906021dc8..092edccfedf315c67950db7a422180ec706e0cb4 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js @@ -39,14 +39,14 @@ const testCaseList = [{ } }, { value: { - password: '123456' + password: '123456abc' }, schema: { password: 'password' } }, { value: { - password: '123456' + password: '123456def' }, schema: { password: 'password' diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js index f187b10e6abd3bb284753c99c25434c1e8d5f93e..c5d5acbe43d5ae2b1193e4ccc85cff2ec7b87274 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js @@ -109,7 +109,9 @@ module.exports = { }) this.config = this.configUtils.getPlatformConfig() - this.validator = new Validator() + this.validator = new Validator({ + passwordStrength: this.config.passwordStrength || 'medium' + }) /** * 示例:覆盖密码验证规则 */ diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/en.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/en.js index 4276401550e4a18caabbc73f75e0d3678e0b80fb..1ef455ab1e419227ffbdfd1bdf52512659be57f4 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/en.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/en.js @@ -16,6 +16,10 @@ const sentence = { 'uni-id-password-error-exceed-limit': 'The number of password errors is excessive', 'uni-id-invalid-username': 'Invalid username', 'uni-id-invalid-password': 'invalid password', + 'uni-id-invalid-password-super': 'Passwords must have 8-16 characters and contain uppercase letters, lowercase letters, numbers, and symbols.', + 'uni-id-invalid-password-strong': 'Passwords must have 8-16 characters and contain letters, numbers and symbols.', + 'uni-id-invalid-password-medium': 'Passwords must have 8-16 characters and contain at least two of the following: letters, numbers, and symbols.', + 'uni-id-invalid-password-weak': 'Passwords must have 6-16 characters and contain letters and numbers.', 'uni-id-invalid-mobile': 'Invalid mobile phone number', 'uni-id-invalid-email': 'Invalid email address', 'uni-id-invalid-nickname': 'Invalid nickname', diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/zh-hans.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/zh-hans.js index 8634d493a353bc6c4baffabebe911e7d091d5af1..5bd2443f045ab895c4a33d98ab4f22e6c697c8b4 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/zh-hans.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lang/zh-hans.js @@ -16,6 +16,10 @@ const sentence = { 'uni-id-password-error-exceed-limit': '密码错误次数过多,请稍后再试', 'uni-id-invalid-username': '用户名不合法', 'uni-id-invalid-password': '密码不合法', + 'uni-id-invalid-password-super': '密码必须包含大小写字母、数字和特殊符号,长度8-16位', + 'uni-id-invalid-password-strong': '密码必须包含字母、数字和特殊符号,长度8-16位不合法', + 'uni-id-invalid-password-medium': '密码必须为字母、数字和特殊符号任意两种的组合,长度8-16位', + 'uni-id-invalid-password-weak': '密码必须包含字母和数字,长度6-16位', 'uni-id-invalid-mobile': '手机号码不合法', 'uni-id-invalid-email': '邮箱不合法', 'uni-id-invalid-nickname': '昵称不合法',