Skip to content

Commit

Permalink
⚡️ add resilience to errors (#183)
Browse files Browse the repository at this point in the history
* ⚡️ add resilience to errors

* 🔖 1.7.8

* Update index.js

* Update index.js
  • Loading branch information
a-tokyo authored Nov 6, 2024
1 parent 7cad3a6 commit 1938db7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apple-signin-auth",
"version": "1.7.7",
"version": "1.7.8",
"description": " Apple signin for node.",
"author": {
"name": "Ahmed Tarek",
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,26 @@ const _getIdTokenApplePublicKey = async (
header: string,
cb: (?Error, ?string) => any,
): Function => {
/** error if found */
let error;
// attempt fetching from cache
if (APPLE_KEYS_CACHE[header.kid]) {
return cb(null, APPLE_KEYS_CACHE[header.kid]);
}
// fetch and cache current Apple public keys
await _getApplePublicKeys();
try {
// fetch and cache current Apple public keys
await _getApplePublicKeys();
} catch (err) {
// key was not fetched - highly unlikely, means apple is having issues or somebody faked the JSON
// we will still try to get the key from the cache
error = err;
}
// attempt fetching from cache
if (APPLE_KEYS_CACHE[header.kid]) {
return cb(null, APPLE_KEYS_CACHE[header.kid]);
}
// key was not fetched - highly unlikely, means apple is having issues or somebody faked the JSON
return cb(new Error('input error: Invalid id token public key id'));
return cb(error || new Error('input error: Invalid id token public key id'));
};

/** Verifies an Apple id token */
Expand Down

0 comments on commit 1938db7

Please sign in to comment.