Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #60

Merged
merged 6 commits into from
Jul 14, 2024
Merged

Dev #60

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const conf = {
// server
port: process.env.PORT,
corsWhiteList: process.env.CORS_WHITELIST,
corsUserAgent: process.env.CORS_USERAGENT,

// database
mongoURL: process.env.MONGO_URL,
Expand Down
16 changes: 15 additions & 1 deletion src/common/modules/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@ module.exports = expressLoader = (app) => {
next();
});

// Content Security Policy 설정, 위 난수 활용
app.use((req, res, next) => {
res.setHeader('Content-Security-Policy', `script-src 'self' 'nonce-${res.locals.nonce}';`);
next();
});

// CORS 설정
app.use((req, res, next) => {
cors({
credentials: true,
origin: (origin, callback) => {
if (origin === undefined || (origin && conf.corsWhiteList?.indexOf(origin) !== -1)) {
if (
// whitelist에 있는 origin 허용
(origin && conf.corsWhiteList.indexOf(origin) !== -1) ||
// postman 허용
(!origin &&
conf.corsUserAgent.split(',').some((agent) => req.headers['user-agent'].includes(agent)))
) {
return callback(null, true);
}

console.error(`Blocked CORS request from: ${origin}`);
callback(new Error('CORS ERROR'));
},
})(req, res, next);
Expand Down
12 changes: 7 additions & 5 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports.localLogin = async (req, res, next) => {
const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);

await redisClient.set(user.email, refreshToken);
await redisClient.set(user.email, refreshToken, 'EX', 60 * 60 * 12);

res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

Expand Down Expand Up @@ -137,9 +137,8 @@ exports.kakaoLogin = async (req, res) => {

const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);
console.log(user.email);
const re = await redisClient.set(user.email, refreshToken);
console.log('hh', re);

await redisClient.set(user.email, refreshToken, 'EX', 60 * 60 * 12);
res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
Expand Down Expand Up @@ -172,6 +171,9 @@ exports.refreshToken = async (req, res) => {
const storedRefreshToken = await redisClient.get(user.email);

if (storedRefreshToken !== refreshToken) {
console.error('Refresh token mismatch');
await redisClient.del(user.email);
res.clearCookie('refreshToken', config.cookieInRefreshTokenDeleteOptions);
return sendResponse.unAuthorized(res, {
message: ErrorMessage.REFRESH_TOKEN_MISMATCH,
});
Expand All @@ -188,7 +190,7 @@ exports.refreshToken = async (req, res) => {
email: user.email,
});

await redisClient.set(user.email, newRefreshToken);
await redisClient.set(user.email, newRefreshToken, 'EX', 60 * 60 * 12);
res.cookie('refreshToken', newRefreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
Expand Down