Skip to content

Commit 1d51dc9

Browse files
author
Will Hackett
committed
Initial commit
0 parents  commit 1d51dc9

File tree

11 files changed

+5790
-0
lines changed

11 files changed

+5790
-0
lines changed

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# next.js build output
61+
.next
62+
63+
# dist
64+
dist

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Will Hackett
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# is-pwned
2+
3+
##### Check if passwords are PWNED before they're used
4+
5+
## Purpose
6+
7+
[Credential stuffing](https://www.owasp.org/index.php/Credential_stuffing) is a very real threat to web application security and it's important that businesses are doing their best to protect their users from passwords that been been seen in known breaches.
8+
9+
This library is designed to be hooked into your _login_ or _change password_ forms to ensure that users aren't using or setting passwords seen in known breaches. This is achieved by sending the first five characters of a SHA-1 copy of the user's password to [Have I Been Pwned](https://haveibeenpwned.com/API/v2)'s API and matching it against the result set that is returned.
10+
11+
TL;DR: This adds breached password detection to input fields of your choice and has a configurable timeout so it doesn't block your login.
12+
13+
#### Note: This library is intended for browser use only. In future versions it might support Node but the intent is to keep the library free from dependencies.
14+
15+
## Installation
16+
17+
Install the `is-pwned` package using `npm` or `yarn`.
18+
19+
```bash
20+
npm i -S is-pwned
21+
```
22+
23+
```bash
24+
yarn add is-pwned
25+
```
26+
27+
### Import the Module & Configure
28+
29+
Import the module and instantiate the class. TypeScript types are available.
30+
31+
```typescript
32+
import IsPwned from 'is-pwned';
33+
34+
const pw = new IsPwned(config);
35+
```
36+
37+
## Methods
38+
39+
### Check Password (`pw.check()`)
40+
41+
pw.check returns a promise that resolves `true` or reject with an `Error`. See _Error Handling_ below.
42+
43+
```typescript
44+
const pw = new IsPwned(config);
45+
46+
pw.check('password'); // Promise<true>
47+
```
48+
49+
### Hash Password (`pw.hashPassword()`)
50+
51+
Exposes the internal password hashing method.
52+
53+
```typescript
54+
const pw = new IsPwned(config);
55+
56+
pw.hashPassword('password'); // '5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8'
57+
```
58+
59+
## Error Handling
60+
61+
Methods in the `is-pwned` library use custom errors so you can better handle responses.
62+
63+
```typescript
64+
try {
65+
await pw.check('password');
66+
} catch (e) {
67+
switch (e.name) {
68+
case 'UnexpectedHttpResponseError':
69+
// A response other than 200 was received from HIBP
70+
case 'TimedOutError':
71+
// The timeout was reached
72+
case 'InvalidPasswordError':
73+
// The password is either not a string or is empty
74+
case 'BreachedError':
75+
// The password has been breached
76+
// You can use e.count on this error type
77+
console.log(`Password has been breached ${e.count} times.`);
78+
}
79+
}
80+
```
81+
82+
## Configuration
83+
84+
IsPwned can be configured accordingly:
85+
86+
| Option | Type | Default | Purpose |
87+
| :----------------- | :------------------- | :-------------------------------------- | :---------------------------------------------- |
88+
| `endpoint` | `string` (optional) | `https://api.pwnedpasswords.com/range/` | Substitute the HIBP endpoit for your own. |
89+
| `timeout` | `number` (optional) | `5000` | Timeout on the check. |
90+
| `userAgent` | `string` (optional) | `is-pwned-js` | Change the UserAgent to represent your service. |
91+
| `resolveOnTimeout` | `boolean` (optional) | `false` | Resolve instead of erroring on timeout. |
92+
93+
### `endpoint`
94+
95+
In some environments a business may choose to either proxy or host their own HIBP endpoint. The script will pass the first five characters of the SHA-1'd password to the last url part — for example: `https://api.pwnedpasswords.com/range/{hash}`
96+
97+
### `timeout`
98+
99+
To ensure that the user experience isn't adversely affected by the additional HTTP request it is recommended to set a timeout. The default timeout is `5000` milliseconds, but you may want to change this depending on your situation.
100+
101+
### `userAgent`
102+
103+
The [HIBP API Acceptable Use Policy](https://haveibeenpwned.com/API/v2#AcceptableUse) requires that user agent "accurately describe the nature of the API consumer such that it can be clearly identified in the request". It is recommended that you change this, however it will default to `is-pwned-js`.
104+
105+
Please also refer to the licensing requirements for using the [Passwords API](https://haveibeenpwned.com/API/v2#License). Accreditation isn't required but it is welcomed by HIBP.
106+
107+
### `resolveOnTimeout`
108+
109+
Setting this to `true` will resolve the `check` method promise if it times out which can assist in some programming situations. This is not recommended as you should try and handle this in your codebase.

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
setupFiles: [
5+
'./test/setupJest.ts'
6+
]
7+
};

0 commit comments

Comments
 (0)