All files / node-user-accounts-boilerplate-nahid/fields strongPassword.js

93.22% Statements 55/59
89.74% Branches 35/39
100% Functions 6/6
93.22% Lines 55/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155    3x   3x   3x   2x   1x   1x     3x                   12x   12x   12x   60x   610x   60x 60x 60x   550x   15x       535x   14x   535x   550x   8x       542x   4x   542x     60x       60x         12x     3x   20x         20x   2x     18x   2x     16x   2x     14x         14x   1x     13x   1x     12x   1x     11x               3x   14x             14x 14x   14x   56x     14x   14x   140x   140x     14x    
"use strict";
 
const password = require('./password');
 
module.exports = Object.assign({}, password);
 
module.exports.assign = async function (user, field, value, fieldMeta, loginUser, config)
{
  if (typeof value === 'string' && value !== '')
  {
    this.checkStrongPassword(value);
  }
  await password.assign.call(this, user, field, value, fieldMeta, loginUser, config);
};
 
const sequences = [
  'abcdefghijklmnopqrstuvwxyz',
  'pyfgcrlaoeuidhtnsqjkxbmwvz',
  '0123456789',
  'qwertyuiop[]asdfghjkl;\'zxcvbnm,./',
  '!@#$%^&*()_+'
];
 
function findMaxSequenceSize(password)
{
  var maxSequence = 0;
 
  password = password.toLowerCase();
 
  sequences.forEach(function (sequence)
  {
    var converted = Array.prototype.map.call(password, function (c)
    {
      return sequence.indexOf(c);
    });
    var progressiveSize = 1;
    var sameSize = 1;
    for (var x = 1; x < converted.length; x++)
    {
      if (converted[x] === converted[x - 1] && converted[x] !== -1)
      {
        sameSize++;
      }
      else
      {
        if (sameSize > maxSequence)
        {
          maxSequence = sameSize;
        }
        sameSize = 1;
      }
      if (converted[x] === converted[x - 1] + 1 && converted[x - 1] !== -1)
      {
        progressiveSize++;
      }
      else
      {
        if (progressiveSize > maxSequence)
        {
          maxSequence = progressiveSize;
        }
        progressiveSize = 1;
      }
    }
    Iif (sameSize > maxSequence)
    {
      maxSequence = sameSize;
    }
    Iif (progressiveSize > maxSequence)
    {
      maxSequence = progressiveSize;
    }
  });
  return maxSequence;
}
 
module.exports.checkStrongPassword = function (password)
{
  Iif (password.length > 12)
  {
    return true;
  }
 
  if (!password || password.length < 10)
  {
    throw new Error('Password must be provided and must be at least 10 characters. Must consist of lowercase, uppercase letters, numbers, symbols. Must not contain spaces. Must not be sequential.');
  }
 
  if (!password.match(/[a-z]+/))
  {
    throw new Error('Password must consist of lowercase, uppercase letters, numbers and symbols.');
  }
 
  if (!password.match(/[A-Z]+/))
  {
    throw new Error('Password must consist of lowercase, uppercase letters, numbers and symbols.');
  }
 
  Iif (!password.match(/[0-9]+/))
  {
    throw new Error('Password must consist of lowercase, uppercase letters, numbers and symbols.');
  }
 
  if (!password.match(/[^a-zA-Z0-9]+/))
  {
    throw new Error('Password must consist of lowercase, uppercase letters, numbers and symbols.');
  }
 
  if (password.match(/\s+/))
  {
    throw new Error('Password must not consist whitespace characters');
  }
 
  if (findMaxSequenceSize(password) > 2)
  {
    throw new Error('Password must not consist of sequences of size greater than 2.');
  }
 
  return true;
}
 
/**
 * generate strong password
 * @param {number} [length=10] length of password to generate
 * @return {string}
 */
module.exports.generate = function generate(length = 10)
{
  const groups = [
    '0123456789',
    'abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGJIJKLMNOPQRSTUVWXYZ',
    '!@$^*_'
  ];
 
  let availableOrder = [0, 1, 2, 3];
  let order = [];
 
  while (availableOrder.length > 0)
  {
    order.push(availableOrder.splice(Math.floor(Math.random() * availableOrder.length), 1)[0]);
  }
 
  let password = '';
 
  for (let i = 0; i < length; i++)
  {
    let currentGroup = groups[i % groups.length];
 
    password += currentGroup[Math.floor(Math.random() * currentGroup.length)];
  }
 
  return password;
}