diff --git a/packages/core/src/social-providers/apple.ts b/packages/core/src/social-providers/apple.ts index d41b9319ce..cb6dd9c370 100644 --- a/packages/core/src/social-providers/apple.ts +++ b/packages/core/src/social-providers/apple.ts @@ -111,30 +111,34 @@ export const apple = (options: AppleOptions) => { if (options.verifyIdToken) { return options.verifyIdToken(token, nonce); } - const decodedHeader = decodeProtectedHeader(token); - const { kid, alg: jwtAlg } = decodedHeader; - if (!kid || !jwtAlg) return false; - const publicKey = await getApplePublicKey(kid); - const { payload: jwtClaims } = await jwtVerify(token, publicKey, { - algorithms: [jwtAlg], - issuer: "https://appleid.apple.com", - audience: - options.audience && options.audience.length - ? options.audience - : options.appBundleIdentifier - ? options.appBundleIdentifier - : options.clientId, - maxTokenAge: "1h", - }); - ["email_verified", "is_private_email"].forEach((field) => { - if (jwtClaims[field] !== undefined) { - jwtClaims[field] = Boolean(jwtClaims[field]); + try { + const decodedHeader = decodeProtectedHeader(token); + const { kid, alg: jwtAlg } = decodedHeader; + if (!kid || !jwtAlg) return false; + const publicKey = await getApplePublicKey(kid); + const { payload: jwtClaims } = await jwtVerify(token, publicKey, { + algorithms: [jwtAlg], + issuer: "https://appleid.apple.com", + audience: + options.audience && options.audience.length + ? options.audience + : options.appBundleIdentifier + ? options.appBundleIdentifier + : options.clientId, + maxTokenAge: "1h", + }); + ["email_verified", "is_private_email"].forEach((field) => { + if (jwtClaims[field] !== undefined) { + jwtClaims[field] = Boolean(jwtClaims[field]); + } + }); + if (nonce && jwtClaims.nonce !== nonce) { + return false; } - }); - if (nonce && jwtClaims.nonce !== nonce) { + return !!jwtClaims; + } catch { return false; } - return !!jwtClaims; }, refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken diff --git a/packages/core/src/social-providers/google.ts b/packages/core/src/social-providers/google.ts index 0b60f4fb80..60f6632ff2 100644 --- a/packages/core/src/social-providers/google.ts +++ b/packages/core/src/social-providers/google.ts @@ -131,22 +131,26 @@ export const google = (options: GoogleOptions) => { // Verify JWT integrity // See https://developers.google.com/identity/sign-in/web/backend-auth#verify-the-integrity-of-the-id-token - const { kid, alg: jwtAlg } = decodeProtectedHeader(token); - if (!kid || !jwtAlg) return false; + try { + const { kid, alg: jwtAlg } = decodeProtectedHeader(token); + if (!kid || !jwtAlg) return false; - const publicKey = await getGooglePublicKey(kid); - const { payload: jwtClaims } = await jwtVerify(token, publicKey, { - algorithms: [jwtAlg], - issuer: ["https://accounts.google.com", "accounts.google.com"], - audience: options.clientId, - maxTokenAge: "1h", - }); + const publicKey = await getGooglePublicKey(kid); + const { payload: jwtClaims } = await jwtVerify(token, publicKey, { + algorithms: [jwtAlg], + issuer: ["https://accounts.google.com", "accounts.google.com"], + audience: options.clientId, + maxTokenAge: "1h", + }); - if (nonce && jwtClaims.nonce !== nonce) { + if (nonce && jwtClaims.nonce !== nonce) { + return false; + } + + return true; + } catch { return false; } - - return true; }, async getUserInfo(token) { if (options.getUserInfo) {