Skip to content

Commit 1109650

Browse files
authored
Add an "api key" scheme (#440)
1 parent 3ed2254 commit 1109650

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/helpers/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const applicationKey = getEnv("DISCORD_PUBLIC_KEY");
2424
export const applicationId = getEnv("DISCORD_APP_ID");
2525
export const guildId = getEnv("GUILD_ID");
2626
export const discordToken = getEnv("DISCORD_HASH");
27+
export const reactibotApiKey = getEnv("REACTIBOT_API_KEY", true);
2728
export const gitHubReadToken = getEnv("GH_READ_TOKEN", true);
2829
export const amplitudeKey = getEnv("AMPLITUDE_KEY", true);
2930
export const openAiKey = getEnv("OPENAI_KEY", true);

src/server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "./features/jobs-moderation/job-mod-helpers.js";
1111
import { compressLineBreaks } from "./helpers/string.js";
1212
import { constructDiscordLink } from "./helpers/discord.js";
13+
import { reactibotApiKey } from "./helpers/env.js";
1314

1415
const fastify = Fastify({ logger: true });
1516

@@ -22,6 +23,14 @@ const openApiConfig = {
2223
version: "1.0.0",
2324
},
2425
components: {
26+
securitySchemes: {
27+
apiKey: {
28+
type: "apiKey",
29+
name: "api-key",
30+
in: "header",
31+
},
32+
},
33+
security: [{ apiKey: [] }],
2534
schemas: {
2635
PaginationParams: {
2736
type: "object",
@@ -118,6 +127,15 @@ const openApiConfig = {
118127
},
119128
};
120129

130+
fastify.addHook("onRequest", async (request, reply) => {
131+
const apiKey = request.headers["api-key"];
132+
console.log("onreq");
133+
if (apiKey !== reactibotApiKey) {
134+
reply.code(401).send({ error: "Unauthorized" });
135+
return;
136+
}
137+
});
138+
121139
try {
122140
Object.entries(openApiConfig.components.schemas).forEach(([k, schema]) => {
123141
fastify.addSchema({ ...schema, $id: k });

0 commit comments

Comments
 (0)