mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-23 16:32:54 +10:00
refactor: session handler
This commit is contained in:
@@ -81,8 +81,8 @@ class ACLManager {
|
||||
if (!request)
|
||||
throw new Error("Native web requests not available - weird deployment?");
|
||||
// Sessions automatically have all ACLs
|
||||
const userId = await sessionHandler.getUserId(request);
|
||||
if (userId) return userId;
|
||||
const user = await sessionHandler.getSession(request);
|
||||
if (user) return user.userId;
|
||||
|
||||
const authorizationToken = this.getAuthorizationToken(request);
|
||||
if (!authorizationToken) return undefined;
|
||||
@@ -116,9 +116,11 @@ class ACLManager {
|
||||
) {
|
||||
if (!request)
|
||||
throw new Error("Native web requests not available - weird deployment?");
|
||||
const userId = await sessionHandler.getUserId(request);
|
||||
if (userId) {
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
const userSession = await sessionHandler.getSession(request);
|
||||
if (userSession) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userSession.userId },
|
||||
});
|
||||
if (!user) return false;
|
||||
if (user.admin) return true;
|
||||
return false;
|
||||
|
||||
@@ -10,8 +10,8 @@ export default function createDBSessionHandler(): SessionProvider {
|
||||
});
|
||||
|
||||
return {
|
||||
async setSession(token, data) {
|
||||
cache.set(token, data);
|
||||
async setSession(token, session) {
|
||||
cache.set(token, session);
|
||||
|
||||
// const strData = JSON.stringify(data);
|
||||
await prisma.session.upsert({
|
||||
@@ -20,54 +20,16 @@ export default function createDBSessionHandler(): SessionProvider {
|
||||
},
|
||||
create: {
|
||||
token,
|
||||
data,
|
||||
},
|
||||
update: {
|
||||
data,
|
||||
...session,
|
||||
},
|
||||
update: session,
|
||||
});
|
||||
return true;
|
||||
},
|
||||
async updateSession(token, key, data) {
|
||||
const newObj: { [key: string]: any } = {};
|
||||
newObj[key] = data;
|
||||
cache.set(token, newObj);
|
||||
|
||||
const session = await prisma.session.upsert({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
create: {
|
||||
token,
|
||||
data: newObj,
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
// if json object and not arrary, update session
|
||||
if (
|
||||
typeof session.data === "object" &&
|
||||
!Array.isArray(session.data) &&
|
||||
session.data !== null
|
||||
) {
|
||||
// means we set it above
|
||||
if (session.data[key] === data) return true;
|
||||
|
||||
// else we need to set it ourselves
|
||||
(session.data as Prisma.JsonObject)[key] = data;
|
||||
await prisma.session.update({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
data: {
|
||||
data: session.data,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
async updateSession(token, data) {
|
||||
return await this.setSession(token, data);
|
||||
},
|
||||
async getSession<T>(token: string) {
|
||||
async getSession<T extends Session>(token: string) {
|
||||
const cached = cache.get(token);
|
||||
if (cached !== undefined) return cached as T;
|
||||
|
||||
@@ -77,15 +39,31 @@ export default function createDBSessionHandler(): SessionProvider {
|
||||
},
|
||||
});
|
||||
if (result === null) return undefined;
|
||||
return result.data as T;
|
||||
|
||||
// i hate casting
|
||||
// need to cast to unknown since result.data can be an N deep json object technically
|
||||
// ts doesn't like that be cast down to the more constraining session type
|
||||
return result as unknown as T;
|
||||
},
|
||||
async clearSession(token) {
|
||||
async removeSession(token) {
|
||||
cache.delete(token);
|
||||
await prisma.session.delete({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
},
|
||||
async cleanupSessions() {
|
||||
const now = new Date();
|
||||
|
||||
await prisma.session.deleteMany({
|
||||
where: {
|
||||
expiresAt: {
|
||||
lt: now,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { H3Event, Session } from "h3";
|
||||
import { H3Event } from "h3";
|
||||
import createMemorySessionProvider from "./memory";
|
||||
import { SessionProvider } from "./types";
|
||||
import prisma from "../db/database";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { Session, SessionProvider } from "./types";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import moment from "moment";
|
||||
import { parse as parseCookies } from "cookie-es";
|
||||
import { MinimumRequestObject } from "~/server/h3";
|
||||
@@ -14,9 +13,7 @@ This implementation may need work.
|
||||
It exposes an API that should stay static, but there are plenty of opportunities for optimisation/organisation under the hood
|
||||
*/
|
||||
|
||||
const userSessionKey = "_userSession";
|
||||
const userIdKey = "_userId";
|
||||
const dropTokenCookie = "drop-token";
|
||||
const dropTokenCookieName = "drop-token";
|
||||
const normalSessionLength = [31, "days"];
|
||||
const extendedSessionLength = [1, "year"];
|
||||
|
||||
@@ -29,86 +26,94 @@ export class SessionHandler {
|
||||
// this.sessionProvider = createMemorySessionProvider();
|
||||
}
|
||||
|
||||
private getSessionToken(request: MinimumRequestObject | undefined) {
|
||||
async signin(h3: H3Event, userId: string, rememberMe: boolean = false) {
|
||||
const expiresAt = this.createExipreAt(rememberMe);
|
||||
const token = this.createSessionCookie(h3, expiresAt);
|
||||
return await this.sessionProvider.setSession(token, {
|
||||
userId,
|
||||
expiresAt,
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a session associated with a request
|
||||
* @returns session
|
||||
*/
|
||||
async getSession<T extends Session>(request: MinimumRequestObject) {
|
||||
const token = this.getSessionToken(request);
|
||||
if (!token) return undefined;
|
||||
// TODO: should validate if session is expired or not here, not in application code
|
||||
|
||||
const data = await this.sessionProvider.getSession<T>(token);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signout session associated with request and deauthenticates it
|
||||
* @param request
|
||||
* @returns
|
||||
*/
|
||||
async signout(h3: H3Event) {
|
||||
const token = this.getSessionToken(h3);
|
||||
if (!token) return false;
|
||||
const res = await this.sessionProvider.removeSession(token);
|
||||
if (!res) return false;
|
||||
deleteCookie(h3, dropTokenCookieName);
|
||||
return true;
|
||||
}
|
||||
|
||||
async cleanupSessions() {
|
||||
await this.sessionProvider.cleanupSessions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update session info
|
||||
* @param token session token
|
||||
* @param data new session data
|
||||
* @returns success or not
|
||||
*/
|
||||
private async updateSession(token: string, data: Session) {
|
||||
return await this.sessionProvider.updateSession(token, data);
|
||||
}
|
||||
|
||||
// ---------------------- Private API Below ------------------------
|
||||
|
||||
/**
|
||||
* Get session token on a request
|
||||
* @param request
|
||||
* @returns session token
|
||||
*/
|
||||
private getSessionToken(
|
||||
request: MinimumRequestObject | undefined
|
||||
): string | undefined {
|
||||
if (!request) throw new Error("Native web request not available");
|
||||
const cookieHeader = request.headers.get("Cookie");
|
||||
if (!cookieHeader) return undefined;
|
||||
const cookies = parseCookies(cookieHeader);
|
||||
const cookie = cookies[dropTokenCookie];
|
||||
const cookie = cookies[dropTokenCookieName];
|
||||
return cookie;
|
||||
}
|
||||
|
||||
private async createSession(h3: H3Event, extend = false) {
|
||||
const token = uuidv4();
|
||||
const expiry = moment().add(
|
||||
...(extend ? extendedSessionLength : normalSessionLength)
|
||||
);
|
||||
|
||||
setCookie(h3, dropTokenCookie, token, { expires: expiry.toDate() });
|
||||
|
||||
this.sessionProvider.setSession(dropTokenCookie, {});
|
||||
private createExipreAt(rememberMe: boolean) {
|
||||
return moment()
|
||||
.add(...(rememberMe ? extendedSessionLength : normalSessionLength))
|
||||
.toDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates cookie that represents user session
|
||||
* @param h3
|
||||
* @param extend
|
||||
* @returns
|
||||
*/
|
||||
private createSessionCookie(h3: H3Event, expiresAt: Date) {
|
||||
const token = randomUUID();
|
||||
// TODO: we should probably switch to jwts to minimize possibility of someone
|
||||
// trying to guess a session id (jwts let us sign + encrypt stuff in a std way)
|
||||
setCookie(h3, dropTokenCookieName, token, { expires: expiresAt });
|
||||
return token;
|
||||
}
|
||||
|
||||
getDropTokenCookie() {
|
||||
return dropTokenCookie;
|
||||
}
|
||||
|
||||
async getSession<T extends Session>(request: MinimumRequestObject) {
|
||||
const token = this.getSessionToken(request);
|
||||
if (!token) return undefined;
|
||||
const data = await this.sessionProvider.getSession<{ [userSessionKey]: T }>(
|
||||
token
|
||||
);
|
||||
if (!data) return undefined;
|
||||
|
||||
return data[userSessionKey];
|
||||
}
|
||||
async setSession(h3: H3Event, data: any, extend = false) {
|
||||
const token =
|
||||
this.getSessionToken(h3) ?? (await this.createSession(h3, extend));
|
||||
const result = await this.sessionProvider.updateSession(
|
||||
token,
|
||||
userSessionKey,
|
||||
data
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
async clearSession(request: MinimumRequestObject) {
|
||||
const token = this.getSessionToken(request);
|
||||
if (!token) return false;
|
||||
await this.sessionProvider.clearSession(token);
|
||||
return true;
|
||||
}
|
||||
|
||||
async getUserId(h3: MinimumRequestObject) {
|
||||
const token = this.getSessionToken(h3);
|
||||
if (!token) return undefined;
|
||||
|
||||
return await this.getUserIdRaw(token);
|
||||
}
|
||||
async getUserIdRaw(token: string) {
|
||||
const session = await this.sessionProvider.getSession<{
|
||||
[userIdKey]: string | undefined;
|
||||
}>(token);
|
||||
|
||||
if (!session) return undefined;
|
||||
|
||||
return session[userIdKey];
|
||||
}
|
||||
|
||||
async setUserId(h3: H3Event, userId: string, extend = false) {
|
||||
const token =
|
||||
this.getSessionToken(h3) ?? (await this.createSession(h3, extend));
|
||||
|
||||
const result = await this.sessionProvider.updateSession(
|
||||
token,
|
||||
userIdKey,
|
||||
userId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const sessionHandler = new SessionHandler();
|
||||
|
||||
@@ -8,15 +8,23 @@ export default function createMemorySessionHandler() {
|
||||
sessions[token] = data;
|
||||
return true;
|
||||
},
|
||||
async updateSession(token, key, data) {
|
||||
sessions[token] = Object.assign({}, sessions[token], { [key]: data });
|
||||
async getSession<T extends Session>(token: string): Promise<T | undefined> {
|
||||
const session = sessions[token];
|
||||
return session ? (session as T) : undefined; // Ensure undefined is returned if session is not found
|
||||
},
|
||||
async updateSession(token, data) {
|
||||
return this.setSession(token, data);
|
||||
},
|
||||
async removeSession(token) {
|
||||
delete sessions[token];
|
||||
return true;
|
||||
},
|
||||
async getSession(token) {
|
||||
return sessions[token] as any; // Wild type cast because we let the user specify types if they want
|
||||
},
|
||||
async clearSession(token) {
|
||||
delete sessions[token];
|
||||
async cleanupSessions() {
|
||||
const now = new Date();
|
||||
for (let token in sessions) {
|
||||
// if expires at time is before now, the session is expired
|
||||
if (sessions[token].expiresAt < now) await this.removeSession(token);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Vendored
+11
-4
@@ -1,10 +1,17 @@
|
||||
import { H3Event } from "h3";
|
||||
|
||||
export type Session = { [key: string]: any };
|
||||
export type Session = {
|
||||
userId: string;
|
||||
expiresAt: Date;
|
||||
data: {
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
|
||||
export interface SessionProvider {
|
||||
setSession: (token: string, data: Session) => Promise<boolean>;
|
||||
updateSession: (token: string, key: string, data: any) => Promise<boolean>;
|
||||
getSession: <T extends Session>(token: string) => Promise<T | undefined>;
|
||||
clearSession: (token: string) => Promise<void>;
|
||||
setSession: (token: string, data: Session) => Promise<boolean>;
|
||||
updateSession: (token: string, data: Session) => Promise<boolean>;
|
||||
removeSession: (token: string) => Promise<boolean>;
|
||||
cleanupSessions: () => Promise<void>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user