@@ -4,6 +4,11 @@ const Bluebird = require('bluebird');
44const Boom = require ( '@hapi/boom' ) ;
55const { request : httpRequest } = require ( 'undici' ) ;
66
7+ const {
8+ ERROR_OAUTH_APPLE_VALIDATE_CODE ,
9+ ERROR_OAUTH_APPLE_VERIFY_PROFILE ,
10+ } = require ( '../../../constants' ) ;
11+
712// @todo more options from config
813const jwksClient = getJwksClient ( {
914 jwksUri : 'https://appleid.apple.com/auth/keys' ,
@@ -151,13 +156,24 @@ function getProvider(options, server) {
151156 } ;
152157}
153158
154- async function upgradeAppleCode ( params ) {
159+ async function upgradeAppleCode ( { params, log } ) {
155160 const { providerSettings, code, query, redirectUrl } = params ;
156161 const { profile } = providerSettings . provider ;
157162
163+ let tokenResponse ;
164+
165+ try {
166+ tokenResponse = await validateGrantCode ( providerSettings , code , redirectUrl ) ;
167+ } catch ( error ) {
168+ log . error ( Boom . internal ( error . body ?. error , undefined , error . statusCode ) ) ;
169+
170+ throw ERROR_OAUTH_APPLE_VALIDATE_CODE ;
171+ }
172+
173+ let credentials ;
174+
158175 try {
159- const tokenResponse = await validateGrantCode ( providerSettings , code , redirectUrl ) ;
160- const credentials = await profile . call (
176+ credentials = await profile . call (
161177 providerSettings ,
162178 {
163179 query,
@@ -167,11 +183,13 @@ async function upgradeAppleCode(params) {
167183 } ,
168184 tokenResponse
169185 ) ;
170-
171- return credentials ;
172186 } catch ( error ) {
173- throw Boom . internal ( error . body ?. error , undefined , error . statusCode ) ;
187+ log . error ( Boom . internal ( error . body ?. error , undefined , error . statusCode ) ) ;
188+
189+ throw ERROR_OAUTH_APPLE_VERIFY_PROFILE ;
174190 }
191+
192+ return credentials ;
175193}
176194
177195module . exports = {
0 commit comments