Skip to content

Commit c6728d0

Browse files
committed
Adapt the auth service to also accept OATs
1 parent 0a82a56 commit c6728d0

6 files changed

+197
-186
lines changed
Lines changed: 15 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
22
import { type GetProjectEnvResponse } from "@trigger.dev/core/v3";
3-
import { type RuntimeEnvironment } from "@trigger.dev/database";
43
import { z } from "zod";
5-
import { prisma } from "~/db.server";
64
import { env as processEnv } from "~/env.server";
7-
import { logger } from "~/services/logger.server";
8-
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
5+
import {
6+
authenticatedEnvironmentForAuthentication,
7+
authenticateRequest,
8+
} from "~/services/apiAuth.server";
99

1010
const ParamsSchema = z.object({
1111
projectRef: z.string(),
@@ -15,14 +15,6 @@ const ParamsSchema = z.object({
1515
type ParamsSchema = z.infer<typeof ParamsSchema>;
1616

1717
export async function loader({ request, params }: LoaderFunctionArgs) {
18-
logger.info("projects get env", { url: request.url });
19-
20-
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
21-
22-
if (!authenticationResult) {
23-
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
24-
}
25-
2618
const parsedParams = ParamsSchema.safeParse(params);
2719

2820
if (!parsedParams.success) {
@@ -31,162 +23,24 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
3123

3224
const { projectRef, env } = parsedParams.data;
3325

34-
const project = await prisma.project.findFirst({
35-
where: {
36-
externalRef: projectRef,
37-
organization: {
38-
members: {
39-
some: {
40-
userId: authenticationResult.userId,
41-
},
42-
},
43-
},
44-
},
45-
});
46-
47-
if (!project) {
48-
return json({ error: "Project not found" }, { status: 404 });
49-
}
50-
51-
const envResult = await getEnvironmentFromEnv({
52-
projectId: project.id,
53-
userId: authenticationResult.userId,
54-
env,
55-
});
26+
const authenticationResult = await authenticateRequest(request);
5627

57-
if (!envResult.success) {
58-
return json({ error: envResult.error }, { status: 404 });
28+
if (!authenticationResult) {
29+
return json({ error: "Invalid or Missing API key" }, { status: 401 });
5930
}
6031

61-
const runtimeEnv = envResult.environment;
32+
const environment = await authenticatedEnvironmentForAuthentication(
33+
authenticationResult,
34+
projectRef,
35+
env
36+
);
6237

6338
const result: GetProjectEnvResponse = {
64-
apiKey: runtimeEnv.apiKey,
65-
name: project.name,
39+
apiKey: environment.apiKey,
40+
name: environment.project.name,
6641
apiUrl: processEnv.API_ORIGIN ?? processEnv.APP_ORIGIN,
67-
projectId: project.id,
42+
projectId: environment.project.id,
6843
};
6944

7045
return json(result);
7146
}
72-
73-
export async function getEnvironmentFromEnv({
74-
projectId,
75-
userId,
76-
env,
77-
branch,
78-
}: {
79-
projectId: string;
80-
userId: string;
81-
env: ParamsSchema["env"];
82-
branch?: string;
83-
}): Promise<
84-
| {
85-
success: true;
86-
environment: RuntimeEnvironment;
87-
}
88-
| {
89-
success: false;
90-
error: string;
91-
}
92-
> {
93-
if (env === "dev") {
94-
const environment = await prisma.runtimeEnvironment.findFirst({
95-
where: {
96-
projectId,
97-
orgMember: {
98-
userId: userId,
99-
},
100-
},
101-
});
102-
103-
if (!environment) {
104-
return {
105-
success: false,
106-
error: "Dev environment not found",
107-
};
108-
}
109-
110-
return {
111-
success: true,
112-
environment,
113-
};
114-
}
115-
116-
let slug: "stg" | "prod" | "preview" = "prod";
117-
switch (env) {
118-
case "staging":
119-
slug = "stg";
120-
break;
121-
case "prod":
122-
slug = "prod";
123-
break;
124-
case "preview":
125-
slug = "preview";
126-
break;
127-
default:
128-
break;
129-
}
130-
131-
if (slug === "preview") {
132-
const previewEnvironment = await prisma.runtimeEnvironment.findFirst({
133-
where: {
134-
projectId,
135-
slug: "preview",
136-
},
137-
});
138-
139-
if (!previewEnvironment) {
140-
return {
141-
success: false,
142-
error: "Preview environment not found",
143-
};
144-
}
145-
146-
// If no branch is provided, just return the parent preview environment
147-
if (!branch) {
148-
return {
149-
success: true,
150-
environment: previewEnvironment,
151-
};
152-
}
153-
154-
const branchEnvironment = await prisma.runtimeEnvironment.findFirst({
155-
where: {
156-
parentEnvironmentId: previewEnvironment.id,
157-
branchName: branch,
158-
},
159-
});
160-
161-
if (!branchEnvironment) {
162-
return {
163-
success: false,
164-
error: `Preview branch ${branch} not found`,
165-
};
166-
}
167-
168-
return {
169-
success: true,
170-
environment: branchEnvironment,
171-
};
172-
}
173-
174-
const environment = await prisma.runtimeEnvironment.findFirst({
175-
where: {
176-
projectId,
177-
slug,
178-
},
179-
});
180-
181-
if (!environment) {
182-
return {
183-
success: false,
184-
error: `${env === "staging" ? "Staging" : "Production"} environment not found`,
185-
};
186-
}
187-
188-
return {
189-
success: true,
190-
environment,
191-
};
192-
}

apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LoaderFunctionArgs, json } from "@remix-run/server-runtime";
22
import { z } from "zod";
33
import { prisma } from "~/db.server";
44
import {
5-
authenticateProjectApiKeyOrPersonalAccessToken,
5+
authenticateRequest,
66
authenticatedEnvironmentForAuthentication,
77
} from "~/services/apiAuth.server";
88
import zlib from "node:zlib";
@@ -20,7 +20,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
2020
return json({ error: "Invalid params" }, { status: 400 });
2121
}
2222

23-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
23+
const authenticationResult = await authenticateRequest(request);
2424

2525
if (!authenticationResult) {
2626
return json({ error: "Invalid or Missing API key" }, { status: 401 });

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.$name.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { UpdateEnvironmentVariableRequestBody } from "@trigger.dev/core/v3";
33
import { z } from "zod";
44
import { prisma } from "~/db.server";
55
import {
6-
authenticateProjectApiKeyOrPersonalAccessToken,
6+
authenticateRequest,
77
authenticatedEnvironmentForAuthentication,
88
} from "~/services/apiAuth.server";
99
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
@@ -21,7 +21,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
2121
return json({ error: "Invalid params" }, { status: 400 });
2222
}
2323

24-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
24+
const authenticationResult = await authenticateRequest(request);
2525

2626
if (!authenticationResult) {
2727
return json({ error: "Invalid or Missing API key" }, { status: 401 });
@@ -97,7 +97,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
9797
return json({ error: "Invalid params" }, { status: 400 });
9898
}
9999

100-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
100+
const authenticationResult = await authenticateRequest(request);
101101

102102
if (!authenticationResult) {
103103
return json({ error: "Invalid or Missing API key" }, { status: 401 });

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.import.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ImportEnvironmentVariablesRequestBody } from "@trigger.dev/core/v3";
33
import { parse } from "dotenv";
44
import { z } from "zod";
55
import {
6-
authenticateProjectApiKeyOrPersonalAccessToken,
6+
authenticateRequest,
77
authenticatedEnvironmentForAuthentication,
88
branchNameFromRequest,
99
} from "~/services/apiAuth.server";
@@ -21,7 +21,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
2121
return json({ error: "Invalid params" }, { status: 400 });
2222
}
2323

24-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
24+
const authenticationResult = await authenticateRequest(request);
2525

2626
if (!authenticationResult) {
2727
return json({ error: "Invalid or Missing API key" }, { status: 401 });

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ActionFunctionArgs, LoaderFunctionArgs, json } from "@remix-run/server-
22
import { CreateEnvironmentVariableRequestBody } from "@trigger.dev/core/v3";
33
import { z } from "zod";
44
import {
5-
authenticateProjectApiKeyOrPersonalAccessToken,
5+
authenticateRequest,
66
authenticatedEnvironmentForAuthentication,
77
} from "~/services/apiAuth.server";
88
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
@@ -19,7 +19,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
1919
return json({ error: "Invalid params" }, { status: 400 });
2020
}
2121

22-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
22+
const authenticationResult = await authenticateRequest(request);
2323

2424
if (!authenticationResult) {
2525
return json({ error: "Invalid or Missing API key" }, { status: 401 });
@@ -66,7 +66,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
6666
return json({ error: "Invalid params" }, { status: 400 });
6767
}
6868

69-
const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request);
69+
const authenticationResult = await authenticateRequest(request);
7070

7171
if (!authenticationResult) {
7272
return json({ error: "Invalid or Missing API key" }, { status: 401 });

0 commit comments

Comments
 (0)