Skip to content

Commit

Permalink
Cache valid API keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Dec 12, 2023
1 parent cce1794 commit ee2b033
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import { APIKey } from "./models/api_key";

const HASHER = new SHA3(256);

const validKeys = new Map<string, APIKey>();

export async function getAPIKey(key: string): Promise<APIKey | null> {
const cachedKey = validKeys.get(key);
if (cachedKey !== undefined) {
return cachedKey;
}
HASHER.update(key);
const hashedKey = HASHER.digest("hex");
const apiKey = await APIKey.findOne({ where: { hashed_key: hashedKey } });
HASHER.reset();
if (apiKey !== null) {
validKeys.set(key, apiKey);
}
return apiKey;
}

Expand Down

0 comments on commit ee2b033

Please sign in to comment.