Skip to content

Commit

Permalink
basicauth helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
VINXIS committed Nov 1, 2024
1 parent f271335 commit 2c8cc03
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
7 changes: 2 additions & 5 deletions DiscordBot/commands/tournaments/matchup/stopAutoLobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
},
});

Expand Down
7 changes: 2 additions & 5 deletions Server/api/routes/matchup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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),
},
});

Expand Down
7 changes: 2 additions & 5 deletions Server/api/routes/referee/bancho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -40,10 +41,6 @@ refereeBanchoRouter.$post<object, TournamentAuthenticatedState>("/: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,
Expand All @@ -52,7 +49,7 @@ refereeBanchoRouter.$post<object, TournamentAuthenticatedState>("/:tournamentID/
{
headers: {
"Content-Type": "application/json",
"Authorization": authHeader,
"Authorization": basicAuth(config.interOpAuth),
},
});

Expand Down
3 changes: 2 additions & 1 deletion Server/cron/cronFunctions/qualifierMatchup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CronJobData[]> {
// Get all tournament registration ends
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions Server/cron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Server/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function basicAuth ({ username, password }: { username: string; password: string }): string {
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
}

0 comments on commit 2c8cc03

Please sign in to comment.