Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Alt

Manav Rathib550ee6b159a
diff --git a/web/packages/accounts/services/session.ts b/web/packages/accounts/services/session.ts
index a64b181153..8b6d3e29d3 100644
--- a/web/packages/accounts/services/session.ts
+++ b/web/packages/accounts/services/session.ts
@@ -1,10 +1,18 @@
import { authenticatedRequestHeaders, HTTPError } from "ente-base/http";
-import { ensureLocalUser } from "ente-base/local-user";
+import {
+ ensureAuthToken,
+ ensureLocalUser,
+ getAuthToken,
+} from "ente-base/local-user";
+import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import { getData } from "ente-shared/storage/localStorage";
import type { KeyAttributes } from "ente-shared/user/types";
+import { nullToUndefined } from "ente-utils/transform";
+import { z } from "zod";
import type { SRPAttributes } from "./srp-remote";
import { getSRPAttributes } from "./srp-remote";
+import { putAttributes } from "./user";
type SessionValidity =
| { status: "invalid" }
@@ -15,6 +23,16 @@ type SessionValidity =
updatedSRPAttributes: SRPAttributes;
};
+const SessionValidityResponse = z.object({
+ hasSetKeys: z.boolean(),
+ // TODO: Update me when we have a zod type for KeyAttributes.
+ keyAttributes: z
+ .object({})
+ .passthrough()
+ .nullish()
+ .transform(nullToUndefined),
+});
+
/**
* Check if the local token and/or key attributes we have are still valid.
*
@@ -72,16 +90,13 @@ export const checkSessionValidity = async (): Promise<SessionValidity> => {
// See if the response contains keyAttributes (they might not for older
// deployments).
- const json = await res.json();
- if (
- "keyAttributes" in json &&
- typeof json.keyAttributes == "object" &&
- json.keyAttributes !== null
- ) {
+ const { keyAttributes } = SessionValidityResponse.parse(await res.json());
+ if (keyAttributes) {
// Assume it is a `KeyAttributes`.
//
// Enhancement: Convert this to a zod validation.
- const remoteKeyAttributes = json.keyAttributes as KeyAttributes;
+ // TODO: Update me when we have a zod type for KeyAttributes.
+ const remoteKeyAttributes = keyAttributes as KeyAttributes;
// We should have these values locally if we reach here.
const email = ensureLocalUser().email;
@@ -116,3 +131,51 @@ export const checkSessionValidity = async (): Promise<SessionValidity> => {
// The token is still valid (to the best of our ascertainable knowledge).
return { status: "valid" };
};
+
+/**
+ * Return `true` if the user does not have a saved auth token, of it is no
+ * longer valid. If needed, also update the key attributes at remote.
+ *
+ * This is a subset of {@link checkSessionValidity} that has been tailored for
+ * use during each remote sync, to detect if the user has been logged out
+ * elsewhere.
+ *
+ * @returns `true` if either we don't have an auth token, or if remote tells us
+ * that the auth token (and the associated session) has been invalidated. In all
+ * other cases, return `false`.
+ *
+ * In particular, this function doesn't throw and instead returns `false` on
+ * errors. This is because returning `true` will trigger a blocking alert that
+ * ends in logging the user out, and we don't want to log the user out on on
+ * e.g. transient network issues.
+ */
+export const isAuthTokenInvalid = async (): Promise<boolean> => {
+ if (!(await getAuthToken())) {
+ return true; /* No saved token, session is invalid */
+ }
+
+ try {
+ const res = await fetch(await apiURL("/users/session-validity/v2"), {
+ headers: await authenticatedRequestHeaders(),
+ });
+ if (!res.ok) {
+ if (res.status == 401) return true; /* session is no longer valid */
+ else throw new HTTPError(res);
+ }
+
+ const { hasSetKeys } = SessionValidityResponse.parse(await res.json());
+ if (!hasSetKeys) {
+ await putAttributes(
+ await ensureAuthToken(),
+ getData("originalKeyAttributes"),
+ );
+ }
+ } catch (e) {
+ log.warn("Failed to check session validity", e);
+ // Don't logout user on potentially transient errors.
+ return false;
+ }
+
+ // Everything seems ok.
+ return false;
+};