added types for crypto

This commit is contained in:
Joel Biddle
2023-08-22 17:26:00 +10:00
parent 20f34b4764
commit e855d4a0ba
3 changed files with 6 additions and 13 deletions
+1 -1
View File
@@ -102,7 +102,7 @@
"@storybook/testing-library": "^0.2.0", "@storybook/testing-library": "^0.2.0",
"@tailwindcss/typography": "^0.5.4", "@tailwindcss/typography": "^0.5.4",
"@types/jsrp": "^0.2.4", "@types/jsrp": "^0.2.4",
"@types/node": "18.11.9", "@types/node": "^18.11.9",
"@types/react": "^18.0.26", "@types/react": "^18.0.26",
"@types/sanitize-html": "^2.9.0", "@types/sanitize-html": "^2.9.0",
"@typescript-eslint/eslint-plugin": "^5.48.1", "@typescript-eslint/eslint-plugin": "^5.48.1",
+1 -1
View File
@@ -110,7 +110,7 @@
"@storybook/testing-library": "^0.2.0", "@storybook/testing-library": "^0.2.0",
"@tailwindcss/typography": "^0.5.4", "@tailwindcss/typography": "^0.5.4",
"@types/jsrp": "^0.2.4", "@types/jsrp": "^0.2.4",
"@types/node": "18.11.9", "@types/node": "^18.11.9",
"@types/react": "^18.0.26", "@types/react": "^18.0.26",
"@types/sanitize-html": "^2.9.0", "@types/sanitize-html": "^2.9.0",
"@typescript-eslint/eslint-plugin": "^5.48.1", "@typescript-eslint/eslint-plugin": "^5.48.1",
@@ -1,12 +1,11 @@
import axios from "axios"; import axios from "axios";
import crypto from "crypto"; import crypto from "crypto"; // added types from @types/node
///// REMINDER: ensure all logs are deleted!!! ///// ///// REMINDER: ensure all logs are deleted!!! /////
export const checkIsPasswordBreached = async (password: string) => { export const checkIsPasswordBreached = async (password: string) => {
const dataBreachCheckAPIBaseURL = "https://api.pwnedpasswords.com/range/"; const dataBreachCheckAPIBaseURL = "https://api.pwnedpasswords.com/range/";
try { try {
console.log("password:", password); // delete later!!! console.log("password:", password); // delete later!!!
const textEncoder = new TextEncoder(); const textEncoder = new TextEncoder();
@@ -14,7 +13,7 @@ export const checkIsPasswordBreached = async (password: string) => {
const encodedPwd = textEncoder.encode(password); const encodedPwd = textEncoder.encode(password);
console.log("encodedPwd:", encodedPwd); // delete later!!! console.log("encodedPwd:", encodedPwd); // delete later!!!
const hashBuffer = crypto.subtle.digest("SHA-1", encodedPwd); // removed async const hashBuffer = await crypto.subtle.digest("SHA-1", encodedPwd); // returns promise
console.log("hashBuffer:", hashBuffer); // delete later!!! console.log("hashBuffer:", hashBuffer); // delete later!!!
const hashedPwd = Array.from(new Uint8Array(hashBuffer)) const hashedPwd = Array.from(new Uint8Array(hashBuffer))
@@ -24,9 +23,7 @@ export const checkIsPasswordBreached = async (password: string) => {
console.log("hashedPwd:", hashedPwd); // delete later!!! console.log("hashedPwd:", hashedPwd); // delete later!!!
const response = await axios.get( const response = await axios.get(`${dataBreachCheckAPIBaseURL}${hashedPwd.slice(0, 5)}`);
`${dataBreachCheckAPIBaseURL}${hashedPwd.slice(0, 5)}`
);
console.log("response:", response); // delete later!!! console.log("response:", response); // delete later!!!
const responseData = response.data.toUpperCase(); const responseData = response.data.toUpperCase();
@@ -40,11 +37,7 @@ export const checkIsPasswordBreached = async (password: string) => {
return isBreachedPassword; return isBreachedPassword;
} catch (err: any) { } catch (err: any) {
if ( if (axios.isAxiosError(err) && err.response && err.response.status === 429) {
axios.isAxiosError(err) &&
err.response &&
err.response.status === 429
) {
console.error("Received a 429 response from the Pwnd Passwords API"); console.error("Received a 429 response from the Pwnd Passwords API");
// Handle the 429 error here // Handle the 429 error here
} else { } else {