setp12password.js (4034B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 /** 7 * @file Implements the functionality of setp12password.xhtml: a dialog that lets 8 * the user confirm the password to set on a PKCS #12 file. 9 * @param {nsISupports} window.arguments.0 10 * Object to set the return values of calling the dialog on, queryable 11 * to the underlying type of SetP12PasswordReturnValues. 12 */ 13 14 /** 15 * @typedef SetP12PasswordReturnValues 16 * @type {nsIWritablePropertyBag2} 17 * @property {boolean} confirmedPassword 18 * Set to true if the user entered two matching passwords and 19 * confirmed the dialog. 20 * @property {string} password 21 * The password the user entered. Undefined value if 22 * |confirmedPassword| is not true. 23 */ 24 25 /** 26 * onload() handler. 27 */ 28 function onLoad() { 29 // Ensure the first password textbox has focus. 30 let pw1 = document.getElementById("pw1"); 31 pw1.focus(); 32 pw1.addEventListener("input", () => onPasswordInput(true)); 33 let pw2 = document.getElementById("pw2"); 34 pw2.addEventListener("input", () => onPasswordInput(false)); 35 document.addEventListener("dialogaccept", onDialogAccept); 36 document.addEventListener("dialogcancel", onDialogCancel); 37 } 38 39 /** 40 * ondialogaccept() handler. 41 */ 42 function onDialogAccept() { 43 let password = document.getElementById("pw1").value; 44 45 let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2); 46 retVals.setPropertyAsBool("confirmedPassword", true); 47 retVals.setPropertyAsAString("password", password); 48 } 49 50 /** 51 * ondialogcancel() handler. 52 */ 53 function onDialogCancel() { 54 let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2); 55 retVals.setPropertyAsBool("confirmedPassword", false); 56 } 57 58 /** 59 * Calculates the strength of the given password, suitable for use in updating 60 * a progress bar that represents said strength. 61 * 62 * The strength of the password is calculated by checking the number of: 63 * - Characters 64 * - Numbers 65 * - Non-alphanumeric chars 66 * - Upper case characters 67 * 68 * @param {string} password 69 * The password to calculate the strength of. 70 * @returns {number} 71 * The strength of the password in the range [0, 100]. 72 */ 73 function getPasswordStrength(password) { 74 let lengthStrength = password.length; 75 if (lengthStrength > 5) { 76 lengthStrength = 5; 77 } 78 79 let nonNumericChars = password.replace(/[0-9]/g, ""); 80 let numericStrength = password.length - nonNumericChars.length; 81 if (numericStrength > 3) { 82 numericStrength = 3; 83 } 84 85 let nonSymbolChars = password.replace(/\W/g, ""); 86 let symbolStrength = password.length - nonSymbolChars.length; 87 if (symbolStrength > 3) { 88 symbolStrength = 3; 89 } 90 91 let nonUpperAlphaChars = password.replace(/[A-Z]/g, ""); 92 let upperAlphaStrength = password.length - nonUpperAlphaChars.length; 93 if (upperAlphaStrength > 3) { 94 upperAlphaStrength = 3; 95 } 96 97 let strength = 98 lengthStrength * 10 - 99 20 + 100 numericStrength * 10 + 101 symbolStrength * 15 + 102 upperAlphaStrength * 10; 103 if (strength < 0) { 104 strength = 0; 105 } 106 if (strength > 100) { 107 strength = 100; 108 } 109 110 return strength; 111 } 112 113 /** 114 * oninput() handler for both password textboxes. 115 * 116 * @param {boolean} recalculatePasswordStrength 117 * Whether to recalculate the strength of the first password. 118 */ 119 function onPasswordInput(recalculatePasswordStrength) { 120 let pw1 = document.getElementById("pw1").value; 121 122 if (recalculatePasswordStrength) { 123 document.getElementById("pwmeter").value = getPasswordStrength(pw1); 124 } 125 126 // Disable the accept button if the two passwords don't match, and enable it 127 // if the passwords do match. 128 let pw2 = document.getElementById("pw2").value; 129 document.getElementById("setp12password").getButton("accept").disabled = 130 pw1 != pw2; 131 } 132 133 window.addEventListener("load", () => onLoad());