Skip to content

Commit

Permalink
[FIX] conflict resolve develop to main
Browse files Browse the repository at this point in the history
  • Loading branch information
jokj624 committed Jan 29, 2023
2 parents 22f5212 + 4d80140 commit 1a2c510
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion functions/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
./package-lock.json
./package-lock.json
*.p8
83 changes: 83 additions & 0 deletions functions/lib/appleAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const axios = require('axios');
const jwt = require('jsonwebtoken');
const functions = require("firebase-functions");
const slackAPI = require("../middlewares/slackAPI");
const fs = require('fs').promises;
const { resolve } = require('path');
const qs = require('qs');

const appleBaseURL = "https://appleid.apple.com";

const getPrivateKey = async () => {
return (await fs.readFile(resolve(__dirname, `../${process.env.APPLE_PRIVATE_KEY_FILE}`), 'utf8')).toString();
}

const header = {
kid: process.env.APPLE_KEY_ID,
};

const payload = {
iss: process.env.APPLE_TEAM_ID,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 15777000,
aud: appleBaseURL,
sub: process.env.APPLE_CLIENT_ID,
};

/**
* @desc apple 서버에 refresh token 요청
* @param {String} appleCode
*/
const getAppleRefreshToken = async (appleCode) => {
const clientSecret = jwt.sign(payload, await getPrivateKey(), {
algorithm: 'ES256',
header
});

const data = {
'client_id': process.env.APPLE_CLIENT_ID,
'client_secret': clientSecret,
'code': appleCode,
'grant_type': 'authorization_code'
};

try {
const response = await axios.post(`${appleBaseURL}/auth/token`, qs.stringify(data));

return response.data.refresh_token;
} catch (error) {
functions.logger.error(`[ERROR] Get Apple Access Token` , `[CONTENT] ${error}`);
const slackMessage = `[ERROR] Get Apple Access Token ${JSON.stringify(error)}`;
slackAPI.sendMessageToSlack(slackMessage, slackAPI.WEB_HOOK_ERROR_MONITORING);
throw error;
}
}

/**
* @desc apple 서버에 소셜 로그인 연결 해지 요청
* @param {String} appleRefreshToken
*/
const revokeAppleToken = async (appleRefreshToken) => {
const clientSecret = jwt.sign(payload, await getPrivateKey(), {
algorithm: 'ES256',
header
});

const data = {
'client_id': process.env.APPLE_CLIENT_ID,
'client_secret': clientSecret,
'token': appleRefreshToken,
'token_type_hint': 'refresh_token',
};

try {
await axios.post(`${appleBaseURL}/auth/revoke`, qs.stringify(data));
} catch (error) {
functions.logger.error(`[ERROR] Get Apple Access Token`, `[CONTENT] ${error}`);
const slackMessage = `[ERROR] Get Apple Access Token ${JSON.stringify(error)}`;
slackAPI.sendMessageToSlack(slackMessage, slackAPI.WEB_HOOK_ERROR_MONITORING);
throw error;
}
}

module.exports = { getAppleRefreshToken, revokeAppleToken }

0 comments on commit 1a2c510

Please sign in to comment.