Name

global.PasswordPolicyCompliantAutoGenPassword

Description

No description available

Script

var PasswordPolicyCompliantAutoGenPassword = Class.create();
PasswordPolicyCompliantAutoGenPassword.prototype = {
  category: 'password_reset.extension.password_generator',    // DO NOT REMOVE THIS LINE!
  
  initialize: function() {
  },
  
  process: function(params) {
  	if(params.enablePasswordPolicy){
  		var userName = GlideUser.getUserByID(gs.getSession().getProperty('sysparm_sys_user_id')).getName();
  		var autoGeneratedPwd = new SNC.PasswordGenerator(params.passwordPolicyId).generate({"username" : userName});
  		if (GlideStringUtil.notNil(autoGeneratedPwd)) {
  			autoGeneratedPwd = autoGeneratedPwd.replaceAll('<','>');
  			return autoGeneratedPwd;
  		}
  	}
      return this.generatePasswordDefault();
  },
  
  /**
   * Generates a cryptographically secure pseudo-random password, with length between 8 and 12,
   * containing at least one special character 
   * 
   * @return {string} New string with character inserted
   */
  generatePasswordDefault: function() {
  	var specialCharacters = "!\"#$%'()*+,-./:;=?@[\]^_`{|}~";
  	var secureRandom = GlideSecureRandomUtil;
  	var pwdBaseLength = secureRandom.getSecureRandomIntBound(3) + 7;
  	var newPwd = secureRandom.getSecureRandomString(pwdBaseLength);
  	var numSpecialCharacters = secureRandom.getSecureRandomIntBound(3) + 1;
  	for (var i = 0; i < numSpecialCharacters; i++) {
  		var idx = secureRandom.getSecureRandomIntBound(newPwd.length);
  		var c = specialCharacters.charAt(secureRandom.getSecureRandomIntBound(specialCharacters.length));
  		newPwd = this._insertCharacterAtIndex(newPwd, c, idx);
  	}
  	
  	return newPwd;
  },
  
  /**
   * Returns a string with a character inserted at a specific index
   *
   * @param {string} str The string to insert the character into
   * @param {string} c The character to instert into the string
   * @param {number} idx The index in the string to insert the character
   * @return {string} New string with character inserted
   */
  _insertCharacterAtIndex: function(str, c, idx) {
  	return (str.slice(0, idx) + c + str.slice(idx));
  },

  type: 'PasswordPolicyCompliantAutoGenPassword'
};

Sys ID

f4f48ed1d5880010f877ae4424e764dc

Offical Documentation

Official Docs: