Skip to content

Commit

Permalink
Merge pull request #99 from Carifio24/cache-hashed-keys
Browse files Browse the repository at this point in the history
Cache valid API keys
  • Loading branch information
Carifio24 authored Dec 12, 2023
2 parents cce1794 + ee2b033 commit 308e29d
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 308e29d

Please sign in to comment.