Skip to content

Commit 2243830

Browse files
committed
Adapt the auth service to also accept OATs
1 parent 622b185 commit 2243830

6 files changed

+197
-141
lines changed
Lines changed: 15 additions & 116 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,117 +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-
});
26+
const authenticationResult = await authenticateRequest(request);
4627

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-
});
56-
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-
async function getEnvironmentFromEnv({
74-
projectId,
75-
userId,
76-
env,
77-
}: {
78-
projectId: string;
79-
userId: string;
80-
env: ParamsSchema["env"];
81-
}): Promise<
82-
| {
83-
success: true;
84-
environment: RuntimeEnvironment;
85-
}
86-
| {
87-
success: false;
88-
error: string;
89-
}
90-
> {
91-
if (env === "dev") {
92-
const environment = await prisma.runtimeEnvironment.findFirst({
93-
where: {
94-
projectId,
95-
orgMember: {
96-
userId: userId,
97-
},
98-
},
99-
});
100-
101-
if (!environment) {
102-
return {
103-
success: false,
104-
error: "Dev environment not found",
105-
};
106-
}
107-
108-
return {
109-
success: true,
110-
environment,
111-
};
112-
}
113-
114-
let slug: "stg" | "prod" | "preview" = "prod";
115-
switch (env) {
116-
case "staging":
117-
slug = "stg";
118-
break;
119-
case "prod":
120-
slug = "prod";
121-
break;
122-
case "preview":
123-
slug = "preview";
124-
break;
125-
default:
126-
break;
127-
}
128-
129-
const environment = await prisma.runtimeEnvironment.findFirst({
130-
where: {
131-
projectId,
132-
slug,
133-
},
134-
});
135-
136-
if (!environment) {
137-
return {
138-
success: false,
139-
error: `${env === "staging" ? "Staging" : "Production"} environment not found`,
140-
};
141-
}
142-
143-
return {
144-
success: true,
145-
environment,
146-
};
147-
}

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)