Files
drop/server/internal/session/memory.ts
Husky f04daf0388 Add ODIC Back-Channel Logout (#304)
* prevent returning expired sessions

* add issuer to ODIC creds

* get id token in ODIC

* make session signin return session

* working backchannel logout?

* require https for ODIC provider

* handle wellknown not being https

* find session api progress

* fix windows build

* return session token on session

* switch OIDC to #searchSessions

* update pnpm

* switch to using message on error obj

* move odic callback

* fix type errors

* redirect old oidc callback

* make redirect url a URL

* remove scheduled task downloadCleanup

* fix session search for oidc

* fix signin result

* cleanup code

* ignore data dir

* fix lint error
2026-01-20 09:50:04 +11:00

74 lines
2.2 KiB
TypeScript

import type { SessionProvider, SessionWithToken } from "./types";
export default function createMemorySessionHandler() {
const sessions = new Map<string, SessionWithToken>();
const memoryProvider: SessionProvider = {
async setSession(token, data) {
const session = { ...data, token };
sessions.set(token, session);
return session;
},
async getSession<T extends SessionWithToken>(
token: string,
): Promise<T | undefined> {
const session = sessions.get(token);
return session ? (session as T) : undefined; // Ensure undefined is returned if session is not found
},
async updateSession(token, data) {
return (await this.setSession(token, data)) !== undefined;
},
async removeSession(token) {
sessions.delete(token);
return true;
},
async cleanupSessions() {
const now = new Date();
for (const [token, session] of sessions) {
// if expires at time is before now, the session is expired
if (session.expiresAt < now) await this.removeSession(token);
}
},
async findSessions(options) {
const results: SessionWithToken[] = [];
for (const session of sessions.values()) {
let match = true;
if (
options.userId &&
session.authenticated &&
session.authenticated.userId !== options.userId
) {
match = false;
}
if (options.oidc && session.oidc) {
for (const [key, value] of Object.entries(options.oidc)) {
// stringify to do deep comparison
if (
JSON.stringify(
(session.oidc as unknown as Record<string, unknown>)[key],
) !== JSON.stringify(value)
) {
match = false;
break;
}
}
}
for (const [key, value] of Object.entries(options.data || {})) {
// stringify to do deep comparison
if (JSON.stringify(session.data[key]) !== JSON.stringify(value)) {
match = false;
break;
}
}
if (match) {
results.push(session);
}
}
return results;
},
};
return memoryProvider;
}