diff --git a/src/authorization.ts b/src/authorization.ts index a4d1932..e673197 100644 --- a/src/authorization.ts +++ b/src/authorization.ts @@ -4,11 +4,20 @@ import { APIKey } from "./models/api_key"; const HASHER = new SHA3(256); +const validKeys = new Map(); + export async function getAPIKey(key: string): Promise { + 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; }