Random Password in catalog item run script

Brendan Hallida
Kilo Guru

Hi all,

So I got some assistance from a consultant on this script.  Our requirements are to generate a random password, and the password needed to have the following:

  • 8 characters
  • At least 1 Uppercase
  • At least 1 Lowercase
  • At least 1 Number

The password is then passed over to our custom powershell activities.

We came up with the below:

var randomNum;
lowerUpperNum = false;
while(!lowerUpperNum) {
    randomNum = GlideSecureRandomUtil.getSecureRandomString(8);
    if(hasLowerUpperNumCase(randomNum)) {
        lowerUpperNum = true;
    }
}

current.variables.password = randomNum; //passes string to request item variable

function hasLowerUpperNumCase(_randomNum) {
    return (/[a-zA-Z0-9]/.test(_randomNum));
}

It seemed to be working, however, i just got a result without a lowercase: ie this was it 38B9U485.

Anyone know what could be happening, or if there was a better way to meet our requirements?

 

Cheers,

Brendan

 

1 ACCEPTED SOLUTION

Try running it with the print enclosed inside of an evaluation of lowerUpperNum. As you've written it, the password is always printed, even if lowerUpperNum evaluates as false.

I just tested the following and got 1,000 matches exactly.

 

for (i=0; i<1000; ){
	var lowerUpperNum = false;
	while(!lowerUpperNum) {

		var password = GlideSecureRandomUtil.getSecureRandomString(8);	
		lowerUpperNum = !!((password).match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/));
		
		if(lowerUpperNum) {
			gs.print(password);
			i++;
		}

		//current.variables.password = GlideSecureRandomUtil.getSecureRandomString(8);	
		//lowerUpperNum = !!((current.variables.password).match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/));
	}
}

 

You could take it a step further and modify it for your application by the following, which guarantees that lowerUpperNum needs to be truthy before password is assigned.

 

var lowerUpperNum = false;
while(!lowerUpperNum) {

	var password = GlideSecureRandomUtil.getSecureRandomString(8);	
	lowerUpperNum = !!((password).match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/));
	
	if(lowerUpperNum) {
		current.variables.password = password;	
	}
	
}

View solution in original post

11 REPLIES 11

Brendan Hallida
Kilo Guru

 I just found this post, which contains a regex validation for our requirements.

https://stackoverflow.com/questions/14850553/javascript-regex-for-password-containing-at-least-8-cha...

 

it can be seen in action here: http://www.rubular.com/r/jf69EWPQ4N

 

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/

 

What is the best way we can add this into my script to validate the output against this regex before commiting the password to the variable?

 

Cheers,

Brendan

 

Brendan Hallida
Kilo Guru

just bumping to see if anyone had any ideas?

Hello Brendan, I tried like this and worked fine.

return (/[a-z][A-Z][0-9]/.test(_randomNum));

Cheers for your reply 🙂

I changed it up, however, I am still getting passwords that do not contain all 3 requirememnts.

I am running the following in the background scripts, and get the below image:

var randomNum;
lowerUpperNum = false;
while(!lowerUpperNum) {
    randomNum = GlideSecureRandomUtil.getSecureRandomString(8);
    if(hasLowerUpperNumCase(randomNum)) {
        lowerUpperNum = true;
    }
}

//gs.print(randomNum);

var count = 0;

while(count != 100) {
gs.log(GlideSecureRandomUtil.getSecureRandomString(8));
count++
}


function hasLowerUpperNumCase(_randomNum) {
return (/[a-z][A-Z][0-9]/.test(_randomNum));

}

 

find_real_file.png