Generating Passwords


Searching for an OOB* feature to generate User passwords, I have found the example below.

var username = 'itil';

var password = SNC.PasswordPolicyEvaluator.generateUserPassword(username);
gs.info(password); // to view the password

If we try to access the SNC API in a Scoped Application it will not work because it is designed for System Administrators. They can run scripts within the Global scope. 

Last month while participating as a Hacktoberfest maintainer I had the chance to evaluate a contribution regarding password generation:


var PasswordGenerator = Class.create();
PasswordGenerator.prototype = {
initialize: function() {},

//
// Input: Minimum password length that is required
// Returns a random password for the min length specified
//
generate: function(givenPasswordLength) {
var specials = '!@#$%&*()_+<>[].~';
var lowercase = 'abcdefghijklmnopqrstuvwxyz';
var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var numbers = '0123456789';
var all = specials + lowercase + uppercase + numbers;

String.prototype.pick = function(min, max) {
var n, chars = '';
if (typeof max === 'undefined') {
n = min;
} else {
n = min + Math.floor(Math.random() * (max - min));
}
for (var i = 0; i < n; i++) {
chars += this.charAt(Math.floor(Math.random() * this.length));
}
return chars;
};


String.prototype.shuffle = function() {
var array = this.split('');
var tmp, current, top = array.length;

if (top)
while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array.join('');
};

//adjust the pick numbers here to increase or decrease password strength
var ent = givenPasswordLength - 4;
if (ent < 0) {
ent = 0;
}

var password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) +
                        numbers.pick(1) + all.pick(ent)).shuffle();
return (password + '');
},


type: 'PasswordGenerator'
};

You can create the Script Include above in a Scoped Application. 

Let's test it from the Scripts - Background in our PDI**:

var helper = new PasswordGenerator(); 
var result = helper.generate(20); // password length
gs.info("Password: " + result);

I approved the contribution because despite the fact there is an OOB feature to generate User passwords, this one is specially useful because: 

1) We can specify the password length to be generated;
2) It runs on Scoped Applications;
3) We have many websites on the Internet capable of generating passwords but it is not a good idea to rely on unknown websites to generate your password, right?
4) GlideEncrypter API is deprecated and should not be used


Thank you.


____
*OOB means already developed functionality by ServiceNow developers
**PDI stands for Personal Developer Instance
Hacktoberfest contribution Generates a random password with a specified length
There is a plugin called ServiceNow Vault which is not in this article scope

Comments

Popular posts from this blog

Hacktoberfest

VIDEO How to fix a reference to the Choice [sys_choice] table

How to hide a Related List