Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 9d0c940

Browse files
FF-404 add password preHashing with sha (we will hash again in the Backend) (#159)
* add password preHashin with sha (we will hash again in the Backedn) * add reference * [CodeFactor] Apply fixes to commit 61a4df8 * add a static salt before hashing * make hashed password HEX code uppercase Co-authored-by: codefactor-io <[email protected]>
1 parent 96a8f39 commit 9d0c940

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/background/api/auth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { AccessToken, CookieStatus } from "../redux/actions/tokenTypes";
1414
import { deleteCookie, getCookie, setCookie } from "../methods/cookies";
1515
import {updateUser} from "../redux/actions/user";
16+
import {hashPassword} from "../methods/passwords";
1617

1718
// reference: https://daveceddia.com/access-redux-store-outside-react/
1819

@@ -40,16 +41,17 @@ export const checkForCookie = () => {
4041
}
4142
};
4243

43-
export const loginWithUsernameAndPassword = (
44+
export const loginWithUsernameAndPassword = async (
4445
userName: string,
4546
password: string,
4647
stayLoggedIn: boolean
4748
): Promise<BackendLoginData> => {
48-
console.log("[Auth] loginWithUsernameAndPassword", userName, password);
49+
console.log("[Auth] loginWithUsernameAndPassword", userName);
50+
let hashed = await hashPassword(password);
4951
return new Promise<BackendLoginData>((resolve, reject) => {
5052
let config = {
5153
headers: {
52-
Authorization: `Basic ${btoa(userName + ":" + password)}`
54+
Authorization: `Basic ${btoa(userName + ":" + hashed)}`
5355
}
5456
};
5557

src/background/api/registration.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import Axios, {AxiosError, AxiosResponse} from "axios";
22
import {hostname, userPath} from "./api";
3+
import {hashPassword} from "../methods/passwords";
34

45
export interface IRegisterServerResponse {
56
httpStatus: number,
67
httpMessage: string
78
outputMessage?: string
89
}
910

10-
export const registerNewUser = (username: string, password: string, passwordConfirmation: string): Promise<IRegisterServerResponse> => {
11+
export const registerNewUser = async (username: string, password: string, passwordConfirmation: string): Promise<IRegisterServerResponse> => {
12+
13+
if (password !== passwordConfirmation){
14+
throw new Error("Password did not match passwordConfirmation");
15+
}
16+
let hashedPassword = await hashPassword(password);
1117

1218
return new Promise((resolve, reject) => {
1319
const newUser = {
1420
username: username,
15-
password: password,
16-
confirmationPassword: passwordConfirmation
21+
password: hashedPassword,
22+
confirmationPassword: hashedPassword
1723
}
1824

1925

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//see: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
2+
3+
async function hashPassword(password: string) {
4+
const msgUint8 = new TextEncoder().encode(password + "FileFighterWithSomeSalt"); // encode as (utf-8) Uint8Array
5+
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
6+
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
7+
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
8+
return hashHex.toUpperCase();
9+
}
10+
11+
export {hashPassword}
12+

src/components/pages/User/Profile.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {DEFAULT_ALERT_DURATION, MIN_PASSWORD_LENGTH} from "../../../background/c
77
import {changeUserInformation, UserInformation} from "../../../background/api/userInformation";
88
import {notMinStrLength} from "../../../background/methods/checkInput";
99
import edit_svg from "../../../assets/images/icons/material.io/edit_white_24dp.svg";
10+
import {hashPassword} from "../../../background/methods/passwords";
1011

1112

1213
export default function Profile(): ReactElement {
@@ -52,8 +53,8 @@ export default function Profile(): ReactElement {
5253
handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Please pay attention to the notes below the input fields.")
5354
return;
5455
}
55-
newUser["password"] = inputUser.password
56-
newUser["confirmationPassword"] = inputUser.passwordConfirmation
56+
newUser["password"] = await hashPassword(inputUser.password);
57+
newUser["confirmationPassword"] = newUser["password"];
5758
}
5859

5960
await changeUserInformation(newUser)

0 commit comments

Comments
 (0)