|
| 1 | +var _ = require('../helper'); |
| 2 | + |
| 3 | +/** |
| 4 | + * @readonly |
| 5 | + * @enum {number} |
| 6 | + */ |
| 7 | +var Direction = { |
| 8 | + Ascending: 1, |
| 9 | + Descending: -1, |
| 10 | + None: 0 |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * @readonly |
| 15 | + * @enum {number} |
| 16 | + */ |
| 17 | +var CharacterCodes = { |
| 18 | + LowerCaseA: 'a'.charCodeAt(0), |
| 19 | + LowerCaseZ: 'z'.charCodeAt(0), |
| 20 | + Zero: '0'.charCodeAt(0), |
| 21 | + Nine: '9'.charCodeAt(0), |
| 22 | + UpperCaseA: 'A'.charCodeAt(0), |
| 23 | + UpperCaseZ: 'Z'.charCodeAt(0), |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Determines if a character is alphanumeric |
| 28 | + * |
| 29 | + * @param {number} code character code |
| 30 | + * @return {boolean} |
| 31 | + */ |
| 32 | +function isAlphanumeric(code) { |
| 33 | + if (!_.isNumber(code) || _.isNaN(code)) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + if (code >= CharacterCodes.LowerCaseA && code <= CharacterCodes.LowerCaseZ) { |
| 38 | + return true; |
| 39 | + } else if (code >= CharacterCodes.UpperCaseA && code <= CharacterCodes.UpperCaseZ) { |
| 40 | + return true; |
| 41 | + } else if (code >= CharacterCodes.Zero && code <= CharacterCodes.Nine) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Returns true if password has more sequential characters the configured max allowed |
| 50 | + * |
| 51 | + * @param {{max: number}} options |
| 52 | + * @param {string} password |
| 53 | + * @return {boolean} |
| 54 | + */ |
| 55 | +function assert(options, password) { |
| 56 | + if (!password) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + var prevCode = password.charCodeAt(0); |
| 61 | + var seqLen = isAlphanumeric(prevCode) ? 1 : 0; |
| 62 | + var currentDirection = Direction.None; |
| 63 | + |
| 64 | + for (var i = 1; i < password.length; i++) { |
| 65 | + var currentCode = password.charCodeAt(i); |
| 66 | + if (!isAlphanumeric(currentCode) || !isAlphanumeric(prevCode)) { |
| 67 | + currentDirection = Direction.None; |
| 68 | + seqLen = 1; |
| 69 | + prevCode = currentCode; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + var diff = currentCode - prevCode; |
| 74 | + prevCode = currentCode; |
| 75 | + |
| 76 | + if (diff === Direction.Ascending || diff === Direction.Descending) { |
| 77 | + if (currentDirection === diff) { |
| 78 | + seqLen += 1; |
| 79 | + } else { |
| 80 | + // start a new potential sequence of length 2 (prev + current) |
| 81 | + currentDirection = diff; |
| 82 | + seqLen = 2; |
| 83 | + } |
| 84 | + } else { |
| 85 | + currentDirection = Direction.None; |
| 86 | + seqLen = 1; |
| 87 | + } |
| 88 | + |
| 89 | + if (seqLen > options.max) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @param {{max: number}} options |
| 99 | + * @param {boolean} verified |
| 100 | + * @return {boolean} |
| 101 | + */ |
| 102 | +function explain(options, verified) { |
| 103 | + var example = ''; |
| 104 | + for (var i = 0; i < options.max + 1; i++) { |
| 105 | + example += String.fromCharCode('a'.charCodeAt(0) + i); |
| 106 | + } |
| 107 | + var d = { |
| 108 | + message: 'No more than %d sequential alphanumeric characters (e.g., "%s" not allowed)', // updated |
| 109 | + code: 'sequentialChars', |
| 110 | + format: [options.max, example] |
| 111 | + }; |
| 112 | + if (verified !== undefined) { |
| 113 | + d.verified = verified; |
| 114 | + } |
| 115 | + return d; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {{max: number}} options |
| 120 | + * @return {boolean} |
| 121 | + */ |
| 122 | +function validate(options) { |
| 123 | + if (!_.isObject(options)) { |
| 124 | + throw new Error('options should be an object'); |
| 125 | + } |
| 126 | + |
| 127 | + if (!_.isNumber(options.max) || _.isNaN(options.max) || options.max < 2) { |
| 128 | + throw new Error('max should be a number greater than or equal to 2'); |
| 129 | + } |
| 130 | + |
| 131 | + if (!_.isNumber(options.max) || _.isNaN(options.max) || options.max > 26) { |
| 132 | + throw new Error('max should be a number less than or equal to 26'); |
| 133 | + } |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +module.exports = { |
| 139 | + validate, |
| 140 | + explain, |
| 141 | + missing: function (options, password) { |
| 142 | + return explain(options, assert(options, password)); |
| 143 | + }, |
| 144 | + assert |
| 145 | +}; |
0 commit comments