Reflected changes in requirements and added logging
This commit is contained in:
parent
3cac439056
commit
64453aa176
@ -1,13 +1,14 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class User {
|
export class 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> {
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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";
|
||||||
|
|
||||||
@ -18,8 +20,8 @@ class UserDB {
|
|||||||
this.username = username
|
this.username = username
|
||||||
this.password_hash = password_hash
|
this.password_hash = password_hash
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UserDBBroker implements IUserBroker {
|
export class UserDBBroker implements IUserBroker {
|
||||||
|
|
||||||
private static initialized = false
|
private static initialized = false
|
||||||
@ -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,20 +88,20 @@ 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
|
||||||
throw new Error("The specified user does not exist on the database")
|
throw new Error("The specified user does not exist on the database")
|
||||||
}
|
}
|
||||||
|
|
||||||
let match = false
|
let match = false
|
||||||
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
@ -114,7 +118,7 @@ export class UserDBBroker implements IUserBroker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async updatePassword(username: string, password: string, newPassword: string): Promise<void> {
|
public async updatePassword(username: string, password: string, newPassword: string): Promise<void> {
|
||||||
|
|
||||||
const userToUpdate = await this.getUser(username, password)
|
const userToUpdate = await this.getUser(username, password)
|
||||||
|
|
||||||
if (!userToUpdate) {
|
if (!userToUpdate) {
|
||||||
@ -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
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user