From ee2b033ef4691396ff0b1b64023139a543808e98 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Tue, 12 Dec 2023 18:10:59 -0500 Subject: [PATCH] Cache valid API keys. --- src/authorization.ts | 9 +++++++++ 1 file changed, 9 insertions(+) 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; }