From 8a925177e1bc86c1d0a269d08085e71136f0e5af Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Thu, 25 Jul 2019 10:20:28 -0700 Subject: [PATCH] Cache apple public key for the case it fails to fetch again (#5848) --- src/Adapters/Auth/apple.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Adapters/Auth/apple.js b/src/Adapters/Auth/apple.js index f8ae9522..cf1d6f17 100644 --- a/src/Adapters/Auth/apple.js +++ b/src/Adapters/Auth/apple.js @@ -5,8 +5,19 @@ const jwt = require('jsonwebtoken'); const TOKEN_ISSUER = 'https://appleid.apple.com'; +let currentKey; + 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 pubKey = new NodeRSA(); @@ -14,7 +25,8 @@ const getApplePublicKey = async () => { { n: Buffer.from(key.n, 'base64'), e: Buffer.from(key.e, 'base64') }, 'components-public' ); - return pubKey.exportKey(['public']); + currentKey = pubKey.exportKey(['public']); + return currentKey; }; const verifyIdToken = async (token, clientID) => {