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/. */
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.
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.
29 // Ensure the first password textbox has focus.
30 document.getElementById("pw1").focus();
31 document.addEventListener("dialogaccept", onDialogAccept);
32 document.addEventListener("dialogcancel", onDialogCancel);
36 * ondialogaccept() handler.
38 function onDialogAccept() {
39 let password = document.getElementById("pw1").value;
41 let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2);
42 retVals.setPropertyAsBool("confirmedPassword", true);
43 retVals.setPropertyAsAString("password", password);
47 * ondialogcancel() handler.
49 function onDialogCancel() {
50 let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2);
51 retVals.setPropertyAsBool("confirmedPassword", false);
55 * Calculates the strength of the given password, suitable for use in updating
56 * a progress bar that represents said strength.
58 * The strength of the password is calculated by checking the number of:
61 * - Non-alphanumeric chars
62 * - Upper case characters
64 * @param {string} password
65 * The password to calculate the strength of.
67 * The strength of the password in the range [0, 100].
69 function getPasswordStrength(password) {
70 let lengthStrength = password.length;
71 if (lengthStrength > 5) {
75 let nonNumericChars = password.replace(/[0-9]/g, "");
76 let numericStrength = password.length - nonNumericChars.length;
77 if (numericStrength > 3) {
81 let nonSymbolChars = password.replace(/\W/g, "");
82 let symbolStrength = password.length - nonSymbolChars.length;
83 if (symbolStrength > 3) {
87 let nonUpperAlphaChars = password.replace(/[A-Z]/g, "");
88 let upperAlphaStrength = password.length - nonUpperAlphaChars.length;
89 if (upperAlphaStrength > 3) {
90 upperAlphaStrength = 3;
96 numericStrength * 10 +
98 upperAlphaStrength * 10;
102 if (strength > 100) {
110 * oninput() handler for both password textboxes.
112 * @param {boolean} recalculatePasswordStrength
113 * Whether to recalculate the strength of the first password.
115 function onPasswordInput(recalculatePasswordStrength) {
116 let pw1 = document.getElementById("pw1").value;
118 if (recalculatePasswordStrength) {
119 document.getElementById("pwmeter").value = getPasswordStrength(pw1);
122 // Disable the accept button if the two passwords don't match, and enable it
123 // if the passwords do match.
124 let pw2 = document.getElementById("pw2").value;
125 document.getElementById("setp12password").getButton("accept").disabled =