-
Notifications
You must be signed in to change notification settings - Fork 10
/
gold-password-input-strength-meter.html
337 lines (304 loc) · 11.8 KB
/
gold-password-input-strength-meter.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input-addon-behavior.html">
<link rel="import" href="../paper-styles/color.html">
<link rel="import" href="../paper-progress/paper-progress.html">
<link rel="import" href="../paper-tooltip/paper-tooltip.html">
<link rel="import" href="./gold-password-input-icons.html">
<!--
`<gold-password-input-strength-meter>` is a password strength meter for use with
`<gold-password-input>`. It shows the strength of the password entered in the input.
### Styling
The following mixin is available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--gold-password-input-strength-meter-none-color` | The text color for a strength value of None | `--paper-red-700`
`--gold-password-input-strength-meter-veryweak-color` | The text color for a strength value of Very Weak | `--paper-red-700`
`--gold-password-input-strength-meter-weak-color` | The text color for a strength value of Weak | `--paper-orange-700`
`--gold-password-input-strength-meter-medium-color` | The text color for a strength value of Medium | `--paper-yellow-700`
`--gold-password-input-strength-meter-strong-color` | The text color for a strength value of Strong | `--paper-blue-700`
`--gold-password-input-strength-meter-verystrong-color` | The text color for a strength value of Very Strong | `--paper-green-700`
`--gold-password-input-strength-meter` | Mixin applied to the element | `{}`
-->
<dom-module id="gold-password-input-strength-meter">
<template>
<style>
:host {
outline: inherit;
@apply --paper-font-caption;
@apply --gold-password-input-strength-meter;
}
/* FIXME PG: suppress when paper-input-container has reflectToAttribute for invalid... cf. ( https://github.com/PolymerElements/paper-input/pull/258 ) */
/*
:host-context([invalid]) {
visibility: hidden;
};
*/
:host([invalid]), :host([warning]) {
visibility: hidden;
};
/* FIXME PG: remove !important used as current workaround before getting the proper explanation... cf. ( https://github.com/Polymer/polymer/issues/3059 )*/
.None {
color: var(--gold-password-input-strength-meter-none-color, var(--paper-grey-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-none-color, var(--paper-grey-700)) !important;
}
.VeryWeak {
color: var(--gold-password-input-strength-meter-veryweak-color, var(--paper-red-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-veryweak-color, var(--paper-red-700)) !important;
}
.Weak {
color: var(--gold-password-input-strength-meter-weak-color, var(--paper-orange-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-weak-color, var(--paper-orange-700)) !important;
}
.Medium {
color: var(--gold-password-input-strength-meter-medium-color, var(--paper-yellow-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-medium-color, var(--paper-yellow-700)) !important;
}
.Strong {
color: var(--gold-password-input-strength-meter-strong-color, var(--paper-blue-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-strong-color, var(--paper-blue-700)) !important;
}
.VeryStrong {
color: var(--gold-password-input-strength-meter-verystrong-color, var(--paper-green-700)) !important;
--paper-progress-active-color: var(--gold-password-input-strength-meter-verystrong-color, var(--paper-green-700)) !important;
}
#strengthProgress {
position: absolute;
width: 100%;
height: 2px;
top: -2.2px;
}
#strengthLabel iron-icon {
width: 14px;
height: 14px;
padding-bottom: 3px;
color: var(--paper-grey-700);
};
#strengthTooltip * {
display: block;
color: white !important;
};
</style>
<paper-progress
id="strengthProgress"
hidden$=[[_computeShowProgress(useProgress,_strengthMeterScore)]]
value=[[_computeProgress(_strengthMeterScore)]]
class$=[[_strengthMeterScoreLabel]]>
</paper-progress>
<span id="strengthLabel" hidden$=[[noLabel]]>
<iron-icon
id="strengthTooltipIcon"
icon="gold-password-input:info"
hidden$="[[disableTooltip]]"
></iron-icon>
[[strengthMeterLabels.Label]]:
<span class$=[[_strengthMeterScoreLabel]]>[[_computeStrengthMeterLabel(_strengthMeterScoreLabel)]]</span>
<paper-tooltip
id="strengthTooltip"
for="strengthTooltipIcon"
position="top"
offset="4"
fit-to-visible-bounds>
<div class="warning">
<span><b>Warning: </b>[[_strengthMeterFeedback.warning]]</span>
</div>
<div class="suggestions">
<span><b>Suggestions: </b>[[_strengthMeterFeedback.suggestions]]</span>
</div>
</paper-tooltip>
</span>
</template>
<!-- TODO PG: remove this ASAP when https://github.com/PolymerElements/paper-tooltip/issues/121 is fixed -->
<script src="../web-animations-js/web-animations-next-lite.min.js"></script>
<script>
(function() {
var isZxcvbnLoaded = false;
Polymer({
is: 'gold-password-input-strength-meter',
behaviors: [
Polymer.PaperInputAddonBehavior
],
properties: {
/**
* Specifies the label to be used to display the score to the user.
*/
strengthMeterLabels: {
type: Object,
value: function() { return {
Label: "Strength",
None: "None",
VeryWeak: "Very Weak",
Weak: "Weak",
Medium: "Medium",
Strong: "Strong",
VeryStrong: "Very Strong"};
}
},
// FIXME PG: suppress when paper-input-container has reflectToAttribute for invalid... cf. ( https://github.com/PolymerElements/paper-input/pull/258 )
/**
* True if the error is showing.
*/
invalid: {
readOnly: true,
reflectToAttribute: true,
type: Boolean
},
/**
* Contains the current label score value of the input.
*/
_strengthMeterScoreLabel: {
type: String,
value: 'None'
},
/**
* Provides additional hints (Warning and Suggestions) to help a user strengthen its password
*/
_strengthMeterFeedback: {
type: Object,
value: function() { return {
warning: 'None',
suggestions: 'None'};
}
},
/**
* Contains the current score value of the input.
*/
_strengthMeterScore: {
type: Number,
value: 0
},
/**
* Min strength meter score label (used on validate).
*/
minStrengthMeterScoreLabel: String,
/**
* Controls wether the tooltip is rendered or not.
*/
disableTooltip: Boolean,
/**
* Controls whether to use labels to show the strength result.
*/
noLabel: Boolean,
/**
* Controls whether to use a progress bar to show the strength result.
*/
useProgress: Boolean,
/**
* True if a warning is showing.
*/
warning: {
reflectToAttribute: true,
type: Boolean
},
},
ready: function() {
isZxcvbnLoaded = typeof zxcvbn !== "undefined";
if (!isZxcvbnLoaded) {
isZxcvbnLoaded = true;
var oScript = document.createElement("script");
oScript.type = "text\/javascript";
oScript.onerror = function(err) {
isZxcvbnLoaded = false;
throw new URIError("The script " + err.target.src + " is not accessible.");
};
this.parentNode.insertBefore(oScript, this);
oScript.src = this.resolveUrl("../zxcvbn/dist/zxcvbn.js");
}
},
update: function(state) {
// FIXME PG: suppress when paper-input-container has reflectToAttribute for invalid... cf. ( https://github.com/PolymerElements/paper-input/pull/258 )
this._setInvalid(state.invalid);
if (!state.inputElement) {
return;
}
state.value = state.value || '';
if (state.value === '') {
this._strengthMeterScoreLabel = 'None';
this._strengthMeterScore = 0;
this.set('_strengthMeterFeedback.warning', 'None');
this.set('_strengthMeterFeedback.suggestions', 'None');
return;
}
if (!isZxcvbnLoaded) {
return;
}
// Use zxcvbn to evaluate the strength of the password.
var result = zxcvbn(state.value);
// Current score
this._strengthMeterScore = result.score + 1;
// update the zxcvbn score property
switch(this._strengthMeterScore) {
case 1:
this._strengthMeterScoreLabel = 'VeryWeak';
break;
case 2:
this._strengthMeterScoreLabel = 'Weak';
break;
case 3:
this._strengthMeterScoreLabel = 'Medium';
break;
case 4:
this._strengthMeterScoreLabel = 'Strong';
break;
case 5:
this._strengthMeterScoreLabel = 'VeryStrong';
break;
default:
this._strengthMeterScoreLabel = 'None';
}
// update the user feedback if any
this._strengthMeterFeedback = result.feedback;
if (this._strengthMeterFeedback.warning === "") {
this.set('_strengthMeterFeedback.warning', 'None');
}
if (this._strengthMeterFeedback.suggestions.length === 0) {
this.set('_strengthMeterFeedback.suggestions', 'None');
}
},
_computeStrengthMeterLabel: function(_strengthMeterScoreLabel) {
return this.strengthMeterLabels[_strengthMeterScoreLabel];
},
_getStrengthMeterFromLabel(strengthMeterLabel) {
return Object.keys(this.strengthMeterLabels).find(strengthMeterKey => this.strengthMeterLabels[strengthMeterKey] === strengthMeterLabel);
},
_getStrengthMeterScoreFromStrengthMeter(strengthMeter) {
switch(strengthMeter) {
case 'VeryWeak':
return 1;
break;
case 'Weak':
return 2;
break;
case 'Medium':
return 3;
break;
case 'Strong':
return 4;
break;
case 'VeryStrong':
return 5;
break;
default:
return 0;
}
},
_computeShowProgress: function(useProgress, score) {
return !(useProgress && (score !== 0));
},
_computeProgress: function(score) {
return score * 20;
},
validate: function() {
if (this.minStrengthMeterScoreLabel) {
const minStrengthMeter = this._getStrengthMeterFromLabel(this.minStrengthMeterScoreLabel);
const minStrengthMeterScore = this._getStrengthMeterScoreFromStrengthMeter(minStrengthMeter);
if (this._strengthMeterScore >= minStrengthMeterScore) {
return true;
}
return false;
}
return true;
}
});
}());
</script>
</dom-module>