Skip to content

Commit

Permalink
added auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienR1 committed Oct 31, 2023
1 parent f5d937a commit f2eed56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/auth/argon2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { config } from "../config.js";
import * as argon2 from "./argon2.js";

const passwordHash =
"$argon2id$v=19$m=65536,t=2,p=1$53yGw9x/71TwPK/jEX056kYMTLq+DIFAkCg2wIo+N7A$VGxk8EPwP8sLib1NDoo9YNh1eKLNCr2sy3uZywh5ayk";

describe("argon2", () => {
let authKey: string | undefined = "";

beforeEach(() => (authKey = config.authKey));
afterEach(() => (config["authKey"] = authKey));

test("it should skip auth check when no auth-key is passed in", () => {
config["authKey"] = "";
const request = new Request("http://localhost", {
headers: { Authorization: "Bearer auth-key" },
});

const response = argon2.beforeHandle(request);
expect(response).toBeUndefined();
});

test("it should return 'unauthorized' on invalid password", () => {
config["authKey"] = passwordHash;
const request = new Request("http://localhost", { headers: { Authorization: "Bearer pwd" } });

const response = argon2.beforeHandle(request);
expect(response?.status).toBe(401);
});

test("it should allow valid passwords", () => {
config["authKey"] = passwordHash;
const request = new Request("http://localhost", {
headers: { Authorization: "Bearer password" },
});

const response = argon2.beforeHandle(request);
expect(response?.status).toBeUndefined();
});
});
4 changes: 1 addition & 3 deletions src/auth/bearer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const PREFIX = "Bearer";
const REALM = "sign";

export const NoAuthorization = toText("Unauthorized", 400, new Headers({ "WWW-Authenticate": `${PREFIX} realm="${REALM}"` }));

export const Unauthorized = toText("Unauthorized", 400, new Headers({ "WWW-Authenticate": `${PREFIX} error="invalid_token"` }));

export const Unauthorized = toText("Unauthorized", 401, new Headers({ "WWW-Authenticate": `${PREFIX} error="invalid_token"` }));
export const InvalidRequest = toText("Bad Request", 400, new Headers({ "WWW-Authenticate": `${PREFIX} error="invalid_request"` }));

export function getBearerToken(headerToken: string) {
Expand Down

0 comments on commit f2eed56

Please sign in to comment.