@@ -19,11 +19,38 @@ interface AppleTokenPayload {
1919 auth_time ?: number ; // 인증 시간
2020}
2121
22- // Apple 설정 불러오기
23- const teamId = process . env . APPLE_TEAM_ID ;
24- const clientId = process . env . APPLE_CLIENT_ID ;
25- const keyId = process . env . APPLE_KEY_ID ;
26- const privateKey = ( process . env . APPLE_PRIVATE_KEY || "" ) . replace ( / \\ n / g, "\n" ) ;
22+ function getAppleConfiguration ( ) {
23+ const teamId = process . env . APPLE_TEAM_ID ;
24+ const clientId = process . env . APPLE_CLIENT_ID ;
25+ const keyId = process . env . APPLE_KEY_ID ;
26+ const privateKey = ( process . env . APPLE_PRIVATE_KEY || "" ) . replace ( / \\ n / g, "\n" ) ;
27+ const configs = {
28+ APPLE_TEAM_ID : teamId ,
29+ APPLE_CLIENT_ID : clientId ,
30+ APPLE_KEY_ID : keyId ,
31+ APPLE_PRIVATE_KEY : privateKey
32+ } ;
33+ const missingKeys = Object . entries ( configs )
34+ . filter ( ( [ , value ] ) => ! value )
35+ . map ( ( [ key ] ) => key ) ;
36+
37+ if ( 0 < missingKeys . length ) {
38+ console . error ( "Missing Apple configuration" , {
39+ missingKeys
40+ } ) ;
41+ throw new HttpsError (
42+ 'internal' ,
43+ `Missing Apple configuration for: ${ missingKeys . join ( ", " ) } `
44+ ) ;
45+ }
46+
47+ return {
48+ teamId : teamId ! ,
49+ clientId : clientId ! ,
50+ keyId : keyId ! ,
51+ privateKey : privateKey !
52+ } ;
53+ }
2754
2855export const requestAppleCustomToken = onCall ( {
2956 cors : true ,
@@ -37,11 +64,6 @@ export const requestAppleCustomToken = onCall({
3764 throw new HttpsError ( 'invalid-argument' , 'ID token and authorization code are required' ) ;
3865 }
3966
40- // Apple 설정 불러오기
41- if ( ! teamId || ! clientId || ! keyId || ! privateKey ) {
42- throw new HttpsError ( 'internal' , 'Missing Apple configuration' ) ;
43- }
44-
4567 // // 1. Verify and decode the Apple ID token
4668 let decodedToken : AppleTokenPayload ;
4769 try {
@@ -143,10 +165,6 @@ export const requestAppleRefreshToken = onCall({
143165 if ( ! authorizationCode || ! uid ) {
144166 throw new HttpsError ( "invalid-argument" , "Authorization code and uid are required" ) ;
145167 }
146-
147- if ( ! teamId || ! clientId || ! keyId || ! privateKey ) {
148- throw new HttpsError ( "internal" , "Missing Apple configuration" ) ;
149- }
150168
151169 const refreshToken = await requestAppleRefreshTokenHelper ( authorizationCode ) ;
152170 console . log ( "appleRefreshToken:" , refreshToken ) ;
@@ -197,13 +215,7 @@ export const refreshAppleAccessToken = onCall({
197215 }
198216
199217 console . log ( "Successfully retrieved refresh token from Firestore" ) ;
200-
201- if ( ! teamId || ! clientId || ! keyId || ! privateKey ) {
202- throw new HttpsError (
203- "internal" ,
204- "Missing Apple configuration environment variables."
205- ) ;
206- }
218+ const { teamId, clientId, keyId, privateKey } = getAppleConfiguration ( ) ;
207219
208220 // Create client_secret JWT
209221 const clientSecret = jwt . sign ( { } , privateKey , {
@@ -265,15 +277,12 @@ export const revokeAppleAccessToken = onCall({
265277
266278 try {
267279 const { token } = request . data ;
280+ const { teamId, clientId, keyId, privateKey } = getAppleConfiguration ( ) ;
268281
269282 if ( ! token ) {
270283 throw new HttpsError ( "invalid-argument" , "Token is required" ) ;
271284 }
272285
273- if ( ! teamId || ! clientId || ! keyId || ! privateKey ) {
274- throw new HttpsError ( "internal" , "Missing Apple configuration" ) ;
275- }
276-
277286 // JWT 생성
278287 const clientSecret = jwt . sign ( { } , privateKey , {
279288 algorithm : "ES256" ,
@@ -323,10 +332,7 @@ export const revokeAppleAccessToken = onCall({
323332} ) ;
324333
325334export async function requestAppleRefreshTokenHelper ( authorizationCode : string ) : Promise < string > {
326- // Apple 설정 불러오기
327- if ( ! teamId || ! clientId || ! keyId || ! privateKey ) {
328- throw new HttpsError ( 'internal' , 'Missing Apple configuration' ) ;
329- }
335+ const { teamId, clientId, keyId, privateKey } = getAppleConfiguration ( ) ;
330336
331337 // JWT 생성
332338 const clientSecret = jwt . sign ( { } , privateKey , {
0 commit comments