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
This commit is contained in:
Bruno Bernardino
2025-06-05 18:10:40 +01:00
committed by GitHub
parent cabc18f15d
commit aa18dcdb4e
14 changed files with 490 additions and 22 deletions

View File

@@ -0,0 +1,69 @@
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);
}
}
}