diff --git a/app/src/immich.ts b/app/src/immich.ts index 8938944..4474f99 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -45,14 +45,29 @@ const shareCache = new Map() const shareCacheTtlMs = 120_000 const shareCacheMax = 100 +/* + Immich replaced the deprecated `?password=...` query-param auth for shared + links with `POST /shared-links/login`, which returns an + `immich_shared_link_token` cookie used on subsequent calls. This cache holds + one such token per (keyType, key, password) so the gallery's many asset + requests reuse a single login round-trip. Keying by password is load-bearing + for security: a request without the correct password produces a different + cache key (often empty) and falls through to a fresh login that Immich will + reject — IPP never serves cached tokens to unauthenticated visitors. +*/ +type TokenCacheEntry = { promise: Promise; expiresAt: number } +const tokenCache = new Map() +const tokenCacheTtlMs = 120_000 +const tokenCacheMax = 100 + class Immich { /** * Make a request to Immich API. We're not using the SDK to limit * the possible attack surface of this app. */ - async request (endpoint: string) { + async request (endpoint: string, init?: RequestInit) { try { - const res = await fetch(this.apiUrl() + endpoint) + const res = await fetch(this.apiUrl() + endpoint, init) if (res.status === 200) { const contentType = res.headers.get('Content-Type') || '' if (contentType.includes('application/json')) { @@ -212,10 +227,10 @@ class Immich { private async fetchShareByKey (key: string, password?: string, keyType: KeyType = KeyType.key): Promise { let link const url = this.buildUrl(this.apiUrl() + '/shared-links/me', { - [keyType]: key, - password + [keyType]: key }) - const res = await fetch(url) + const headers = await this.authHeaders(keyType, key, password) + const res = await fetch(url, { headers }) if ((res.headers.get('Content-Type') || '').toLowerCase().includes('application/json')) { const jsonBody = await res.json() if (jsonBody) { @@ -228,9 +243,8 @@ class Immich { // the array of assets if (link.type === AlbumType.album) { const albumRes = await fetch(this.buildUrl(this.apiUrl() + '/albums/' + link?.album?.id, { - [keyType]: key, - password - })) + [keyType]: key + }), { headers }) const album = await albumRes.json() as Album if (!album?.id) { log('Invalid album ID - ' + link?.album?.id) @@ -306,13 +320,79 @@ class Immich { * Get the content-type of a video, for the lightbox