Skip to content

Commit

Permalink
fix: HASSU-977-api-sivujen-autentikaatio (#357)
Browse files Browse the repository at this point in the history
* add validation for testapi calls
  • Loading branch information
kettunju committed Sep 9, 2022
1 parent dda6393 commit 0ec08a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/pages/api/test/[oid]/nahtavillaolomenneisyyteen.dev.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next";
import { projektiDatabase } from "../../../../../backend/src/database/projektiDatabase";
import { validateCredentials } from "../../../../util/basicAuthentication";
import { validateApiCredentials } from "../../../../util/basicAuthentication";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
let environment = process.env.ENVIRONMENT;
if ((environment == "dev" || environment == "test") && !(await validateCredentials(req.headers.authorization))) {
if ((environment == "dev" || environment == "test") && !(await validateApiCredentials(req.headers.authorization))) {
res.status(401);
res.setHeader("www-authenticate", "Basic");

Expand Down
13 changes: 10 additions & 3 deletions src/util/basicAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { getCredentials } from "./apiUtil";
import { parameterStore } from "./parameterStore";

export function createAuthorizationHeader(username: string, password: string) {
return "Basic " + Buffer.from(username + ":" + password, "binary").toString("base64");
}

export async function validateCredentials(authorization: string | undefined): Promise<boolean> {
let configuredCredentials: string[] | undefined = (
await parameterStore.getParameter("/IlmoitustauluSyoteCredentials")
)?.split("\n");
let configuredCredentials: string[] | undefined = (await parameterStore.getParameter("/IlmoitustauluSyoteCredentials"))?.split("\n");
if (!configuredCredentials || !authorization) {
return false;
}
Expand All @@ -23,3 +22,11 @@ export async function validateCredentials(authorization: string | undefined): Pr
}
return false;
}

export async function validateApiCredentials(authorization: string | undefined): Promise<boolean> {
let configuredCredentials: { username: string; password: string } = await getCredentials();
if (!configuredCredentials || !authorization) {
return false;
}
return authorization == createAuthorizationHeader(configuredCredentials.username, configuredCredentials.password);
}

0 comments on commit 0ec08a4

Please sign in to comment.