mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-08 21:15:18 +09:00
## Description The issue it's the production bug only. In all other cases, session was used (same domain on canvas and on builder). closes #1159 ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio-builder/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env.example` and the `builder/env-check.js` if mandatory
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import type { AppContext } from "@webstudio-is/trpc-interface/server";
|
|
import { z } from "zod";
|
|
import * as cryptoJson from "./crypto/crypto-json.server";
|
|
import { authenticator } from "~/services/auth.server";
|
|
import { trpcClient } from "~/services/trpc.server";
|
|
import env from "~/env/env.server";
|
|
|
|
const AuthReadToken = z.object({
|
|
projectId: z.string(),
|
|
});
|
|
|
|
type AuthReadToken = z.infer<typeof AuthReadToken>;
|
|
|
|
const createAuthorizationContext = async (
|
|
request: Request,
|
|
buildEnv: AppContext["authorization"]["buildEnv"]
|
|
): Promise<AppContext["authorization"]> => {
|
|
const originUrl = new URL(request.url);
|
|
let url = originUrl;
|
|
|
|
// In case of css use referrer as url to extract tokens from canvas iframe href
|
|
if (originUrl.pathname === "/s/css") {
|
|
const referer = request.headers.get("referer");
|
|
if (referer !== null) {
|
|
url = new URL(referer);
|
|
}
|
|
}
|
|
|
|
const authReadTokenRaw = url.searchParams.get("authReadToken") ?? undefined;
|
|
const authToken = url.searchParams.get("authToken") ?? url.hostname;
|
|
|
|
const user = await authenticator.isAuthenticated(request);
|
|
|
|
let authReadToken: AuthReadToken | undefined;
|
|
|
|
if (authReadTokenRaw !== undefined) {
|
|
authReadToken = AuthReadToken.parse(
|
|
await cryptoJson.decode(authReadTokenRaw, env.AUTH_SECRET ?? "NO-SECRET")
|
|
);
|
|
}
|
|
|
|
const context: AppContext["authorization"] = {
|
|
userId: user?.id,
|
|
authReadToken,
|
|
authToken,
|
|
buildEnv,
|
|
authorizeTrpc: trpcClient.authorize,
|
|
};
|
|
|
|
return context;
|
|
};
|
|
|
|
export const createAuthReadToken = async (tokenData: AuthReadToken) => {
|
|
return cryptoJson.encode(tokenData, env.AUTH_SECRET ?? "NO-SECRET");
|
|
};
|
|
|
|
/**
|
|
* argument buildEnv==="prod" only if we are loading project with production build
|
|
*/
|
|
export const createContext = async (
|
|
request: Request,
|
|
buildEnv: AppContext["authorization"]["buildEnv"] = "dev"
|
|
): Promise<AppContext> => {
|
|
const authorization = await createAuthorizationContext(request, buildEnv);
|
|
|
|
return {
|
|
authorization,
|
|
};
|
|
};
|