Cache apple public key for the case it fails to fetch again (#5848)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-07-25 10:20:28 -07:00
committed by GitHub
parent 50f1e8eb77
commit 8a925177e1

View File

@@ -5,8 +5,19 @@ const jwt = require('jsonwebtoken');
const TOKEN_ISSUER = 'https://appleid.apple.com'; const TOKEN_ISSUER = 'https://appleid.apple.com';
let currentKey;
const getApplePublicKey = async () => { const getApplePublicKey = async () => {
const data = await httpsRequest.get('https://appleid.apple.com/auth/keys'); let data;
try {
data = await httpsRequest.get('https://appleid.apple.com/auth/keys');
} catch (e) {
if (currentKey) {
return currentKey;
}
throw e;
}
const key = data.keys[0]; const key = data.keys[0];
const pubKey = new NodeRSA(); const pubKey = new NodeRSA();
@@ -14,7 +25,8 @@ const getApplePublicKey = async () => {
{ n: Buffer.from(key.n, 'base64'), e: Buffer.from(key.e, 'base64') }, { n: Buffer.from(key.n, 'base64'), e: Buffer.from(key.e, 'base64') },
'components-public' 'components-public'
); );
return pubKey.exportKey(['public']); currentKey = pubKey.exportKey(['public']);
return currentKey;
}; };
const verifyIdToken = async (token, clientID) => { const verifyIdToken = async (token, clientID) => {