forked from tom-smith-okta/node-lambda-oauth2-jwt-authorizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey-cache.js
More file actions
62 lines (54 loc) · 1.66 KB
/
key-cache.js
File metadata and controls
62 lines (54 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const CACHE_TTL_IN_MS = 3 * 60 * 60 * 1000;
const keyCache = {
// Azure 全球版的密钥缓存
azure: {},
// Azure 中国版的密钥缓存
azureCN: {},
};
/**
* 从缓存获取密钥,如果缓存未命中则通过回调函数获取
* @param {string} provider - 提供商标识('azure' 或 'azureCN')
* @param {string} kid - 密钥ID
* @param {Function} fetchCallback - 缓存未命中时用于获取密钥的回调函数
* @returns {Promise<string>} - 返回密钥
*/
const getKey = async (provider, kid, fetchCallback) => {
// 检查参数
if (!provider || !kid || typeof fetchCallback !== "function") {
throw new Error("无效的参数");
}
// 检查提供商是否有效
if (!keyCache[provider]) {
throw new Error(`未知的提供商: ${provider}`);
}
// 检查缓存是否命中
if (
keyCache[provider][kid] &&
keyCache[provider][kid].expiresAt > Date.now()
) {
console.log(`缓存命中: ${provider} 密钥 ${kid}`);
return keyCache[provider][kid].publicKey;
}
// 缓存未命中,调用回调函数获取密钥
console.log(`缓存未命中: ${provider} 密钥 ${kid},从远程获取`);
try {
const signingKey = await fetchCallback(kid);
// 更新缓存
keyCache[provider][kid] = {
publicKey: signingKey,
expiresAt: Date.now() + CACHE_TTL_IN_MS,
};
console.log(
`已缓存 ${provider} 密钥 ${kid},有效期至 ${new Date(
keyCache[provider][kid].expiresAt
).toISOString()}`
);
return signingKey;
} catch (error) {
console.error(`获取 ${provider} 密钥失败: ${error.message}`);
throw error;
}
};
module.exports = {
getKey,
};