diff --git a/src/lib/classes/sessions.ts b/src/lib/classes/sessions.ts index cc1b8dd..8b90cc9 100644 --- a/src/lib/classes/sessions.ts +++ b/src/lib/classes/sessions.ts @@ -2,22 +2,69 @@ export interface ISessionBroker { createTable(): void createSessionFromUserID(userID: number): Session - getSessionFromUserID(userID: number) : Session|null - getSessionFromToken(token: string) : Session|null + getSessionFromUserID(userID: number): Session | null + getSessionFromToken(token: string): Session | null } export class Session { public sessionID: number + public userID: number public sessionToken: string constructor( sessionID: number, + userID: number, sessionToken: string ) { this.sessionID = sessionID + this.userID = userID this.sessionToken = sessionToken } -} \ No newline at end of file +} + +export class SessionApp { + + private static broker: ISessionBroker + private static initialized: boolean = false + + public static init(broker: ISessionBroker) { + if (SessionApp.initialized) { + // UGLY: make this Error more specific + throw Error("SessionApp has already been initialized") + } + + SessionApp.initialized = true + SessionApp.broker = broker + SessionApp.broker.createTable() + + } + + public static createSessionFromUserID(userID: number): Session { + SessionApp.assertInitialized() + return SessionApp.broker.createSessionFromUserID(userID) + } + + public static getSessionFromUserID(userID: number): Session | null { + SessionApp.assertInitialized() + return SessionApp.broker.getSessionFromUserID(userID) + } + + public static getSessionFromToken(token: string): Session | null { + SessionApp.assertInitialized() + return SessionApp.broker.getSessionFromToken(token) + } + + private static assertInitialized() { + + if (!SessionApp.initialized) { + // UGLY: make more specific + throw Error("SessionApp has't been Initialized yet!") + } + + } + + +} diff --git a/src/lib/db-utils/SQL/Sessions.sql b/src/lib/db-utils/SQL/Sessions.sql index f97ba08..87ff710 100644 --- a/src/lib/db-utils/SQL/Sessions.sql +++ b/src/lib/db-utils/SQL/Sessions.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS sessions ( session_id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER UNIQUE NOT NULL - session_token TEXT UNIQUE NOT NULL , + user_id INTEGER UNIQUE NOT NULL, + session_token TEXT UNIQUE NOT NULL, FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE ); diff --git a/src/lib/db-utils/Sessions.ts b/src/lib/db-utils/Sessions.ts index 4d77e54..d42c33d 100644 --- a/src/lib/db-utils/Sessions.ts +++ b/src/lib/db-utils/Sessions.ts @@ -1,4 +1,5 @@ import { Session, type ISessionBroker } from "$lib/classes/sessions" +import { logger } from "$lib/utils/logger" import { SSLSnifferApp } from "./sqlite" class SessionDB { @@ -32,6 +33,8 @@ export class SessionDBBroker implements ISessionBroker { // UGLY: make more specific throw Error("SessionBroker has already been initialized") } + + logger.debug("Correctly initialized", "SessionDBBroker") } @@ -41,8 +44,8 @@ export class SessionDBBroker implements ISessionBroker { ` CREATE TABLE IF NOT EXISTS sessions ( session_id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER UNIQUE NOT NULL - session_token TEXT UNIQUE NOT NULL , + user_id INTEGER UNIQUE NOT NULL, + session_token TEXT UNIQUE NOT NULL, FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE ); ` @@ -101,6 +104,7 @@ export class SessionDBBroker implements ISessionBroker { return new Session( candidateSession.session_id, + candidateSession.user_id, candidateSession.session_token ) } @@ -114,6 +118,7 @@ export class SessionDBBroker implements ISessionBroker { return new Session( candidateSession.session_id, + candidateSession.user_id, candidateSession.session_token )