Last active
July 7, 2020 09:21
-
-
Save Satak/a7c6dcd3d69416d2f11e62d8e404f539 to your computer and use it in GitHub Desktop.
Password generator function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onLoad() { | |
var generatePassword = new GlideAjax('x_fstfs_fj_cmp.TfSCMPClientUtils'); | |
function setPassword (password) { | |
g_form.setValue('AWS_Windows_UserPassword', password); | |
} | |
generatePassword.addParam('sysparm_name', 'generateRandomPassword'); | |
generatePassword.getXMLAnswer(setPassword); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates random password. Uses ES5 syntax that can be used in ServiceNow. | |
* @param {number} length - length of the password, default 20. Min 8, max 100 | |
* @param {boolean} noSpecials - truthie if you don't want specil characters like !?-_&% | |
* @param {boolean} noNumbers - truthie if you don't want numbers | |
* @param {boolean} noCapitals - truthie if you don't want capital letters | |
* @param {boolean} noForceComplexity - truthie to disable enforcing all char types in password | |
* @return {string} randomly generated password string | |
*/ | |
function generateRandomPassword(length, noSpecials, noNumbers, noCapitals, noForceComplexity) { | |
var defaultLength = 20; | |
var minPasswordLength = 8; | |
var maxPasswordLength = 100; | |
var lower = 'abcdefghijklmnopqrstuvwxyz'; | |
var numbers = '0123456789'; | |
var specials = '!?_&%*-'; // do not add - to middle since it will break the regex rule! | |
var chars = [ | |
{ chars: lower, disabled: false }, | |
{ chars: lower.toUpperCase(), disabled: noCapitals }, | |
{ chars: numbers, disabled: noNumbers }, | |
{ chars: specials, disabled: noSpecials }, | |
]; | |
var allCharacters = chars | |
.map(function (i) { | |
if (!i.disabled) return i.chars; | |
}) | |
.join(''); | |
var validatedLength; | |
var outputArray; | |
var password; | |
function validate(n) { | |
return !(isNaN(n) || n < minPasswordLength || n > maxPasswordLength); | |
} | |
function getRandomCharacter() { | |
return allCharacters.charAt( | |
Math.floor(Math.random() * allCharacters.length); | |
); | |
} | |
function generatePassword(arr) { | |
return arr | |
.map(function (i) { | |
return getRandomCharacter(); | |
}) | |
.join(''); | |
} | |
function validatePassword() { | |
var regexRule; | |
return chars.map(function (i) { | |
if (i.disabled) return true; | |
regexRule = new RegExp('[' + i.chars + ']'); | |
return regexRule.test(password); | |
}).every(function(i){return i}); | |
} | |
validatedLength = validate(length) ? parseInt(length, 10) : defaultLength; | |
outputArray = Array.apply(null, Array(validatedLength)); | |
do { | |
password = generatePassword(outputArray); | |
} | |
while (!noForceComplexity && !validatePassword(password)); | |
return password; | |
} | |
console.log(generateRandomPassword(22)); | |
/* | |
as default value in catalog item (does not work in cmp portal, use client script instead): | |
javascript:new x_fstfs_fj_cmp.TfSCMPClientUtils().generateRandomPassword(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment