Added Support for Sessions
This commit is contained in:
parent
6532a1988d
commit
db769f4f96
23
src/lib/classes/sessions.ts
Normal file
23
src/lib/classes/sessions.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
export interface ISessionBroker {
|
||||||
|
|
||||||
|
createTable(): void
|
||||||
|
createSessionFromUserID(userID: number): Session
|
||||||
|
getSessionFromUserID(userID: number) : Session
|
||||||
|
getSessionFromToken(token: string) : Session
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Session {
|
||||||
|
|
||||||
|
public sessionID: number
|
||||||
|
public sessionToken: string
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
sessionID: number,
|
||||||
|
sessionToken: string
|
||||||
|
) {
|
||||||
|
this.sessionID = sessionID
|
||||||
|
this.sessionToken = sessionToken
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
src/lib/db-utils/SQL/Sessions.sql
Normal file
19
src/lib/db-utils/SQL/Sessions.sql
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
user_id INTEGER UNIQUE NOT NULL
|
||||||
|
session_token TEXT UNIQUE NOT NULL ,
|
||||||
|
FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO sessions (user_id, session_token)
|
||||||
|
VALUES (@userID, @token);
|
||||||
|
|
||||||
|
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 = @sessionID;
|
||||||
|
|
||||||
|
|
||||||
71
src/lib/db-utils/Sessions.ts
Normal file
71
src/lib/db-utils/Sessions.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import type { ISessionBroker, Session } from "$lib/classes/sessions"
|
||||||
|
import { SSLSnifferApp } from "./sqlite"
|
||||||
|
|
||||||
|
class SessionDB {
|
||||||
|
|
||||||
|
public session_id: number
|
||||||
|
public user_id: number
|
||||||
|
public session_token: string
|
||||||
|
|
||||||
|
// TODO: support fingerprinting to
|
||||||
|
// TODO: increase security
|
||||||
|
// TODO: while logging in
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
session_id: number,
|
||||||
|
user_id: number,
|
||||||
|
session_string: string
|
||||||
|
) {
|
||||||
|
this.session_id = session_id
|
||||||
|
this.user_id = user_id
|
||||||
|
this.session_token = session_string
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SessionDBBroker implements ISessionBroker{
|
||||||
|
|
||||||
|
private static initialized = false
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
if (SessionDBBroker.initialized) {
|
||||||
|
// UGLY: make more specific
|
||||||
|
throw Error("SessionBroker has already been initialized")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
createTable(): void {
|
||||||
|
|
||||||
|
const stmt = SSLSnifferApp.prepare(
|
||||||
|
`
|
||||||
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
user_id INTEGER UNIQUE NOT NULL
|
||||||
|
session_token TEXT UNIQUE NOT NULL ,
|
||||||
|
FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
stmt.run()
|
||||||
|
stmt.finalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
getSessionFromToken(token: string): Session {
|
||||||
|
throw new Error("Method not implemented.")
|
||||||
|
}
|
||||||
|
|
||||||
|
createSessionFromUserID(userID: number): Session {
|
||||||
|
throw new Error("Method not implemented.")
|
||||||
|
}
|
||||||
|
|
||||||
|
getSessionFromUserID(userID: number): Session {
|
||||||
|
throw new Error("Method not implemented.")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user