Add auth to public API

This commit is contained in:
Kalle 2024-01-13 18:37:33 +02:00
parent 2699d1460e
commit 58bbdccee8
5 changed files with 27 additions and 4 deletions

View File

@ -0,0 +1,11 @@
const apiTokens = process.env["PUBLIC_API_TOKENS"]?.split(",") ?? [];
export function requireBearerAuth(req: Request) {
const authHeader = req.headers.get("Authorization");
if (!authHeader) {
throw new Response("Missing Authorization header", { status: 401 });
}
const token = authHeader.replace("Bearer ", "");
if (!apiTokens.includes(token)) {
throw new Response("Invalid token", { status: 401 });
}
}

View File

@ -7,12 +7,15 @@ import type { GetTournamentMatchResponse } from "../schema";
import { i18next } from "~/modules/i18n";
import { jsonArrayFrom } from "kysely/helpers/sqlite";
import { resolveMapList } from "~/features/tournament-bracket/core/mapList.server";
import { requireBearerAuth } from "../api-public-utils.server";
const paramsSchema = z.object({
id,
});
export const loader = async ({ params }: LoaderFunctionArgs) => {
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
requireBearerAuth(request);
const t = await i18next.getFixedT("en", ["game-misc"]);
const { id } = parseParams({
params,

View File

@ -7,12 +7,15 @@ import type { GetTournamentTeamsResponse } from "../schema";
import { databaseTimestampToDate } from "~/utils/dates";
import { jsonArrayFrom } from "kysely/helpers/sqlite";
import { i18next } from "~/modules/i18n";
import { requireBearerAuth } from "../api-public-utils.server";
const paramsSchema = z.object({
id,
});
export const loader = async ({ params }: LoaderFunctionArgs) => {
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
requireBearerAuth(request);
const t = await i18next.getFixedT("en", ["game-misc"]);
const { id } = parseParams({
params,

View File

@ -7,12 +7,15 @@ import type { GetTournamentResponse } from "../schema";
import { databaseTimestampToDate } from "~/utils/dates";
import { HACKY_resolvePicture } from "~/features/tournament/tournament-utils";
import { jsonArrayFrom } from "kysely/helpers/sqlite";
import { requireBearerAuth } from "../api-public-utils.server";
const paramsSchema = z.object({
id,
});
export const loader = async ({ params }: LoaderFunctionArgs) => {
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
requireBearerAuth(request);
const { id } = parseParams({ params, schema: paramsSchema });
const tournament = notFoundIfFalsy(

View File

@ -6,12 +6,15 @@ import { notFoundIfFalsy, parseParams } from "~/utils/remix";
import type { GetUserResponse } from "../schema";
import { jsonArrayFrom } from "kysely/helpers/sqlite";
import { i18next } from "~/modules/i18n";
import { requireBearerAuth } from "../api-public-utils.server";
const paramsSchema = z.object({
identifier: z.string(),
});
export const loader = async ({ params }: LoaderFunctionArgs) => {
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
requireBearerAuth(request);
const t = await i18next.getFixedT("en", ["weapons"]);
const { identifier } = parseParams({ params, schema: paramsSchema });