summaryrefslogtreecommitdiffstats
path: root/security/manager/pki/resources/content/setp12password.js
blob: 14200c36cef082fb6fe642a5ce7cad0fdd4a1675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

/**
 * @file Implements the functionality of setp12password.xhtml: a dialog that lets
 *       the user confirm the password to set on a PKCS #12 file.
 * @param {nsISupports} window.arguments.0
 *           Object to set the return values of calling the dialog on, queryable
 *           to the underlying type of SetP12PasswordReturnValues.
 */

/**
 * @typedef SetP12PasswordReturnValues
 * @type {nsIWritablePropertyBag2}
 * @property {boolean} confirmedPassword
 *           Set to true if the user entered two matching passwords and
 *           confirmed the dialog.
 * @property {string} password
 *           The password the user entered. Undefined value if
 *           |confirmedPassword| is not true.
 */

/**
 * onload() handler.
 */
function onLoad() {
  // Ensure the first password textbox has focus.
  document.getElementById("pw1").focus();
  document.addEventListener("dialogaccept", onDialogAccept);
  document.addEventListener("dialogcancel", onDialogCancel);
}

/**
 * ondialogaccept() handler.
 */
function onDialogAccept() {
  let password = document.getElementById("pw1").value;

  let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2);
  retVals.setPropertyAsBool("confirmedPassword", true);
  retVals.setPropertyAsAString("password", password);
}

/**
 * ondialogcancel() handler.
 */
function onDialogCancel() {
  let retVals = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2);
  retVals.setPropertyAsBool("confirmedPassword", false);
}

/**
 * Calculates the strength of the given password, suitable for use in updating
 * a progress bar that represents said strength.
 *
 * The strength of the password is calculated by checking the number of:
 *   - Characters
 *   - Numbers
 *   - Non-alphanumeric chars
 *   - Upper case characters
 *
 * @param {string} password
 *        The password to calculate the strength of.
 * @returns {number}
 *          The strength of the password in the range [0, 100].
 */
function getPasswordStrength(password) {
  let lengthStrength = password.length;
  if (lengthStrength > 5) {
    lengthStrength = 5;
  }

  let nonNumericChars = password.replace(/[0-9]/g, "");
  let numericStrength = password.length - nonNumericChars.length;
  if (numericStrength > 3) {
    numericStrength = 3;
  }

  let nonSymbolChars = password.replace(/\W/g, "");
  let symbolStrength = password.length - nonSymbolChars.length;
  if (symbolStrength > 3) {
    symbolStrength = 3;
  }

  let nonUpperAlphaChars = password.replace(/[A-Z]/g, "");
  let upperAlphaStrength = password.length - nonUpperAlphaChars.length;
  if (upperAlphaStrength > 3) {
    upperAlphaStrength = 3;
  }

  let strength =
    lengthStrength * 10 -
    20 +
    numericStrength * 10 +
    symbolStrength * 15 +
    upperAlphaStrength * 10;
  if (strength < 0) {
    strength = 0;
  }
  if (strength > 100) {
    strength = 100;
  }

  return strength;
}

/**
 * oninput() handler for both password textboxes.
 *
 * @param {boolean} recalculatePasswordStrength
 *                  Whether to recalculate the strength of the first password.
 */
function onPasswordInput(recalculatePasswordStrength) {
  let pw1 = document.getElementById("pw1").value;

  if (recalculatePasswordStrength) {
    document.getElementById("pwmeter").value = getPasswordStrength(pw1);
  }

  // Disable the accept button if the two passwords don't match, and enable it
  // if the passwords do match.
  let pw2 = document.getElementById("pw2").value;
  document.getElementById("setp12password").getButton("accept").disabled =
    pw1 != pw2;
}