-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(project): improve and cleanup documentation (#285)
* docs(project): improve and cleanup documentation * docs(project): improve and cleanup documentation
- Loading branch information
Showing
8 changed files
with
155 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Best Practices | ||
For optimal scoring results, we recommend always including both the common and English language packages. If a language package is available for your specific language, import that as well. If your language is missing, feel free to contribute by opening a pull request (PR). In the meantime, you can extend the default set. | ||
|
||
## Language Dictionaries | ||
To achieve robust password strength scoring, we suggest using the dictionaries for English, common words, and the user’s language (if available). These dictionaries ensure that passwords are compared against a comprehensive set of words for more accurate scoring. | ||
|
||
## Levenshtein Distance | ||
Enable the Levenshtein distance calculation to better identify variations of words within dictionaries. This helps catch slight modifications to common words that might otherwise go undetected (e.g., password vs. p@ssw0rd). | ||
|
||
## Pwned Password Matcher | ||
The Pwned Matcher should be used to check passwords against databases of leaked or compromised passwords. This informs users if their password has already been exposed in previous data breaches. | ||
|
||
## User Input Context | ||
Incorporate a user inputs dictionary to check passwords against personal information, such as the user's name, email address, or other relevant data. Additionally, include application-specific context where the password is being used. For instance, if the application relates to animals, you can add relevant keywords (e.g., "dog," "cat," "pet") to the dictionary for more effective matching. Custom dictionaries can be as large as needed; for instance, the password dictionary alone can reach nearly 400 KB in size. | ||
|
||
## Lazy Loading | ||
Since language dictionaries can be large, they should only be loaded when necessary—specifically, when a user navigates to a page with a password input field. Use lazy loading techniques to minimize unnecessary data usage and improve performance. | ||
|
||
## Example Implementation | ||
When following all the best practices, your implementation might look something like this: | ||
|
||
```typescript | ||
import { ZxcvbnFactory } from '@zxcvbn-ts/core' | ||
import { matcherPwnedFactory } from '@zxcvbn-ts/matcher-pwned' | ||
|
||
const loadOptions = async () => { | ||
// common has some dictionaries which are not bound the any real language like a password list | ||
const zxcvbnCommonPackage = await import( | ||
/* webpackChunkName: "zxcvbnCommonPackage" */ '@zxcvbn-ts/language-common' | ||
) | ||
// As english is the language of the world it should always be included | ||
const zxcvbnEnPackage = await import( | ||
/* webpackChunkName: "zxcvbnEnPackage" */ '@zxcvbn-ts/language-en' | ||
) | ||
// The primary language of your website should align with the language predominantly used by your user base. | ||
const zxcvbnDePackage = await import( | ||
/* webpackChunkName: "zxcvbnDePackage" */ '@zxcvbn-ts/language-de' | ||
) | ||
|
||
return { | ||
translations: zxcvbnePackage.translations, | ||
graphs: zxcvbnCommonPackage.adjacencyGraphs, | ||
dictionary: { | ||
...zxcvbnCommonPackage.dictionary, | ||
...zxcvbnEnPackage.dictionary, | ||
...zxcvbnDePackage.dictionary | ||
// Enhance the dictionary search by adding custom-defined keywords to a personalized dictionary, enabling seamless and consistent searches for application-wide terms. | ||
userInputs: ['MrWook', 'zxcvbn-ts', 'vuepress', 'npm', 'github', 'typescript'] | ||
}, | ||
useLevenshtein: true, | ||
} | ||
} | ||
|
||
const customMatcher = { | ||
pwned: matcherPwnedFactory(fetch) | ||
} | ||
const options = await loadOptions() | ||
const zxcvbn = new ZxcvbnFactory(options, customMatcher) | ||
|
||
zxcvbn.checkAsync('thePasswordInUse', ['username', 'user email', 'other dynamic user content']) | ||
``` | ||
|
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,40 @@ | ||
# Filtering custom words | ||
Zxcvbn-ts comes with pre-built dictionaries for various languages. However, it’s often beneficial to extend these dictionaries with custom entries specific to your application. Custom dictionaries can help improve password strength by identifying unique terms relevant to your user base. | ||
|
||
The dictionary is provided as a Record<string, string[]>, which allows you to add key-value pairs, where the key is a custom label, and the value is an array of strings. You can extend this object as needed. | ||
|
||
## UserInput | ||
|
||
In many cases, you may want to check if a password matches any user-specific content, such as their username or email address. For this purpose, you can add a userInputs dictionary along with its own sanitizer. | ||
|
||
```js | ||
import { ZxcvbnFactory } from '@zxcvbn-ts/core' | ||
|
||
const password = 'somePassword' | ||
const options = { | ||
dictionary: { | ||
userInputs: ['[email protected]', 'someUsername'], | ||
}, | ||
} | ||
const zxcvbn = new ZxcvbnFactory(options) | ||
zxcvbn.check(password) | ||
``` | ||
|
||
If you need to dynamically add user inputs (e.g., during user registration when they enter their name, email, etc.), you can pass the user inputs as the second argument to the check method. | ||
```js | ||
import { ZxcvbnFactory } from '@zxcvbn-ts/core' | ||
|
||
const password = 'somePassword' | ||
|
||
const zxcvbn = new ZxcvbnFactory() | ||
zxcvbn.check(password, ['[email protected]', 'someUsername']) | ||
``` | ||
This is especially useful during registration when the user provides personal information that should not be included in their password. | ||
|
||
## Handling Duplicate Entries in Dictionaries | ||
If you have large dictionaries and are unsure whether some entries overlap, don't worry. The algorithm will automatically prioritize the first match it finds for any word present in multiple dictionaries, ensuring efficient processing. | ||
|
||
## Performance of large dictionaries | ||
The recommended dictionaries in Zxcvbn-ts already contain over 100,000 words, and even with these large dictionaries, the algorithm performs efficiently, processing results in under 100ms. | ||
|
||
While using large dictionaries can seem like it may slow down the process, the library is optimized to handle these dictionaries quickly, making it suitable for real-world applications without compromising performance. |
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 was deleted.
Oops, something went wrong.