Reflected changes in requirements and added logging

This commit is contained in:
Christian Risi 2025-06-30 11:56:09 +00:00
parent 3cac439056
commit 64453aa176
3 changed files with 71 additions and 19 deletions

View File

@ -1,12 +1,13 @@
import type { Session } from "./sessions"
export interface IUserBroker { export interface IUserBroker {
createTable(): void createTable(): void
createUser(username: string, password: string): Promise<User> createUser(username: string, password: string): Promise<User>
getUser(username: string, password: string): Promise<User|null> getUser(username: string, password: string): Promise<User|null>
updatePassword(username: string, password: string, newPassword: string): Promise<void> updatePassword(username: string, password: string, newPassword: string): Promise<void>
getUserFromSession(sessionID: number): User getUserFromSession(session: Session): User
} }
@ -45,9 +46,10 @@ export class UserApp {
} }
public static getUserFromSession(sessionID: number): User {
public static getUserFromSession(session: Session): User {
UserApp.assertInitialized() UserApp.assertInitialized()
return UserApp.broker.getUserFromSession(sessionID) return UserApp.broker.getUserFromSession(session)
} }
public static async createUser(username: string, password: string): Promise<User> { public static async createUser(username: string, password: string): Promise<User> {

View File

@ -11,6 +11,10 @@ SELECT user_id, username, password_hash
FROM users FROM users
WHERE username = @username; WHERE username = @username;
SELECT user_id, username, password_hash
FROM users
WHERE user_id = @user_id;
UPDATE users UPDATE users
SET password_hash = @newPassword SET password_hash = @newPassword
WHERE username = @username; WHERE username = @username;

View File

@ -1,4 +1,6 @@
import type { Session, SessionApp } from "$lib/classes/sessions";
import { User, type IUserBroker } from "$lib/classes/users"; import { User, type IUserBroker } from "$lib/classes/users";
import { logger } from "$lib/utils/logger";
import { SSLSnifferApp } from "./sqlite"; import { SSLSnifferApp } from "./sqlite";
import * as argon2 from "argon2"; import * as argon2 from "argon2";
@ -29,6 +31,8 @@ export class UserDBBroker implements IUserBroker {
// UGLY: make more specific // UGLY: make more specific
throw Error("UserDB has been already initialized") throw Error("UserDB has been already initialized")
} }
logger.debug("Correctly initialized", "UserDBBroker")
} }
@ -84,9 +88,9 @@ export class UserDBBroker implements IUserBroker {
return user return user
} }
public async getUser(username: string, password: string): Promise<User|null> { public async getUser(username: string, password: string): Promise<User | null> {
const userToVerify = this.getUserByUsername(username) const userToVerify = this.getUserFromUsername(username)
if (!userToVerify) { if (!userToVerify) {
// UGLY: make this more specific // UGLY: make this more specific
@ -97,7 +101,7 @@ export class UserDBBroker implements IUserBroker {
try { try {
match = await argon2.verify(userToVerify.password_hash, password) match = await argon2.verify(userToVerify.password_hash, password)
} catch(error) { } catch (error) {
// UGLY: make this more specific // UGLY: make this more specific
throw new Error("Argon2 had an error") throw new Error("Argon2 had an error")
} }
@ -138,14 +142,28 @@ export class UserDBBroker implements IUserBroker {
stmt.finalize() stmt.finalize()
} }
// TODO: implement this
public getUserFromSession(sessionID: number): User { public getUserFromSession(session: Session): User {
throw new Error("Method not implemented.");
const userDB = this.getUserFromUserID(session.userID)
if (!userDB) {
// UGLY: be specific
throw new Error("Could not find user inside database")
}
return new User(
userDB.user_id,
userDB.username
)
} }
private validateUniqueness(username: string) { private validateUniqueness(username: string) {
const user = this.getUserByUsername(username) const user = this.getUserFromUsername(username)
if (!user) { if (!user) {
return return
@ -154,7 +172,7 @@ export class UserDBBroker implements IUserBroker {
throw new Error("User is already on the system") throw new Error("User is already on the system")
} }
private getUserByUsername(username: string): UserDB | null { private getUserFromUsername(username: string): UserDB | null {
const stmt = SSLSnifferApp.prepare( const stmt = SSLSnifferApp.prepare(
` `
SELECT user_id, username, password_hash SELECT user_id, username, password_hash
@ -163,7 +181,7 @@ export class UserDBBroker implements IUserBroker {
` `
) )
const user : any | null = stmt.get({ const user: any | null = stmt.get({
username: username, username: username,
}) })
@ -181,4 +199,32 @@ export class UserDBBroker implements IUserBroker {
} }
private getUserFromUserID(userID: number): UserDB | null {
const stmt = SSLSnifferApp.prepare(
`
SELECT user_id, username, password_hash
FROM users
WHERE user_id = @user_id;
`
)
const user: any | null = stmt.get({
user_id: userID,
})
stmt.finalize()
if (!user) {
return null
}
return new UserDB(
user.user_id,
user.username,
user.password_hash
)
}
} }