-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
const sha1 = require('sha1'); | ||
|
||
/** | ||
* Check given password agains pwnedpasswords API. | ||
* @param password | ||
* @returns | ||
* @link https://haveibeenpwned.com/API/v2#SearchingPwnedPasswordsByRange | ||
*/ | ||
export const isPasswordPwned = (password) => { | ||
|
||
// Send the first 5 digits of the hash to API | ||
const hash = sha1(password); | ||
const prefix = hash.substring(0,5); // First 5 characters of SHA-1 hash | ||
const suffix = hash.substring(5); // Remaining characters of SHA-1 hash | ||
|
||
return fetch('https://api.pwnedpasswords.com/range/' + prefix) | ||
.then(response => response.text()) // Response is plain text - 1 result per line | ||
.then(response => response.split(/\r?\n/)) // Make an array of results | ||
.then(lines => lines.map(line => line.split(':')[0].toLowerCase())) // Hash suffix is string up to colon | ||
.then(lines => lines.includes(suffix.toLowerCase())); | ||
}; |
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,42 @@ | ||
import {isPasswordPwned} from "./Pwnedpasswords.api"; | ||
|
||
describe('isPasswordPwned', () => { | ||
|
||
const password = 'password'; | ||
const uniquePassword = 'aPasswordNotInThePwnedList'; | ||
|
||
/** | ||
* First 5 characters of SHA-1 hash of password | ||
* @type {string} | ||
*/ | ||
const expectedHashPrefix = '5baa6'; | ||
|
||
/** | ||
* Partial response from API | ||
* https://api.pwnedpasswords.com/range/5baa6 | ||
* @type {string} | ||
*/ | ||
const apiResponse = '1E2AAA439972480CEC7F16C795BBB429372:1\n' + | ||
'1E3687A61BFCE35F69B7408158101C8E414:1\n' + | ||
'1E4C9B93F3F0682250B6CF8331B7EE68FD8:3533661'; // This hash corresponds to 'password' | ||
|
||
beforeEach(() => { | ||
fetch.resetMocks(); | ||
fetch.mockResponseOnce(apiResponse); | ||
}); | ||
|
||
it('requests pwned passwords by partial hash', () => { | ||
isPasswordPwned(password); | ||
expect(fetch.mock.calls[0][0]).toBe('https://api.pwnedpasswords.com/range/' + expectedHashPrefix); | ||
}); | ||
|
||
it('returns true when password is pwned', async () => { | ||
const result = await isPasswordPwned(password); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when password is not pwned', async () => { | ||
const result = await isPasswordPwned(uniquePassword); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { configure } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
|
||
configure({ adapter: new Adapter() }); | ||
configure({ adapter: new Adapter() }); | ||
|
||
global.fetch = require('jest-fetch-mock') |