From 2c8cc03193eb987ae5cccd792a46c66acd425b91 Mon Sep 17 00:00:00 2001 From: VINXIS Date: Fri, 1 Nov 2024 17:45:09 -0600 Subject: [PATCH] basicauth helper function --- DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts | 7 ++----- Server/api/routes/matchup.ts | 7 ++----- Server/api/routes/referee/bancho.ts | 7 ++----- Server/cron/cronFunctions/qualifierMatchup.ts | 3 ++- Server/cron/index.ts | 5 +++-- Server/utils/auth.ts | 3 +++ 6 files changed, 14 insertions(+), 18 deletions(-) create mode 100644 Server/utils/auth.ts diff --git a/DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts b/DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts index cc24588267..96bdaa5797 100644 --- a/DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts +++ b/DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts @@ -7,6 +7,7 @@ import { extractParameter } from "../../../functions/parameterFunctions"; import respond from "../../../functions/respond"; import { securityChecks } from "../../../functions/tournamentFunctions/securityChecks"; import { post } from "../../../../Server/utils/fetch"; +import { basicAuth } from "../../../../Server/utils/auth"; async function run (m: Message | ChatInputCommandInteraction) { if (m instanceof ChatInputCommandInteraction) @@ -32,15 +33,11 @@ async function run (m: Message | ChatInputCommandInteraction) { } const baseUrl = matchup.baseURL; - - const { username, password } = config.interOpAuth; - const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`; - const data = await post(`${baseUrl}/api/bancho/stopAutoLobby`, { matchupID: ID, }, { headers: { - Authentication: authHeader, + Authentication: basicAuth(config.interOpAuth), }, }); diff --git a/Server/api/routes/matchup.ts b/Server/api/routes/matchup.ts index 9d0c61335e..4787828c84 100644 --- a/Server/api/routes/matchup.ts +++ b/Server/api/routes/matchup.ts @@ -26,6 +26,7 @@ import { createHash } from "crypto"; import { Tournament } from "../../../Models/tournaments/tournament"; import { publish } from "../../functions/centrifugo"; import { get } from "../../utils/fetch"; +import { basicAuth } from "../../utils/auth"; const matchupRouter = new CorsaceRouter(); @@ -247,14 +248,10 @@ matchupRouter.$get("/:matchupID/bancho/:endpoint", async (ctx) => { return; } - // Assuming config.interOpAuth is an object with username and password - const { username, password } = config.interOpAuth; - const basicAuth = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`; - try { const response = await get(`${matchup.baseURL ?? config.banchoBot.publicUrl}/api/bancho/stream/${matchup.ID}/${endpoint}`, { headers: { - "Authorization": basicAuth, + "Authorization": basicAuth(config.interOpAuth), }, }); diff --git a/Server/api/routes/referee/bancho.ts b/Server/api/routes/referee/bancho.ts index ee751d4ba4..bdfd17517c 100644 --- a/Server/api/routes/referee/bancho.ts +++ b/Server/api/routes/referee/bancho.ts @@ -6,6 +6,7 @@ import { TournamentRoleType } from "../../../../Interfaces/tournament"; import { hasRoles, validateTournament } from "../../../middleware/tournament"; import { TournamentAuthenticatedState } from "koa"; import { post } from "../../../utils/fetch"; +import { basicAuth } from "../../../utils/auth"; const refereeBanchoRouter = new CorsaceRouter(); @@ -40,10 +41,6 @@ refereeBanchoRouter.$post("/:tournamentID/ try { const url = `${matchup.baseURL ?? config.banchoBot.publicUrl}/api/bancho/referee/${matchup.ID}/${ctx.request.body.endpoint}`; - - const { username, password } = config.interOpAuth; - const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`; - const response = await post(url, { ...ctx.request.body, endpoint: undefined, @@ -52,7 +49,7 @@ refereeBanchoRouter.$post("/:tournamentID/ { headers: { "Content-Type": "application/json", - "Authorization": authHeader, + "Authorization": basicAuth(config.interOpAuth), }, }); diff --git a/Server/cron/cronFunctions/qualifierMatchup.ts b/Server/cron/cronFunctions/qualifierMatchup.ts index 4eee08b7c6..5849767b9c 100644 --- a/Server/cron/cronFunctions/qualifierMatchup.ts +++ b/Server/cron/cronFunctions/qualifierMatchup.ts @@ -2,6 +2,7 @@ import { CronJobData, CronJobType } from "../../../Interfaces/cron"; import { Matchup, preInviteTime } from "../../../Models/tournaments/matchup"; import { config } from "node-config-ts"; import { post } from "../../utils/fetch"; +import { basicAuth } from "../../utils/auth"; async function initialize (): Promise { // Get all tournament registration ends @@ -38,7 +39,7 @@ async function execute (job: CronJobData) { }, { headers: { - Authorization: "Basic " + Buffer.from(`${config.interOpAuth.username}:${config.interOpAuth.password}`).toString("base64"), + Authorization: basicAuth(config.interOpAuth), }, }); if (data.success) diff --git a/Server/cron/index.ts b/Server/cron/index.ts index 103e812d2c..74e6944775 100644 --- a/Server/cron/index.ts +++ b/Server/cron/index.ts @@ -3,6 +3,7 @@ import { CronJob } from "cron"; import cronFunctions from "./cronFunctions"; import { CronJobData, CronJobType } from "../../Interfaces/cron"; import { post } from "../utils/fetch"; +import { basicAuth } from "../utils/auth"; class Cron { @@ -28,7 +29,7 @@ class Cron { }, { headers: { - Authorization: "Basic " + Buffer.from(`${config.interOpAuth.username}:${config.interOpAuth.password}`).toString("base64"), + Authorization: basicAuth(config.interOpAuth), }, }); if (!data.success) @@ -52,7 +53,7 @@ class Cron { }, { headers: { - Authorization: "Basic " + Buffer.from(`${config.interOpAuth.username}:${config.interOpAuth.password}`).toString("base64"), + Authorization: basicAuth(config.interOpAuth), }, }); if (!data.success) diff --git a/Server/utils/auth.ts b/Server/utils/auth.ts new file mode 100644 index 0000000000..f0200f14bf --- /dev/null +++ b/Server/utils/auth.ts @@ -0,0 +1,3 @@ +export function basicAuth ({ username, password }: { username: string; password: string }): string { + return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`; +} \ No newline at end of file