diff --git a/src/lib/classes/sessions.ts b/src/lib/classes/sessions.ts index a774793..cc1b8dd 100644 --- a/src/lib/classes/sessions.ts +++ b/src/lib/classes/sessions.ts @@ -2,8 +2,8 @@ export interface ISessionBroker { createTable(): void createSessionFromUserID(userID: number): Session - getSessionFromUserID(userID: number) : Session - getSessionFromToken(token: string) : Session + getSessionFromUserID(userID: number) : Session|null + getSessionFromToken(token: string) : Session|null } diff --git a/src/lib/db-utils/SQL/Sessions.sql b/src/lib/db-utils/SQL/Sessions.sql index 0696f90..f97ba08 100644 --- a/src/lib/db-utils/SQL/Sessions.sql +++ b/src/lib/db-utils/SQL/Sessions.sql @@ -12,8 +12,14 @@ SELECT session_id, user_id, session_token, FROM sessions WHERE session_token = @token; +SELECT session_id, user_id, session_token, +FROM sessions +WHERE session_id = @userID; + SELECT session_id, user_id, session_token, FROM sessions WHERE session_id = @sessionID; + + diff --git a/src/lib/db-utils/Sessions.ts b/src/lib/db-utils/Sessions.ts index 57369d5..4d77e54 100644 --- a/src/lib/db-utils/Sessions.ts +++ b/src/lib/db-utils/Sessions.ts @@ -1,4 +1,4 @@ -import type { ISessionBroker, Session } from "$lib/classes/sessions" +import { Session, type ISessionBroker } from "$lib/classes/sessions" import { SSLSnifferApp } from "./sqlite" class SessionDB { @@ -14,16 +14,16 @@ class SessionDB { constructor( session_id: number, user_id: number, - session_string: string + session_token: string ) { this.session_id = session_id this.user_id = user_id - this.session_token = session_string + this.session_token = session_token } } -export class SessionDBBroker implements ISessionBroker{ +export class SessionDBBroker implements ISessionBroker { private static initialized = false @@ -33,8 +33,8 @@ export class SessionDBBroker implements ISessionBroker{ throw Error("SessionBroker has already been initialized") } } - - + + createTable(): void { const stmt = SSLSnifferApp.prepare( @@ -50,21 +50,149 @@ export class SessionDBBroker implements ISessionBroker{ stmt.run() stmt.finalize() + } - getSessionFromToken(token: string): Session { - throw new Error("Method not implemented.") - } - createSessionFromUserID(userID: number): Session { - throw new Error("Method not implemented.") + + // Check for existing Sessions + const sessionCheck = this.getSessionFromUserID(userID) + + if (sessionCheck) { + // UGLY: more specific + throw new Error("There's already a session associated with the user") + } + + // Create new Session + const token : string = crypto.randomUUID(); + + // Insert into DB + const stmt = SSLSnifferApp.prepare( + ` + INSERT INTO sessions (user_id, session_token) + VALUES (@userID, @token); + ` + ) + + stmt.run({ + userID: userID, + token: token + }) + stmt.finalize() + + // Check if Session has been successfully created + const session = this.getSessionFromUserID(userID) + + if (!session) { + // UGLY: more specific + throw new Error("Something wrong happened during the creationg of the session") + } + + return session + } - getSessionFromUserID(userID: number): Session { - throw new Error("Method not implemented.") + getSessionFromUserID(userID: number): Session | null { + const candidateSession = this.getSessionDBFromUserID(userID) + + if (!candidateSession) { + return null + } + + return new Session( + candidateSession.session_id, + candidateSession.session_token + ) } + getSessionFromToken(token: string): Session | null { + const candidateSession = this.getSessionDBFromToken(token) + if (!candidateSession) { + return null + } + + return new Session( + candidateSession.session_id, + candidateSession.session_token + ) + + } + + private getSessionDBFromToken(token: string): SessionDB | null { + + const stmt = SSLSnifferApp.prepare( + ` + SELECT session_id, user_id, session_token, + FROM sessions + WHERE session_token = @token; + ` + ) + + const sessions = stmt.all({ + token: token + }) + stmt.finalize() + + return this.parseSessionDBUnique(sessions) + + } + + private getSessionDBFromUserID(userID: number): SessionDB | null { + const stmt = SSLSnifferApp.prepare( + ` + SELECT session_id, user_id, session_token, + FROM sessions + WHERE session_id = @userID; + ` + ) + + const sessions = stmt.all({ + userID: userID + }) + stmt.finalize() + + return this.parseSessionDBUnique(sessions) + } + + private getSessionDBFromSessionID(sessionID: number): SessionDB | null { + + const stmt = SSLSnifferApp.prepare( + ` + SELECT session_id, user_id, session_token, + FROM sessions + WHERE session_id = @sessionID; + ` + ) + + const sessions = stmt.all({ + sessionID: sessionID + }) + stmt.finalize() + + return this.parseSessionDBUnique(sessions) + } + + private parseSessionDBUnique(sessions: any[]) { + + if (sessions.length > 1) { + // UGLY: be specific + throw new Error("Duplicate session?") + } + + if (sessions.length < 1) { + return null + } + + const session: any = sessions[0] + + return new SessionDB( + session.session_id, + session.user_id, + session.session_token + ) + + } }