-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathsolid.mjs
More file actions
456 lines (378 loc) · 13.4 KB
/
solid.mjs
File metadata and controls
456 lines (378 loc) · 13.4 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// ESM version of solid.js
// global: owaspPasswordStrengthTest, TextEncoder, crypto, fetch
(function () {
'use strict'
const PasswordValidator = function (passwordField, repeatedPasswordField) {
if (
passwordField === null || passwordField === undefined ||
repeatedPasswordField === null || repeatedPasswordField === undefined
) {
return
}
this.passwordField = passwordField
this.repeatedPasswordField = repeatedPasswordField
this.fetchDomNodes()
this.bindEvents()
this.currentStrengthLevel = 0
this.errors = []
}
const FEEDBACK_SUCCESS = 'success'
const FEEDBACK_WARNING = 'warning'
const FEEDBACK_ERROR = 'error'
const ICON_SUCCESS = 'glyphicon-ok'
const ICON_WARNING = 'glyphicon-warning-sign'
const ICON_ERROR = 'glyphicon-remove'
const VALIDATION_SUCCESS = 'has-success'
const VALIDATION_WARNING = 'has-warning'
const VALIDATION_ERROR = 'has-error'
const STRENGTH_PROGRESS_0 = 'progress-bar-danger level-0'
const STRENGTH_PROGRESS_1 = 'progress-bar-danger level-1'
const STRENGTH_PROGRESS_2 = 'progress-bar-warning level-2'
const STRENGTH_PROGRESS_3 = 'progress-bar-success level-3'
const STRENGTH_PROGRESS_4 = 'progress-bar-success level-4'
/**
* Prefetch all dom nodes at initialisation in order to gain time at execution since DOM manipulations
* are really time consuming
*/
PasswordValidator.prototype.fetchDomNodes = function () {
this.form = this.passwordField.closest('form')
this.disablePasswordChecks = this.passwordField.classList.contains('disable-password-checks')
this.passwordGroup = this.passwordField.closest('.form-group')
this.passwordFeedback = this.passwordGroup.querySelector('.form-control-feedback')
this.passwordStrengthMeter = this.passwordGroup.querySelector('.progress-bar')
this.passwordHelpText = this.passwordGroup.querySelector('.help-block')
this.repeatedPasswordGroup = this.repeatedPasswordField.closest('.form-group')
this.repeatedPasswordFeedback = this.repeatedPasswordGroup.querySelector('.form-control-feedback')
}
PasswordValidator.prototype.bindEvents = function () {
this.passwordField.addEventListener('focus', this.resetPasswordFeedback.bind(this))
this.passwordField.addEventListener('keyup', this.instantFeedbackForPassword.bind(this))
this.repeatedPasswordField.addEventListener('keyup', this.validateRepeatedPassword.bind(this))
this.passwordField.addEventListener('blur', this.validatePassword.bind(this))
}
/**
* Events Listeners
*/
PasswordValidator.prototype.resetPasswordFeedback = function () {
this.errors = []
this.resetValidation(this.passwordGroup)
this.resetFeedbackIcon(this.passwordFeedback)
if (!this.disablePasswordChecks) {
this.displayPasswordErrors()
this.instantFeedbackForPassword()
}
}
/**
* Validate password on the fly to provide the user a visual strength meter
*/
PasswordValidator.prototype.instantFeedbackForPassword = function () {
const passwordStrength = this.getPasswordStrength(this.passwordField.value)
const strengthLevel = this.getStrengthLevel(passwordStrength)
if (this.currentStrengthLevel === strengthLevel) {
return
}
this.currentStrengthLevel = strengthLevel
this.updateStrengthMeter()
if (this.repeatedPasswordField.value !== '') {
this.updateRepeatedPasswordFeedback()
}
}
/**
* Validate password and display the error(s) message(s)
*/
PasswordValidator.prototype.validatePassword = function () {
this.errors = []
const password = this.passwordField.value
if (!this.disablePasswordChecks) {
const passwordStrength = this.getPasswordStrength(password)
this.currentStrengthLevel = this.getStrengthLevel(passwordStrength)
if (passwordStrength.errors) {
this.addPasswordError(passwordStrength.errors)
}
this.checkLeakedPassword(password).then(this.handleLeakedPasswordResponse.bind(this))
}
this.setPasswordFeedback()
}
/**
* Validate the repeated password upon typing
*/
PasswordValidator.prototype.validateRepeatedPassword = function () {
this.updateRepeatedPasswordFeedback()
}
/**
* User Feedback manipulators
*/
/**
* Update the strength meter based on OWASP feedback
*/
PasswordValidator.prototype.updateStrengthMeter = function () {
this.resetStrengthMeter()
this.passwordStrengthMeter.classList.add.apply(
this.passwordStrengthMeter.classList,
this.tokenize(this.getStrengthLevelProgressClass())
)
}
PasswordValidator.prototype.setPasswordFeedback = function () {
const feedback = this.getFeedbackFromLevel()
this.updateStrengthMeter()
this.displayPasswordErrors()
this.setFeedbackForField(feedback, this.passwordField)
}
/**
* Update the repeated password feedback icon and color
*/
PasswordValidator.prototype.updateRepeatedPasswordFeedback = function () {
const feedback = this.checkPasswordFieldsEquality() ? FEEDBACK_SUCCESS : FEEDBACK_ERROR
this.setFeedbackForField(feedback, this.repeatedPasswordField)
}
/**
* Display the given feedback on the field
* @param {string} feedback success|error|warning
* @param {HTMLElement} field
*/
PasswordValidator.prototype.setFeedbackForField = function (feedback, field) {
const formGroup = this.getFormGroupElementForField(field)
const visualFeedback = this.getFeedbackElementForField(field)
this.resetValidation(formGroup)
this.resetFeedbackIcon(visualFeedback)
visualFeedback.classList.remove('hidden')
visualFeedback.classList
.add
.apply(
visualFeedback.classList,
this.tokenize(this.getFeedbackIconClass(feedback))
)
formGroup.classList
.add
.apply(
formGroup.classList,
this.tokenize(this.getValidationClass(feedback))
)
}
/**
* Password Strength Helpers
*/
/**
* Get OWASP feedback on the given password. Returns false if the password is empty
* @param password
* @returns {object|false}
*/
PasswordValidator.prototype.getPasswordStrength = function (password) {
if (password === '') {
return false
}
return owaspPasswordStrengthTest.test(password)
}
/**
* Get the password strength level based on password strength feedback object given by OWASP
* @param passwordStrength
* @returns {number}
*/
PasswordValidator.prototype.getStrengthLevel = function (passwordStrength) {
if (passwordStrength === false) {
return 0
}
if (passwordStrength.requiredTestErrors.length !== 0) {
return 1
}
if (passwordStrength.strong === false) {
return 2
}
if (passwordStrength.isPassphrase === false || passwordStrength.optionalTestErrors.length !== 0) {
return 3
}
return 4
}
PasswordValidator.prototype.LEVEL_TO_FEEDBACK_MAP = [
FEEDBACK_ERROR,
FEEDBACK_ERROR,
FEEDBACK_WARNING,
FEEDBACK_SUCCESS,
FEEDBACK_SUCCESS
]
/**
* @returns {string}
*/
PasswordValidator.prototype.getFeedbackFromLevel = function () {
return this.LEVEL_TO_FEEDBACK_MAP[this.currentStrengthLevel]
}
PasswordValidator.prototype.LEVEL_TO_PROGRESS_MAP = [
STRENGTH_PROGRESS_0,
STRENGTH_PROGRESS_1,
STRENGTH_PROGRESS_2,
STRENGTH_PROGRESS_3,
STRENGTH_PROGRESS_4
]
/**
* Get the CSS class for the meter based on the current level
*/
PasswordValidator.prototype.getStrengthLevelProgressClass = function () {
return this.LEVEL_TO_PROGRESS_MAP[this.currentStrengthLevel]
}
PasswordValidator.prototype.addPasswordError = function (error) {
this.errors.push(...(Array.isArray(error) ? error : [error]))
}
PasswordValidator.prototype.displayPasswordErrors = function () {
// Erase the error list content
while (this.passwordHelpText.firstChild) {
this.passwordHelpText.removeChild(this.passwordHelpText.firstChild)
}
// Add the errors in the stack to the DOM
this.errors.map((error) => {
const text = document.createTextNode(error)
const paragraph = document.createElement('p')
paragraph.appendChild(text)
this.passwordHelpText.appendChild(paragraph)
})
}
PasswordValidator.prototype.FEEDBACK_TO_ICON_MAP = []
PasswordValidator.prototype.FEEDBACK_TO_ICON_MAP[FEEDBACK_SUCCESS] = ICON_SUCCESS
PasswordValidator.prototype.FEEDBACK_TO_ICON_MAP[FEEDBACK_WARNING] = ICON_WARNING
PasswordValidator.prototype.FEEDBACK_TO_ICON_MAP[FEEDBACK_ERROR] = ICON_ERROR
/**
* @param success|error|warning feedback
*/
PasswordValidator.prototype.getFeedbackIconClass = function (feedback) {
return this.FEEDBACK_TO_ICON_MAP[feedback]
}
PasswordValidator.prototype.FEEDBACK_TO_VALIDATION_MAP = []
PasswordValidator.prototype.FEEDBACK_TO_VALIDATION_MAP[FEEDBACK_SUCCESS] = VALIDATION_SUCCESS
PasswordValidator.prototype.FEEDBACK_TO_VALIDATION_MAP[FEEDBACK_WARNING] = VALIDATION_WARNING
PasswordValidator.prototype.FEEDBACK_TO_VALIDATION_MAP[FEEDBACK_ERROR] = VALIDATION_ERROR
/**
* @param success|error|warning feedback
*/
PasswordValidator.prototype.getValidationClass = function (feedback) {
return this.FEEDBACK_TO_VALIDATION_MAP[feedback]
}
/**
* Validators
*/
/**
* Check if both password fields are equal
* @returns {boolean}
*/
PasswordValidator.prototype.checkPasswordFieldsEquality = function () {
return this.passwordField.value === this.repeatedPasswordField.value
}
/**
* Check if the password is leaked
* @param password
*/
PasswordValidator.prototype.checkLeakedPassword = function (password) {
const url = 'https://api.pwnedpasswords.com/range/'
return new Promise(function (resolve, reject) {
this.sha1(password).then((digest) => {
const preFix = digest.slice(0, 5)
let suffix = digest.slice(5, digest.length)
suffix = suffix.toUpperCase()
return fetch(url + preFix)
.then(function (response) {
return response.text()
})
.then(function (data) {
resolve(data.indexOf(suffix) > -1)
})
.catch(function (err) {
reject(err)
})
})
}.bind(this))
}
PasswordValidator.prototype.handleLeakedPasswordResponse = function (hasPasswordLeaked) {
if (hasPasswordLeaked === true) {
this.currentStrengthLevel--
this.addPasswordError('This password was exposed in a data breach. Please use a more secure alternative one!')
}
this.setPasswordFeedback()
}
/**
* CSS Classes reseters
*/
PasswordValidator.prototype.resetValidation = function (el) {
const tokenizedClasses = this.tokenize(
VALIDATION_ERROR,
VALIDATION_WARNING,
VALIDATION_SUCCESS
)
el.classList.remove.apply(
el.classList,
tokenizedClasses
)
}
PasswordValidator.prototype.resetFeedbackIcon = function (el) {
const tokenizedClasses = this.tokenize(
ICON_ERROR,
ICON_WARNING,
ICON_SUCCESS
)
el.classList.remove.apply(
el.classList,
tokenizedClasses
)
}
PasswordValidator.prototype.resetStrengthMeter = function () {
const tokenizedClasses = this.tokenize(
STRENGTH_PROGRESS_1,
STRENGTH_PROGRESS_2,
STRENGTH_PROGRESS_3,
STRENGTH_PROGRESS_4
)
this.passwordStrengthMeter.classList.remove.apply(
this.passwordStrengthMeter.classList,
tokenizedClasses
)
}
/**
* Helpers
*/
PasswordValidator.prototype.getFormGroupElementForField = function (field) {
if (field === this.passwordField) {
return this.passwordGroup
}
if (field === this.repeatedPasswordField) {
return this.repeatedPasswordGroup
}
}
PasswordValidator.prototype.getFeedbackElementForField = function (field) {
if (field === this.passwordField) {
return this.passwordFeedback
}
if (field === this.repeatedPasswordField) {
return this.repeatedPasswordFeedback
}
}
/**
* Returns an array of strings ready to be applied on classList.add or classList.remove
* @returns {string[]}
*/
PasswordValidator.prototype.tokenize = function () {
const tokenArray = []
for (const i in arguments) {
tokenArray.push(arguments[i])
}
return tokenArray.join(' ').split(' ')
}
PasswordValidator.prototype.sha1 = function (str) {
const buffer = new TextEncoder('utf-8').encode(str)
return crypto.subtle.digest('SHA-1', buffer).then((hash) => {
return this.hex(hash)
})
}
PasswordValidator.prototype.hex = function (buffer) {
const hexCodes = []
const view = new DataView(buffer)
for (let i = 0; i < view.byteLength; i += 4) {
const value = view.getUint32(i)
const stringValue = value.toString(16)
const padding = '00000000'
const paddedValue = (padding + stringValue).slice(-padding.length)
hexCodes.push(paddedValue)
}
return hexCodes.join('')
}
new PasswordValidator(
document.getElementById('password'),
document.getElementById('repeat_password')
)
})()