π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Remove photos auth fallbacks from albums media fetches
Anand Baburajan2459ce4f9934
diff --git a/web/apps/albums/src/gallery/services/download.ts b/web/apps/albums/src/gallery/services/download.tsindex 43391c316c..0ab5e2eecb 100644--- a/web/apps/albums/src/gallery/services/download.ts+++ b/web/apps/albums/src/gallery/services/download.ts@@ -7,20 +7,19 @@ import {} from "ente-base/crypto";import {authenticatedPublicAlbumsRequestHeaders,- authenticatedRequestHeaders,publicRequestHeaders,retryEnsuringHTTPOk,type PublicAlbumsCredentials,} from "ente-base/http";import log from "ente-base/log";-import { apiURL, customAPIOrigin } from "ente-base/origins";-import { ensureAuthToken } from "ente-base/token";+import { customAPIOrigin } from "ente-base/origins";import type { EnteFile } from "ente-media/file";import { fileFileName } from "ente-media/file-metadata";import { FileType } from "ente-media/file-type";import { decodeLivePhoto } from "ente-media/live-photo";import {getPublicAlbumsCredentials,+ requirePublicAlbumsCredentials,setPublicAlbumsCredentials,} from "./public-albums-credentials";import { detectFileTypeInfoFromChunk } from "../utils/detect-type";@@ -104,8 +103,6 @@ export type RenderableSourceURLs =* including caching them for subsequent retrieval if appropriate.** External code can use it via its singleton instance, {@link downloadManager}.- * The class will initialize itself on first use, however {@link logout} should- * be called on logout to reset its internal state.*/class DownloadManager {get publicAlbumsCredentials() {@@ -312,14 +309,10 @@ class DownloadManager {};private async _downloadThumbnail(file: EnteFile) {- if (this.publicAlbumsCredentials) {- return publicAlbums_downloadThumbnail(- file,- this.publicAlbumsCredentials,- );- } else {- return photos_downloadThumbnail(file);- }+ return publicAlbums_downloadThumbnail(+ file,+ requirePublicAlbumsCredentials(this.publicAlbumsCredentials),+ );}/**@@ -525,19 +518,15 @@ class DownloadManager {}/**- * Download the full contents of {@link file}, automatically choosing the- * credentials for the logged in user or the public albums depending on the- * current app context we are in.+ * Download the full contents of {@link file} using the current public album+ * credentials.*/private async _downloadFile(file: EnteFile, opts?: FileDownloadOpts) {- if (this.publicAlbumsCredentials) {- return publicAlbums_downloadFile(- file,- this.publicAlbumsCredentials,- );- } else {- return photos_downloadFile(file, opts);- }+ return publicAlbums_downloadFile(+ file,+ requirePublicAlbumsCredentials(this.publicAlbumsCredentials),+ opts,+ );}private async blobWithInferredType(@@ -695,32 +684,8 @@ const createRenderableSourceURLs = async (};/**- * The various photos_* functions are used for the actual downloads when- * we're running in the context of the the photos app.+ * Options for file downloads.*/-const photos_downloadThumbnail = async (file: EnteFile) => {- const customOrigin = await customAPIOrigin();-- const getThumbnail = async () => {- if (customOrigin) {- // See: [Note: Passing credentials for self-hosted file fetches]- const token = await ensureAuthToken();- const params = new URLSearchParams({ token });- return fetch(- `${customOrigin}/files/preview/${file.id}?${params.toString()}`,- { headers: publicRequestHeaders() },- );- } else {- return fetch(`https://thumbnails.ente.io/?fileID=${file.id}`, {- headers: await authenticatedRequestHeaders(),- });- }- };-- const res = await retryEnsuringHTTPOk(getThumbnail);- return new Uint8Array(await res.arrayBuffer());-};-interface FileDownloadOpts {/*** `true` if the request is for a background task. These are considered less@@ -728,83 +693,12 @@ interface FileDownloadOpts {** See: [Note: User initiated vs background downloads of files].*- * This parameter is ignored for requests made when using public albums- * credentials to download files; those are always considered interactive.+ * Public album downloads ignore this today; the app only performs+ * interactive file fetches.*/background?: boolean;}-/**- * Download the full contents of the given {@link EnteFile}- */-const photos_downloadFile = async (- file: EnteFile,- opts?: FileDownloadOpts,-): Promise<Response> => {- const { background } = opts ?? {};-- const customOrigin = await customAPIOrigin();-- // [Note: Passing credentials for self-hosted file fetches]- //- // Fetching files (or thumbnails) in the default self-hosted Ente- // configuration involves a redirection:- //- // 1. The browser makes a HTTP GET to a museum with credentials. Museum- // inspects the credentials, in this case the auth token, and if they're- // valid, returns a HTTP 307 redirect to the pre-signed S3 URL that to- // the file in the configured S3 bucket.- //- // 2. The browser follows the redirect to get the actual file. The URL is- // pre-signed, i.e. already has all credentials needed to prove to the S3- // object storage that it should serve this response.- //- // For the first step normally we'd pass the auth the token via the- // "X-Auth-Token" HTTP header. In this case though, that would be- // problematic because the browser preserves the request headers when it- // follows the HTTP 307 redirect, and the "X-Auth-Token" header also gets- // sent to the redirected S3 request made in second step.- //- // To avoid this, we pass the token as a query parameter. Generally this is- // not a good idea, but in this case (a) the URL is not a user visible one- // and (b) even if it gets logged, it'll be in the self-hosters own service.- //- // Note that Ente's own servers don't have these concerns because we use a- // slightly different flow involving a proxy instead of directly connecting- // to the S3 storage.- //- // 1. The web browser makes a HTTP GET request to a proxy passing it the- // credentials in the "X-Auth-Token".- //- // 2. The proxy then does both the original steps: (a). Use the credentials- // to get the pre-signed URL, and (b) fetch that pre-signed URL and- // stream back the response.- //- // [Note: User initiated vs background downloads of files]- //- // The faster proxy approach is used for interactive requests to reduce the- // latency for the user (e.g. when the user is waiting to see a full- // resolution file). It can be faster than a direct connection as the proxy- // is network-nearer to the user (See: [Note: Faster uploads via workers])- //- // For background processing (e.g., ML indexing, HLS generation), the direct- // S3 connection (as what'd happen when self hosting) gets used.-- const getFile = async () => {- if (customOrigin || background) {- const token = await ensureAuthToken();- const url = await apiURL(`/files/download/${file.id}`, { token });- return fetch(url, { headers: publicRequestHeaders() });- } else {- return fetch(`https://files.ente.io/?fileID=${file.id}`, {- headers: await authenticatedRequestHeaders(),- });- }- };-- return retryEnsuringHTTPOk(getFile);-};-/*** The various publicAlbums_* functions are used for the actual downloads when* we're running in the context of the the public albums app.@@ -845,11 +739,13 @@ const publicAlbums_downloadThumbnail = async (const publicAlbums_downloadFile = async (file: EnteFile,credentials: PublicAlbumsCredentials,+ opts?: FileDownloadOpts,) => {+ const { background } = opts ?? {};const customOrigin = await customAPIOrigin();const getFile = () => {- if (customOrigin) {+ if (customOrigin || background) {// See: [Note: Passing credentials for self-hosted file fetches]const { accessToken, accessTokenJWT } = credentials;const params = new URLSearchParams({diff --git a/web/apps/albums/src/gallery/services/file-data.ts b/web/apps/albums/src/gallery/services/file-data.tsindex 0f4e06574f..6407e7b0ae 100644--- a/web/apps/albums/src/gallery/services/file-data.ts+++ b/web/apps/albums/src/gallery/services/file-data.ts@@ -1,12 +1,12 @@import {authenticatedPublicAlbumsRequestHeaders,- authenticatedRequestHeaders,ensureOk,type PublicAlbumsCredentials,} from "ente-base/http";import { apiURL } from "ente-base/origins";import { nullToUndefined } from "ente-utils/transform";import { z } from "zod";+import { requirePublicAlbumsCredentials } from "./public-albums-credentials";/*** [Note: File data APIs]@@ -56,15 +56,15 @@ type RemoteFileData = z.infer<typeof RemoteFileData>;* this file yet (e.g. if type was "vid_preview", this would indicate that a* video preview has been generated for this file yet).*- * @param publicAlbumsCredentials Credentials to use when we are running in the- * context of the public albums app. If these are not specified, then the- * credentials of the logged in user are used.+ * @param publicAlbumsCredentials Credentials for the current public album. If+ * omitted, the currently configured public album credentials are used.*/export const fetchFileData = async (type: FileDataType,fileID: number,publicAlbumsCredentials?: PublicAlbumsCredentials,): Promise<RemoteFileData | undefined> => {+ const credentials = requirePublicAlbumsCredentials(publicAlbumsCredentials);const params = new URLSearchParams({type,fileID: fileID.toString(),@@ -73,19 +73,9 @@ export const fetchFileData = async (preferNoContent: "true",});- let res: Response;- if (publicAlbumsCredentials) {- const url = await apiURL("/public-collection/files/data/fetch");- const headers = authenticatedPublicAlbumsRequestHeaders(- publicAlbumsCredentials,- );- res = await fetch(`${url}?${params.toString()}`, { headers });- } else {- const url = await apiURL("/files/data/fetch");- res = await fetch(`${url}?${params.toString()}`, {- headers: await authenticatedRequestHeaders(),- });- }+ const url = await apiURL("/public-collection/files/data/fetch");+ const headers = authenticatedPublicAlbumsRequestHeaders(credentials);+ const res = await fetch(`${url}?${params.toString()}`, { headers });if (res.status == 204) return undefined;// We're passing `preferNoContent` so the expected response is 204, but this@@ -104,9 +94,8 @@ export const fetchFileData = async (** @param fileIDs The id of the files for which we want the file preview data.*- * @param publicAlbumsCredentials Credentials to use when we are running in the- * context of the public albums app. If these are not specified, then the- * credentials of the logged in user are used.+ * @param publicAlbumsCredentials Credentials for the current public album. If+ * omitted, the currently configured public album credentials are used.** @returns the (pre-signed) URL to the preview data, or undefined if there is* not preview data of the given type for the given file yet.@@ -141,21 +130,12 @@ export const fetchFilePreviewData = async (fileID: number,publicAlbumsCredentials?: PublicAlbumsCredentials,): Promise<string | undefined> => {+ const credentials = requirePublicAlbumsCredentials(publicAlbumsCredentials);const params = new URLSearchParams({ type, fileID: fileID.toString() });- let res: Response;- if (publicAlbumsCredentials) {- const headers = authenticatedPublicAlbumsRequestHeaders(- publicAlbumsCredentials,- );- const url = await apiURL("/public-collection/files/data/preview");- res = await fetch(`${url}?${params.toString()}`, { headers });- } else {- const url = await apiURL("/files/data/preview");- res = await fetch(`${url}?${params.toString()}`, {- headers: await authenticatedRequestHeaders(),- });- }+ const headers = authenticatedPublicAlbumsRequestHeaders(credentials);+ const url = await apiURL("/public-collection/files/data/preview");+ const res = await fetch(`${url}?${params.toString()}`, { headers });if (res.status == 404) return undefined;ensureOk(res);diff --git a/web/apps/albums/src/gallery/services/public-albums-credentials.ts b/web/apps/albums/src/gallery/services/public-albums-credentials.tsindex 62335cc1c5..d4d2ab9ad0 100644--- a/web/apps/albums/src/gallery/services/public-albums-credentials.ts+++ b/web/apps/albums/src/gallery/services/public-albums-credentials.ts@@ -9,3 +9,13 @@ export const setPublicAlbumsCredentials = () => {publicAlbumsCredentials = credentials;};++export const requirePublicAlbumsCredentials = (+ credentials?: PublicAlbumsCredentials,+): PublicAlbumsCredentials => {+ const resolvedCredentials = credentials ?? publicAlbumsCredentials;+ if (!resolvedCredentials) {+ throw new Error("Missing public album credentials");+ }+ return resolvedCredentials;+};diff --git a/web/apps/albums/src/gallery/services/thumbnail-manager.ts b/web/apps/albums/src/gallery/services/thumbnail-manager.tsindex 588bb2993e..d62274ede0 100644--- a/web/apps/albums/src/gallery/services/thumbnail-manager.ts+++ b/web/apps/albums/src/gallery/services/thumbnail-manager.ts@@ -1,17 +1,16 @@import { blobCache, type BlobCache } from "ente-base/blob-cache";import {authenticatedPublicAlbumsRequestHeaders,- authenticatedRequestHeaders,publicRequestHeaders,retryEnsuringHTTPOk,type PublicAlbumsCredentials,} from "ente-base/http";import log from "ente-base/log";import { customAPIOrigin } from "ente-base/origins";-import { ensureAuthToken } from "ente-base/token";import type { EnteFile } from "ente-media/file";import {getPublicAlbumsCredentials,+ requirePublicAlbumsCredentials,setPublicAlbumsCredentials,} from "./public-albums-credentials";import { decryptThumbnailBlobBytes } from "./thumbnail-crypto";@@ -97,14 +96,10 @@ class ThumbnailManager {};private async _downloadThumbnail(file: EnteFile) {- if (this.publicAlbumsCredentials) {- return publicAlbums_downloadThumbnail(- file,- this.publicAlbumsCredentials,- );- } else {- return photos_downloadThumbnail(file);- }+ return publicAlbums_downloadThumbnail(+ file,+ requirePublicAlbumsCredentials(this.publicAlbumsCredentials),+ );}}@@ -130,28 +125,6 @@ const wrapErrors = <T>(op: () => Promise<T>) =>throw new NetworkThumbnailError(e);});-const photos_downloadThumbnail = async (file: EnteFile) => {- const customOrigin = await customAPIOrigin();-- const getThumbnail = async () => {- if (customOrigin) {- const token = await ensureAuthToken();- const params = new URLSearchParams({ token });- return fetch(- `${customOrigin}/files/preview/${file.id}?${params.toString()}`,- { headers: publicRequestHeaders() },- );- } else {- return fetch(`https://thumbnails.ente.io/?fileID=${file.id}`, {- headers: await authenticatedRequestHeaders(),- });- }- };-- const res = await retryEnsuringHTTPOk(getThumbnail);- return new Uint8Array(await res.arrayBuffer());-};-const publicAlbums_downloadThumbnail = async (file: EnteFile,credentials: PublicAlbumsCredentials,