-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce strong passwords in UI (#1266)
- Loading branch information
Showing
12 changed files
with
420 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { LitElement, html, css } from "lit"; | ||
import { msg, localized } from "@lit/localize"; | ||
import { property } from "lit/decorators.js"; | ||
import { when } from "lit/directives/when.js"; | ||
import type { ZxcvbnResult } from "@zxcvbn-ts/core"; | ||
|
||
/** | ||
* Show results of password strength estimate | ||
* | ||
* Usage example: | ||
* ```ts | ||
* <btrix-pw-strength-alert .result=${this.zxcvbnResult}></btrix-pw-strength-alert> | ||
* ``` | ||
*/ | ||
@localized() | ||
export class PasswordStrengthAlert extends LitElement { | ||
@property({ type: String }) | ||
result?: ZxcvbnResult; | ||
|
||
/** Minimum acceptable score */ | ||
@property({ type: String }) | ||
min = 1; | ||
|
||
/** Optimal score */ | ||
@property({ type: String }) | ||
optimal = 4; | ||
|
||
static styles = css` | ||
sl-alert::part(message) { | ||
/* Decrease padding size: */ | ||
--sl-spacing-large: var(--sl-spacing-small); | ||
} | ||
sl-alert[variant="danger"] .icon { | ||
color: var(--sl-color-danger-600); | ||
} | ||
sl-alert[variant="warning"] .icon { | ||
color: var(--sl-color-warning-600); | ||
} | ||
sl-alert[variant="primary"] .icon { | ||
color: var(--sl-color-primary-600); | ||
} | ||
sl-alert[variant="success"] .icon { | ||
color: var(--sl-color-success-600); | ||
} | ||
p, | ||
ul { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
ul { | ||
list-style-position: inside; | ||
} | ||
.score { | ||
display: flex; | ||
gap: var(--sl-spacing-x-small); | ||
align-items: center; | ||
} | ||
.icon { | ||
font-size: var(--sl-font-size-large); | ||
} | ||
.label { | ||
color: var(--sl-color-neutral-900); | ||
font-weight: var(--sl-font-weight-semibold); | ||
} | ||
.feedback { | ||
color: var(--sl-color-neutral-700); | ||
margin-left: var(--sl-spacing-x-large); | ||
} | ||
.text { | ||
margin-top: var(--sl-spacing-small); | ||
} | ||
`; | ||
|
||
render() { | ||
if (!this.result) return; | ||
|
||
const { score, feedback } = this.result; | ||
let scoreProps = { | ||
icon: "exclamation-triangle", | ||
label: msg("Very weak password"), | ||
variant: "danger", | ||
}; | ||
switch (score) { | ||
case 2: | ||
scoreProps = { | ||
icon: "exclamation-circle", | ||
label: msg("Weak password"), | ||
variant: "warning", | ||
}; | ||
break; | ||
case 3: | ||
scoreProps = { | ||
icon: "shield-check", | ||
label: msg("Acceptably strong password"), | ||
variant: "primary", | ||
}; | ||
break; | ||
case 4: | ||
scoreProps = { | ||
icon: "shield-fill-check", | ||
label: msg("Very strong password"), | ||
variant: "success", | ||
}; | ||
break; | ||
default: | ||
break; | ||
} | ||
if (score < this.min) { | ||
scoreProps.label = msg("Please choose a stronger password"); | ||
} | ||
return html` | ||
<sl-alert variant=${scoreProps.variant as any} open> | ||
<div class="score"> | ||
<sl-icon class="icon" name=${scoreProps.icon}></sl-icon> | ||
<p class="label">${scoreProps.label}</p> | ||
</div> | ||
<div class="feedback"> | ||
${when( | ||
feedback.warning, | ||
() => html` <p class="text">${feedback.warning}</p> ` | ||
)} | ||
${when(feedback.suggestions.length, () => | ||
feedback.suggestions.length === 1 | ||
? html`<p class="text"> | ||
${msg("Suggestion:")} ${feedback.suggestions[0]} | ||
</p>` | ||
: html`<p class="text">${msg("Suggestions:")}</p> | ||
<ul> | ||
${feedback.suggestions.map( | ||
(text) => html`<li>${text}</li>` | ||
)} | ||
</ul>` | ||
)} | ||
${when( | ||
score >= this.min && score < this.optimal, | ||
() => html` | ||
<p class="text"> | ||
${msg( | ||
"Tip: To generate very strong passwords, consider using a password manager." | ||
)} | ||
</p> | ||
` | ||
)} | ||
</div> | ||
</sl-alert> | ||
`; | ||
} | ||
} |
Oops, something went wrong.