Skip to content

Commit 8a92517

Browse files
authored
Cache apple public key for the case it fails to fetch again (#5848)
1 parent 50f1e8e commit 8a92517

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Adapters/Auth/apple.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@ const jwt = require('jsonwebtoken');
55

66
const TOKEN_ISSUER = 'https://appleid.apple.com';
77

8+
let currentKey;
9+
810
const getApplePublicKey = async () => {
9-
const data = await httpsRequest.get('https://appleid.apple.com/auth/keys');
11+
let data;
12+
try {
13+
data = await httpsRequest.get('https://appleid.apple.com/auth/keys');
14+
} catch (e) {
15+
if (currentKey) {
16+
return currentKey;
17+
}
18+
throw e;
19+
}
20+
1021
const key = data.keys[0];
1122

1223
const pubKey = new NodeRSA();
1324
pubKey.importKey(
1425
{ n: Buffer.from(key.n, 'base64'), e: Buffer.from(key.e, 'base64') },
1526
'components-public'
1627
);
17-
return pubKey.exportKey(['public']);
28+
currentKey = pubKey.exportKey(['public']);
29+
return currentKey;
1830
};
1931

2032
const verifyIdToken = async (token, clientID) => {

0 commit comments

Comments
 (0)