Skip to content

Commit

Permalink
fix: passwords are correctly checked (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Oct 20, 2022
1 parent 8d12376 commit 5c258ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/app/core/session/session-service/local-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export class LocalSession extends SessionService {
* @param password Password
*/
public async login(username: string, password: string): Promise<LoginState> {
const user: LocalUser = JSON.parse(
window.localStorage.getItem(username.trim().toLowerCase())
);
const user = this.getStoredUser(username);
if (user) {
if (passwordEqualsEncrypted(password, user.encryptedPassword)) {
await this.handleSuccessfulLogin(user);
Expand All @@ -65,6 +63,11 @@ export class LocalSession extends SessionService {
return this.loginState.value;
}

private getStoredUser(username: string): LocalUser {
const stored = window.localStorage.getItem(username.trim().toLowerCase());
return JSON.parse(stored);
}

public async handleSuccessfulLogin(userObject: DatabaseUser) {
this.currentDBUser = userObject;
await this.initializeDatabaseForCurrentUser();
Expand Down Expand Up @@ -142,7 +145,7 @@ export class LocalSession extends SessionService {
}

public checkPassword(username: string, password: string): boolean {
const user: LocalUser = JSON.parse(window.localStorage.getItem(username));
const user = this.getStoredUser(username);
return user && passwordEqualsEncrypted(password, user.encryptedPassword);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ describe("SyncedSessionService", () => {
localStorage.removeItem("[email protected]");
});

it("should correctly check the password", () => {
localSession.saveUser({ name: "TestUser", roles: [] }, TEST_PASSWORD);

expect(sessionService.checkPassword("TestUser", TEST_PASSWORD)).toBeTrue();
expect(sessionService.checkPassword("TestUser", "wrongPW")).toBeFalse();
});

testSessionServiceImplementation(() => Promise.resolve(sessionService));

function passRemoteLogin(response: DatabaseUser = { name: "", roles: [] }) {
Expand Down

0 comments on commit 5c258ae

Please sign in to comment.