Update Session to reflect asjustments to structure
This commit is contained in:
parent
f13a44ba5a
commit
002fd58585
@ -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
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user