Skip to content

Commit

Permalink
Merge branch 'monkeytypegame:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcosatc147 authored Aug 13, 2024
2 parents eae2fc0 + 19cef8b commit c05dc72
Show file tree
Hide file tree
Showing 63 changed files with 2,628 additions and 1,329 deletions.
37 changes: 37 additions & 0 deletions backend/__tests__/__testData__/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Configuration } from "@monkeytype/shared-types";
import { randomBytes } from "crypto";
import { hash } from "bcrypt";
import { ObjectId } from "mongodb";
import { base64UrlEncode } from "../../src/utils/misc";
import * as ApeKeyDal from "../../src/dal/ape-keys";

export async function mockAuthenticateWithApeKey(
uid: string,
config: Configuration
): Promise<string> {
if (!config.apeKeys.acceptKeys)
throw Error("config.apeKeys.acceptedKeys needs to be set to true");
const { apeKeyBytes, apeKeySaltRounds } = config.apeKeys;

const apiKey = randomBytes(apeKeyBytes).toString("base64url");
const saltyHash = await hash(apiKey, apeKeySaltRounds);

const apeKey: MonkeyTypes.ApeKeyDB = {
_id: new ObjectId(),
name: "bob",
enabled: true,
uid,
hash: saltyHash,
createdOn: Date.now(),
modifiedOn: Date.now(),
lastUsedOn: -1,
useCount: 0,
};

const apeKeyId = new ObjectId().toHexString();

vi.spyOn(ApeKeyDal, "getApeKey").mockResolvedValue(apeKey);
vi.spyOn(ApeKeyDal, "updateLastUsedOn").mockResolvedValue();

return base64UrlEncode(`${apeKeyId}.${apiKey}`);
}
Loading

0 comments on commit c05dc72

Please sign in to comment.