forked from monkeytypegame/monkeytype
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'monkeytypegame:master' into master
- Loading branch information
Showing
63 changed files
with
2,628 additions
and
1,329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
Oops, something went wrong.