diff --git a/src/modules/jwt.js b/src/modules/jwt.js new file mode 100644 index 00000000..5c749f7f --- /dev/null +++ b/src/modules/jwt.js @@ -0,0 +1,38 @@ +const jwt = require("jsonwebtoken"); +const { secretKey, option, TOKEN_EXPIRED, TOKEN_INVALID } = + require("../../loadenv").jwt; + +const signJwt = async (payload) => { + const options = { ...option }; + + if (type === "refresh") { + options.expiresIn = "30d"; + } + if (type === "access") { + options.expiresIn = "14d"; + } + + const result = { + token: jwt.sign(payload, secretKey, options), + }; + return result; +}; + +const verifyJwt = async (token) => { + let decoded; + try { + decoded = jwt.verify(token, secretKey); + } catch (err) { + if (err.message === "jwt expired") { + return TOKEN_EXPIRED; + } else { + return TOKEN_INVALID; + } + } + return decoded; +}; + +module.exports = { + sign: signJwt, + verify: verifyJwt, +}; diff --git a/src/services/rooms.js b/src/services/rooms.js index 0ef798fe..2ced458c 100644 --- a/src/services/rooms.js +++ b/src/services/rooms.js @@ -14,6 +14,10 @@ const { notifyRoomCreationAbuseToReportChannel, } = require("../modules/slackNotification"); +const { + signJwt,verifyJwt +} = require("../modules/jwt") + // 이벤트 코드입니다. const { eventConfig } = require("../../loadenv"); const eventPeriod = eventConfig && { @@ -23,7 +27,14 @@ const eventPeriod = eventConfig && { const { contracts } = require("../lottery"); const createHandler = async (req, res) => { - const { name, from, to, time, maxPartLength } = req.body; + const { name, from, to, time, maxPartLength, preValidationKey } = req.body; + + // 만약 preValidationKey를 사용하지 않을때 경고를 표출한다면 아래 코드를 사용하면 됨. + // if(!preValidationKey){ + // return res.status(400).json({ + // error: "Rooms/create : preValidation Key is Not Found" + // }) + // } try { if (from === to) { @@ -112,6 +123,19 @@ const createHandler = async (req, res) => { // 이벤트 코드입니다. await contracts?.completeFirstRoomCreationQuest(req.userOid, req.timestamp); + if (preValidationKey) { + const isAbuseResult = verifyJwt(preValidationKey); + + if (typeof isAbuseResult !== "object" || isAbuseResult.isAbuse) { + const user = await userModel.findById(req.userOid).lean(); + notifyRoomCreationAbuseToReportChannel( + req.userOid, + user?.nickname ?? req.userOid, + req.body + ); + } + } + return res.send(roomObjectFormated); } catch (err) { logger.error(err); @@ -168,16 +192,9 @@ const createTestHandler = async (req, res) => { countRecentlyMadeRooms, candidateRooms ); - if (isAbusing) { - const user = await userModel.findById(req.userOid).lean(); - notifyRoomCreationAbuseToReportChannel( - req.userOid, - user?.nickname ?? req.userOid, - req.body - ); - } + const preValidationKey = await signJwt({isAbusing: isAbusing}) - return res.json({ result: !isAbusing }); + return res.json({ result: !isAbusing, preValidationKey }); } catch (err) { logger.error(err); res.status(500).json({