Files
bewcloud/lib/interfaces/simple-cache.ts
Bruno Bernardino aa18dcdb4e Implement (optional) SSO via OIDC (OpenID Connect) (#64)
This implements optional SSO via OIDC for logging in and signing up (for the first admin sign up or if sign up is allowed). The most requested feature!

Tested with Authentik and Google!

It includes a new `SimpleCache` interface (in-memory, using [`caches`](https://developer.mozilla.org/en-US/docs/Web/API/Window/caches)) for storing the state and code challenges.

Closes #13
2025-06-05 18:10:40 +01:00

70 lines
1.6 KiB
TypeScript

const CACHE_NAME_PREFIX = 'bewcloud-v1-';
const CURRENT_CACHES: Set<string> = new Set();
const FALLBACK_CACHE: Map<string, string> = new Map();
export default class SimpleCache {
protected cacheName = `${CACHE_NAME_PREFIX}default`;
constructor(cacheName = 'default') {
this.cacheName = `${CACHE_NAME_PREFIX}${cacheName}`;
}
public async get() {
if (!CURRENT_CACHES.has(this.cacheName)) {
return '';
}
try {
const request = new Request(`https://fake.cache/${this.cacheName}`);
const cache = await caches.open(this.cacheName);
const response = await cache.match(request);
if (response) {
return response.text();
}
} catch (error) {
console.error(error);
return FALLBACK_CACHE.get(this.cacheName) || '';
}
return '';
}
public async set(value: string) {
if (!CURRENT_CACHES.has(this.cacheName)) {
CURRENT_CACHES.add(this.cacheName);
}
try {
await this.clear();
const request = new Request(`https://fake.cache/${this.cacheName}`);
const cache = await caches.open(this.cacheName);
const response = new Response(value, { headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
await cache.put(request, response.clone());
} catch (error) {
console.error(error);
FALLBACK_CACHE.set(this.cacheName, value);
}
}
public async clear() {
if (!CURRENT_CACHES.has(this.cacheName)) {
return null;
}
try {
await caches.delete(this.cacheName);
} catch (error) {
console.error(error);
FALLBACK_CACHE.delete(this.cacheName);
}
}
}