Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web-backend): add Extension auth and recording handlers
Richie McIlroy38107a06f464
diff --git a/packages/web-backend/src/Extension/Extensions.ts b/packages/web-backend/src/Extension/Extensions.tsnew file mode 100644index 000000000..da50592ba--- /dev/null+++ b/packages/web-backend/src/Extension/Extensions.ts@@ -0,0 +1,170 @@+import * as Db from "@cap/database/schema";+import type { Organisation, User } from "@cap/web-domain";+import * as Dz from "drizzle-orm";+import { Data, Effect, Option } from "effect";++import { Database } from "../Database.ts";++// Auth keys are minted by a GET navigation, so a forced cross-site navigation+// could otherwise spam rows; cap how many keys one user can mint per window.+const AUTH_KEY_MINT_WINDOW_MS = 60 * 60 * 1000;+const AUTH_KEY_MINT_LIMIT = 10;++export class AuthKeyMintRateLimited extends Data.TaggedError(+ "AuthKeyMintRateLimited",+) {}++export class Extensions extends Effect.Service<Extensions>()("Extensions", {+ effect: Effect.gen(function* () {+ const db = yield* Database;++ const selectOwnedOrganization = (where: Dz.SQL) =>+ db.use((db) =>+ db+ .select({+ id: Db.organizations.id,+ name: Db.organizations.name,+ stripeSubscriptionStatus: Db.users.stripeSubscriptionStatus,+ thirdPartyStripeSubscriptionId:+ Db.users.thirdPartyStripeSubscriptionId,+ })+ .from(Db.organizations)+ .innerJoin(Db.users, Dz.eq(Db.organizations.ownerId, Db.users.id))+ .where(where)+ // Deterministic fallback selection: this result can be persisted+ // as the user's activeOrganizationId below, so an unordered+ // LIMIT 1 would let the active org flap between requests.+ .orderBy(Dz.asc(Db.organizations.id))+ .limit(1),+ );++ return {+ mintAuthKey: Effect.fn("Extensions.mintAuthKey")(function* (+ userId: User.UserId,+ ) {+ // Insert first, then count (including the new row) and roll back on+ // overflow. A check-then-insert would let concurrent mints race past+ // the limit; this way racers can transiently overshoot but every+ // overshooting mint deletes its own key and fails, so the table+ // settles at or below the cap.+ const authApiKey = crypto.randomUUID();+ yield* db.use((db) =>+ db.insert(Db.authApiKeys).values({+ id: authApiKey,+ userId,+ }),+ );++ const mintedSince = new Date(Date.now() - AUTH_KEY_MINT_WINDOW_MS);+ const recentKeys = yield* db.use((db) =>+ db+ .select({ id: Db.authApiKeys.id })+ .from(Db.authApiKeys)+ .where(+ Dz.and(+ Dz.eq(Db.authApiKeys.userId, userId),+ Dz.gt(Db.authApiKeys.createdAt, mintedSince),+ ),+ )+ .limit(AUTH_KEY_MINT_LIMIT + 1),+ );++ if (recentKeys.length > AUTH_KEY_MINT_LIMIT) {+ yield* db.use((db) =>+ db+ .delete(Db.authApiKeys)+ .where(Dz.eq(Db.authApiKeys.id, authApiKey)),+ );+ return yield* Effect.fail(new AuthKeyMintRateLimited());+ }++ return authApiKey;+ }),++ revokeAuthKey: Effect.fn("Extensions.revokeAuthKey")(function* (+ userId: User.UserId,+ authApiKey: string,+ ) {+ yield* db.use((db) =>+ db+ .delete(Db.authApiKeys)+ .where(+ Dz.and(+ Dz.eq(Db.authApiKeys.id, authApiKey),+ Dz.eq(Db.authApiKeys.userId, userId),+ ),+ ),+ );+ }),++ resolveBootstrapOrganization: Effect.fn(+ "Extensions.resolveBootstrapOrganization",+ )(function* (user: {+ id: User.UserId;+ activeOrganizationId: Organisation.OrganisationId;+ }) {+ let [organization] = yield* selectOwnedOrganization(+ Dz.eq(Db.organizations.id, user.activeOrganizationId),+ );++ // A dangling activeOrganizationId (deleted org, revoked+ // membership) must not brick the extension; fall back to an+ // organization the user owns, then to one they are a member+ // of.+ if (!organization) {+ [organization] = yield* selectOwnedOrganization(+ Dz.eq(Db.organizations.ownerId, user.id),+ );+ }++ if (!organization) {+ const [viaMembership] = yield* db.use((db) =>+ db+ .select({+ id: Db.organizations.id,+ name: Db.organizations.name,+ stripeSubscriptionStatus: Db.users.stripeSubscriptionStatus,+ thirdPartyStripeSubscriptionId:+ Db.users.thirdPartyStripeSubscriptionId,+ })+ .from(Db.organizationMembers)+ .innerJoin(+ Db.organizations,+ Dz.eq(+ Db.organizationMembers.organizationId,+ Db.organizations.id,+ ),+ )+ .innerJoin(Db.users, Dz.eq(Db.organizations.ownerId, Db.users.id))+ .where(Dz.eq(Db.organizationMembers.userId, user.id))+ // Oldest membership first, with the org id as a stable+ // tie-break — same determinism requirement as above.+ .orderBy(+ Dz.asc(Db.organizationMembers.createdAt),+ Dz.asc(Db.organizations.id),+ )+ .limit(1),+ );+ organization = viaMembership;+ }++ // Repair the dangling pointer: createInstantRecording rejects any+ // org that is not the user's activeOrganizationId, so returning a+ // fallback org without persisting it would let bootstrap succeed+ // while every recording create fails PolicyDenied.+ const resolved = organization;+ if (resolved && resolved.id !== user.activeOrganizationId) {+ yield* db.use((db) =>+ db+ .update(Db.users)+ .set({ activeOrganizationId: resolved.id })+ .where(Dz.eq(Db.users.id, user.id)),+ );+ }++ return Option.fromNullable(resolved);+ }),+ };+ }),+ dependencies: [Database.Default],+}) {}diff --git a/packages/web-backend/src/Extension/Http.ts b/packages/web-backend/src/Extension/Http.tsnew file mode 100644index 000000000..286f0afbe--- /dev/null+++ b/packages/web-backend/src/Extension/Http.ts@@ -0,0 +1,386 @@+import { serverEnv } from "@cap/env";+import { userIsPro } from "@cap/utils";+import {+ CurrentUser,+ Extension,+ Http,+ type User,+ Video,+} from "@cap/web-domain";+import {+ HttpApiBuilder,+ HttpApiError,+ HttpServerRequest,+ HttpServerResponse,+} from "@effect/platform";+import { Effect, Option, Schema } from "effect";++import { getCurrentUser } from "../Auth.ts";+import { handleDomainError } from "../Http/Errors.ts";+import { Videos } from "../Videos/index.ts";+import { Extensions } from "./Extensions.ts";++const CHROMIUM_IDENTITY_HOST_SUFFIX = ".chromiumapp.org";++const validateExtensionRedirectUri = (redirectUri: string) =>+ Effect.gen(function* () {+ const url = yield* Effect.try({+ try: () => new URL(redirectUri),+ catch: () => new HttpApiError.BadRequest(),+ });++ if (+ url.protocol !== "https:" ||+ !url.hostname.endsWith(CHROMIUM_IDENTITY_HOST_SUFFIX)+ ) {+ return yield* new HttpApiError.BadRequest();+ }++ const extensionId = url.hostname.slice(+ 0,+ -CHROMIUM_IDENTITY_HOST_SUFFIX.length,+ );+ const configuredExtensionId = serverEnv().CAP_CHROME_EXTENSION_ID;++ if (configuredExtensionId) {+ if (extensionId !== configuredExtensionId) {+ return yield* new HttpApiError.BadRequest();+ }+ return url;+ }++ // Without a pinned extension id, any installed extension could mint a+ // signed-in user's auth key through this flow. The only deployment+ // where accepting an arbitrary id is safe is localhost development;+ // every reachable deployment (staging, previews, self-hosted) must set+ // CAP_CHROME_EXTENSION_ID regardless of NODE_ENV.+ const webHostname = new URL(serverEnv().WEB_URL).hostname;+ const isLocalDevelopment =+ serverEnv().NODE_ENV !== "production" &&+ (webHostname === "localhost" || webHostname === "127.0.0.1");++ if (!isLocalDevelopment) {+ return yield* new HttpApiError.BadRequest();+ }++ return url;+ });++const redirectToLogin = (nextUrl: URL) => {+ const loginUrl = new URL("/login", serverEnv().WEB_URL);+ loginUrl.searchParams.set("next", nextUrl.toString());+ return HttpServerResponse.redirect(loginUrl);+};++// Rebuilds the consent-page URL from the current request so an expired+// session can restart the flow after login. Resolved as a sibling of the+// current endpoint path, which keeps it correct under whatever prefix the+// host app mounts the API.+const consentPageUrl = (+ requestUrl: string,+ params: { redirectUri: string; state: Option.Option<string> },+) => {+ const currentUrl = new URL(requestUrl, serverEnv().WEB_URL);+ const startPath = Extension.ExtensionApiPaths.startAuth.slice(+ Extension.ExtensionApiPaths.startAuth.lastIndexOf("/") + 1,+ );+ const startUrl = new URL(startPath, currentUrl);+ startUrl.search = "";+ startUrl.searchParams.set("redirectUri", params.redirectUri);+ if (Option.isSome(params.state)) {+ startUrl.searchParams.set("state", params.state.value);+ }+ return startUrl;+};++const escapeHtml = (value: string) =>+ value+ .replace(/&/g, "&")+ .replace(/</g, "<")+ .replace(/>/g, ">")+ .replace(/"/g, """)+ .replace(/'/g, "'");++// The redirect target is the extension's chromiumapp.org interceptor; the+// error travels in the fragment exactly like a minted key would, so the+// extension's launchWebAuthFlow resolves immediately instead of waiting for+// the user to close the window.+const buildCancelUrl = (redirectUri: URL, state: Option.Option<string>) => {+ const params = new URLSearchParams({ error: "access_denied" });+ if (Option.isSome(state)) {+ params.set("state", state.value);+ }+ const cancelUrl = new URL(redirectUri);+ cancelUrl.hash = params.toString();+ return cancelUrl;+};++const renderConsentPage = ({+ email,+ redirectUri,+ state,+}: {+ email: string;+ redirectUri: URL;+ state: Option.Option<string>;+}) => {+ const stateField = Option.isSome(state)+ ? `<input type="hidden" name="state" value="${escapeHtml(state.value)}" />`+ : "";+ // The form action is resolved relative to this page's own URL+ // (.../auth/start -> .../auth/approve) so it works under any API mount+ // prefix without hardcoding it here.+ return HttpServerResponse.html(`<!doctype html>+<html lang="en">+ <head>+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1" />+ <meta name="robots" content="noindex" />+ <title>Connect Cap</title>+ <style>+ body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center; font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; background: #f4f4f5; color: #18181b; }+ .card { background: #fff; border: 1px solid #e4e4e7; border-radius: 16px; padding: 32px; max-width: 400px; width: 100%; margin: 16px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06); }+ h1 { font-size: 18px; margin: 0 0 12px; }+ p { font-size: 14px; line-height: 1.5; color: #52525b; margin: 0 0 12px; }+ .email { font-weight: 600; color: #18181b; }+ .actions { display: flex; gap: 12px; margin: 24px 0 0; }+ .actions > * { flex: 1; display: flex; align-items: center; justify-content: center; height: 40px; border-radius: 10px; font-size: 14px; font-weight: 500; cursor: pointer; text-decoration: none; box-sizing: border-box; }+ button { background: #18181b; color: #fff; border: none; }+ a.cancel { background: #fff; color: #18181b; border: 1px solid #d4d4d8; }+ </style>+ </head>+ <body>+ <main class="card">+ <h1>Connect the Cap Chrome extension</h1>+ <p>The Cap extension is asking for access to your Cap account <span class="email">${escapeHtml(email)}</span> to create and upload recordings on your behalf.</p>+ <p>Only continue if you opened this page from the Cap extension.</p>+ <form method="post" action="approve" class="actions">+ <input type="hidden" name="redirectUri" value="${escapeHtml(redirectUri.toString())}" />+ ${stateField}+ <a class="cancel" href="${escapeHtml(buildCancelUrl(redirectUri, state).toString())}">Cancel</a>+ <button type="submit">Allow access</button>+ </form>+ </main>+ </body>+</html>`);+};++const redirectWithAuthKey = ({+ redirectUri,+ authApiKey,+ userId,+ state,+}: {+ redirectUri: URL;+ authApiKey: string;+ userId: User.UserId;+ state: Option.Option<string>;+}) => {+ const params = new URLSearchParams({+ authApiKey,+ userId,+ });++ if (Option.isSome(state)) {+ params.set("state", state.value);+ }++ redirectUri.hash = params.toString();+ return HttpServerResponse.redirect(redirectUri);+};++const getBearerAuthKey = Effect.gen(function* () {+ const headers = yield* HttpServerRequest.schemaHeaders(+ Schema.Struct({ authorization: Schema.optional(Schema.String) }),+ );+ // Any non-empty bearer token is forwarded; the revoke query matching on+ // (id, userId) decides validity, so this stays correct if the minted key+ // format ever changes.+ const authHeader = headers.authorization?.split(" ")[1];+ return authHeader ? Option.some(authHeader) : Option.none();+});++export const ExtensionHttpLive = HttpApiBuilder.group(+ Http.ApiContract,+ "extension",+ (handlers) =>+ Effect.gen(function* () {+ const extensions = yield* Extensions;+ const videos = yield* Videos;++ return handlers+ .handle("startAuth", ({ urlParams }) =>+ Effect.gen(function* () {+ const request = yield* HttpServerRequest.HttpServerRequest;++ // Deliberately side-effect free: any page, extension, or+ // prefetch (e.g. the router's RSC fetch after OTP login) can+ // force a navigation here, so this only ever renders the+ // consent page. The credential is minted by the consent+ // form's same-origin POST to approveAuth — that two-step+ // shape is also what lets the post-OAuth redirect chain+ // (Sec-Fetch-Site: cross-site via accounts.google.com) land+ // here without weakening the minting endpoint.+ const redirectUri = yield* validateExtensionRedirectUri(+ urlParams.redirectUri,+ );+ const currentUser = yield* getCurrentUser;++ if (Option.isNone(currentUser)) {+ return redirectToLogin(+ consentPageUrl(request.url, {+ redirectUri: urlParams.redirectUri,+ state: urlParams.state,+ }),+ );+ }++ return renderConsentPage({+ email: currentUser.value.email,+ redirectUri,+ state: urlParams.state,+ });+ }).pipe(+ Effect.catchTag(+ "UnknownException",+ () => new Http.InternalServerError({ cause: "unknown" }),+ ),+ handleDomainError,+ ),+ )+ .handle("approveAuth", ({ payload }) =>+ Effect.gen(function* () {+ const request = yield* HttpServerRequest.HttpServerRequest;++ // CSRF gate for the minting step: the only legitimate caller+ // is the consent form, a same-origin top-level POST. Reject+ // every other initiator — cross-site pages, same-site+ // subdomains, and extension-driven navigations+ // (Sec-Fetch-Site: none) alike. Browsers that predate fetch+ // metadata omit the header; fall back to the Origin header+ // they do send on form POSTs.+ const secFetchSite = request.headers["sec-fetch-site"];+ if (secFetchSite !== undefined) {+ if (secFetchSite !== "same-origin") {+ return yield* new HttpApiError.BadRequest();+ }+ } else {+ const origin = request.headers.origin;+ if (+ origin !== undefined &&+ origin !== new URL(serverEnv().WEB_URL).origin+ ) {+ return yield* new HttpApiError.BadRequest();+ }+ }++ const redirectUri = yield* validateExtensionRedirectUri(+ payload.redirectUri,+ );+ const currentUser = yield* getCurrentUser;++ if (Option.isNone(currentUser)) {+ // The session expired between rendering the consent page+ // and approving it; restart the flow at the consent page.+ return redirectToLogin(+ consentPageUrl(request.url, {+ redirectUri: payload.redirectUri,+ state: payload.state,+ }),+ );+ }++ const authApiKey = yield* extensions.mintAuthKey(+ currentUser.value.id,+ );++ return redirectWithAuthKey({+ redirectUri,+ authApiKey,+ userId: currentUser.value.id,+ state: payload.state,+ });+ }).pipe(+ Effect.catchTag(+ "AuthKeyMintRateLimited",+ () => new HttpApiError.BadRequest(),+ ),+ Effect.catchTag(+ "UnknownException",+ () => new Http.InternalServerError({ cause: "unknown" }),+ ),+ handleDomainError,+ ),+ )+ .handle("revokeAuth", () =>+ Effect.gen(function* () {+ const user = yield* CurrentUser;+ const authApiKey = yield* getBearerAuthKey;++ if (Option.isNone(authApiKey)) {+ return { success: false };+ }++ yield* extensions.revokeAuthKey(user.id, authApiKey.value);++ return { success: true };+ }).pipe(+ Effect.catchTag("ParseError", () => new HttpApiError.BadRequest()),+ handleDomainError,+ ),+ )+ .handle("bootstrap", () =>+ Effect.gen(function* () {+ const user = yield* CurrentUser;++ const organization =+ yield* extensions.resolveBootstrapOrganization(user);++ if (Option.isNone(organization)) {+ return yield* new Http.InternalServerError({ cause: "database" });+ }++ const isPro = userIsPro(organization.value);++ return {+ user: {+ id: user.id,+ email: user.email,+ },+ organization: {+ id: organization.value.id,+ name: organization.value.name,+ },+ plan: {+ isPro,+ maxRecordingSeconds: isPro+ ? null+ : Video.FREE_PLAN_MAX_RECORDING_SECONDS,+ },+ };+ }).pipe(handleDomainError),+ )+ .handle("createInstantRecording", ({ payload }) =>+ videos.createInstantRecording(payload).pipe(+ Effect.catchTag("PolicyDenied", (error) => error),+ handleDomainError,+ ),+ )+ .handle("updateInstantRecordingProgress", ({ payload }) =>+ videos.updateUploadProgress(payload).pipe(+ Effect.map((success) => ({ success })),+ Effect.catchTag("VideoNotFoundError", (error) => error),+ Effect.catchTag("PolicyDenied", (error) => error),+ handleDomainError,+ ),+ )+ .handle("deleteInstantRecording", ({ path }) =>+ videos.delete(path.videoId).pipe(+ Effect.as({ success: true }),+ Effect.catchTag("VideoNotFoundError", (error) => error),+ Effect.catchTag("PolicyDenied", (error) => error),+ handleDomainError,+ ),+ );+ }),+);diff --git a/packages/web-backend/src/Extension/index.ts b/packages/web-backend/src/Extension/index.tsnew file mode 100644index 000000000..2831f979f--- /dev/null+++ b/packages/web-backend/src/Extension/index.ts@@ -0,0 +1,2 @@+export { AuthKeyMintRateLimited, Extensions } from "./Extensions.ts";+export { ExtensionHttpLive } from "./Http.ts";diff --git a/packages/web-backend/src/Http/Live.ts b/packages/web-backend/src/Http/Live.tsindex 0ed3b6227..d7e25ab57 100644--- a/packages/web-backend/src/Http/Live.ts+++ b/packages/web-backend/src/Http/Live.ts@@ -2,8 +2,10 @@ import { Http } from "@cap/web-domain";import { HttpApiBuilder } from "@effect/platform";import { Layer } from "effect";+import { ExtensionHttpLive } from "../Extension/Http.ts";import { LoomHttpLive } from "../Loom/Http.ts";export const HttpLive = HttpApiBuilder.api(Http.ApiContract).pipe(+ Layer.provide(ExtensionHttpLive),Layer.provide(LoomHttpLive),);diff --git a/packages/web-backend/src/index.ts b/packages/web-backend/src/index.tsindex 24f5bab91..7cd60bc8f 100644--- a/packages/web-backend/src/index.ts+++ b/packages/web-backend/src/index.ts@@ -1,6 +1,7 @@export * from "./Auth.ts";export * from "./Aws.ts";export * from "./Database.ts";+export * from "./Extension/index.ts";export { Folders } from "./Folders/index.ts";export { HttpLive } from "./Http/Live.ts";export { ImageUploads } from "./ImageUploads/index.ts";