-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpassword.js
266 lines (212 loc) · 6.96 KB
/
password.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"use strict";
var templates = {};
// ---------------------------------------------------------
// Security (well, not really security, but a helper to
// redirect the user if they're not logged in. Security
// is handled by the REST API.
// ---------------------------------------------------------
var redirectToLogin = function () {
var location_href = "index.html";
if (location.hash && location.hash.length > 0) {
location_href += "?hash=" + location.hash.substring(1);
}
location.href = location_href;
};
var theDocument = jQuery(document);
theDocument.ajaxSend(function (event, xhr) {
var cartId = getCookie('UltraCartShoppingCartID');
var merchantId = window.merchantId || getCookie('UltraCartMerchantID');
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("X-UC-Merchant-Id", merchantId);
xhr.setRequestHeader("X-UC-Shopping-Cart-Id", cartId);
});
theDocument.ajaxError(function (event, xhr) {
if (xhr.status === 401)
redirectToLogin();
});
jQuery.ajaxSetup({cache: false});
function initialize() {
var html = templates.password({});
jQuery('#password').html(html);
bindFields();
}
function updatePassword() {
clearAllMessages();
// validate the fields
var oldPassword = jQuery.trim(jQuery('#oldPassword').val());
var newPassword = jQuery.trim(jQuery('#newPassword').val());
var newPasswordAgain = jQuery.trim(jQuery('#newPasswordAgain').val());
if (!oldPassword) {
showError("Please provide your current password.");
return;
}
if (!newPassword) {
showError("Please provide a new password.");
return;
}
if (!newPasswordAgain) {
showError("Please type your password again to avoid mistakes.");
return;
}
if (newPassword !== newPasswordAgain) {
showError("The two new password fields do not match. Please type them again.");
return;
}
ultracart.myAccount.changePassword(oldPassword, newPassword, {
success: function () {
showSuccess("Your password was changed.");
},
failure: function (jqXHR) {
var errorMsg = null;
if (jqXHR && jqXHR.getResponseHeader) {
errorMsg = jqXHR.getResponseHeader('UC-REST-ERROR');
}
if (errorMsg) {
showError("Save failed with this error: " + errorMsg);
} else {
showError("Your password could not be changed at this time. Please try again later.");
}
}
});
}
function bindFields() {
jQuery('#cancelButton').unbind().bind('click', function () {
window.history.back();
});
jQuery('#saveButton').unbind().bind('click', updatePassword());
jQuery('#newPassword').unbind().bind('keypress', passwordStrength());
}
// --------------------------------------------------------------
// Password Strength Checker
// grabbed this from SO: http://stackoverflow.com/questions/948172/password-strength-meter
// don't like it? change it.
// --------------------------------------------------------------
// noinspection SpellCheckingInspection
var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// noinspection SpellCheckingInspection
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~";
function checkPassword(strPassword) {
// Reset combination count
var nScore = 0;
// Password length
// -- Less than 4 characters
if (strPassword.length < 5) {
nScore += 5;
}
// -- 5 to 7 characters
else if (strPassword.length > 4 && strPassword.length < 8) {
nScore += 10;
}
// -- 8 or more
else if (strPassword.length > 7) {
nScore += 25;
}
// Letters
var nUpperCount = countContain(strPassword, m_strUpperCase);
var nLowerCount = countContain(strPassword, m_strLowerCase);
var nLowerUpperCount = nUpperCount + nLowerCount;
// -- Letters are all lower case
if (nUpperCount === 0 && nLowerCount !== 0) {
nScore += 10;
}
// -- Letters are upper case and lower case
else if (nUpperCount !== 0 && nLowerCount !== 0) {
nScore += 20;
}
// Numbers
var nNumberCount = countContain(strPassword, m_strNumber);
// -- 1 number
if (nNumberCount === 1) {
nScore += 10;
}
// -- 3 or more numbers
if (nNumberCount >= 3) {
nScore += 20;
}
// Characters
var nCharacterCount = countContain(strPassword, m_strCharacters);
// -- 1 character
if (nCharacterCount === 1) {
nScore += 10;
}
// -- More than 1 character
if (nCharacterCount > 1) {
nScore += 25;
}
// Bonus
// -- Letters and numbers
if (nNumberCount !== 0 && nLowerUpperCount !== 0) {
nScore += 2;
}
// -- Letters, numbers, and characters
if (nNumberCount !== 0 && nLowerUpperCount !== 0 && nCharacterCount !== 0) {
nScore += 3;
}
// -- Mixed case letters, numbers, and characters
if (nNumberCount !== 0 && nUpperCount !== 0 && nLowerCount !== 0 && nCharacterCount !== 0) {
nScore += 5;
}
return nScore;
}
// Runs password through check and then updates GUI
function passwordStrength() {
var password = jQuery('#newPassword').val();
// Check password
var score = checkPassword(password);
// Get controls
var ctlBar = jQuery('#passwordStrengthBar');
var ctlText = jQuery('#passwordStrengthText');
if (!ctlBar || !ctlText)
return;
// Set new width
var pixelWidth = (score * 1.25 > 100) ? 100 : score * 1.25;
ctlBar.css('width', pixelWidth + "px");
// Color and text
var text = null;
var color = null;
if (score >= 80) {
text = "Very Strong";
color = "#008000";
} else if (score >= 60) {
text = "Strong";
color = "#006000";
} else if (score >= 40) {
text = "Average";
color = "#e3cb00";
} else if (score >= 20) {
text = "Weak";
color = "#Fe3d1a";
} else {
text = "Very Weak";
color = "#e71a1a";
}
if (password.length === 0) {
ctlBar.css('backgroundColor', "").css('border', 'none');
ctlText.html('');
}
else {
ctlBar.css('backgroundColor', color).css('border', '1px solid black');
ctlText.html(text);
}
}
// Checks a string for a list of characters
function countContain(strPassword, strCheck) {
// Declare variables
var nCount = 0;
for (var i = 0; i < strPassword.length; i++) {
if (strCheck.indexOf(strPassword.charAt(i)) > -1) {
nCount++;
}
}
return nCount;
}
// --------------------------------------------------------------
// end of password strength checker
// --------------------------------------------------------------
jQuery(document).ready(function () {
enablePleaseWaitMessage();
templates.password = Handlebars.compile(jQuery('#password-template').html());
initialize();
});