-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from heesung6701/feature/seminar5
Feature/seminar5
- Loading branch information
Showing
12 changed files
with
426 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
module.exports = { | ||
secretOrPrivateKey: "jwtSecretKey!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const { | ||
secretOrPrivateKey | ||
} = require('../config/secretKey'); | ||
const resMessage = require('./util/responseMessage'); | ||
const statusCode = require('./util/statusCode'); | ||
|
||
const options = { | ||
algorithm: "HS256", | ||
expiresIn: "1m", | ||
issuer: "with-sopt" | ||
}; | ||
|
||
const refreshOptions = { | ||
algorithm: "HS256", | ||
expiresIn: "2h", | ||
issuer: "with-sopt" | ||
}; | ||
|
||
module.exports = { | ||
publish: (payload) => { | ||
const token = jwt.sign(payload, secretOrPrivateKey, options); | ||
const refreshToken = jwt.sign({ | ||
refreshToken: payload | ||
}, secretOrPrivateKey, refreshOptions); | ||
return { | ||
token, | ||
refreshToken | ||
}; | ||
}, | ||
create: (payload) => { | ||
return jwt.sign(payload, secretOrPrivateKey, options); | ||
}, | ||
verify: (token) => { | ||
try { | ||
const data = jwt.verify(token, secretOrPrivateKey); | ||
return { | ||
isError: false, | ||
data | ||
}; | ||
} catch (err) { | ||
if (err.message === 'jwt expired') { | ||
console.log('expired token'); | ||
return { | ||
isError: true, | ||
data: { | ||
code: statusCode.UNAUTHORIZED, | ||
json: resMessage.EXPIRED_TOKEN | ||
} | ||
}; | ||
} | ||
if (err.message === 'invalid token') { | ||
console.log('invalid token'); | ||
return { | ||
isError: true, | ||
data: { | ||
code: statusCode.UNAUTHORIZED, | ||
json: resMessage.INVALID_TOKEN | ||
} | ||
}; | ||
} | ||
console.log(err); | ||
return { | ||
isError: true, | ||
data: err | ||
}; | ||
} | ||
}, | ||
reissue: (payload, refreshToken) => { | ||
const result = jwt.verify(refreshToken); | ||
if(result.isError){ | ||
return result; | ||
} | ||
if(result.data.userIdx != payload.userIdx) { | ||
return { | ||
isError: true, | ||
data: { | ||
code: statusCode.UNAUTHORIZED, | ||
json: resMessage.INVALID_TOKEN | ||
} | ||
}; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const randToken = require('rand-token'); | ||
const jwt = require('jsonwebtoken'); | ||
const {secretOrPrivateKey} = require('../config/secretKey'); | ||
const options = { | ||
algorithm: "HS256", | ||
expiresIn: "1h", | ||
issuer: "genie" | ||
}; | ||
|
||
module.exports = { | ||
sign: (user) => { | ||
const payload = { | ||
idx: user.idx, | ||
grade: user.grade, | ||
name: user.name | ||
}; | ||
//발급받은 refreshToken은 반드시 디비에 저장해야 한다. | ||
const result = { | ||
token: jwt.sign(payload, secretOrPrivateKey, options), | ||
refreshToken: randToken.uid(256) | ||
}; | ||
//refreshToken을 만들 때에도 다른 키를 쓰는게 좋다. | ||
//대부분 2주로 만든다. | ||
|
||
return result; | ||
}, | ||
verify: (token) => { | ||
let decoded; | ||
try { | ||
decoded = jwt.verify(token, secretOrPrivateKey); | ||
} catch (err) { | ||
if (err.message === 'jwt expired') { | ||
console.log('expired token'); | ||
return -3; | ||
} else if (err.message === 'invalid token') { | ||
console.log('invalid token'); | ||
return -2; | ||
} else { | ||
console.log("invalid token"); | ||
return -2; | ||
} | ||
} | ||
return decoded; | ||
}, | ||
refresh: (user) => { | ||
const payload = { | ||
idx: user.idx, | ||
grade: user.grade, | ||
name: user.name | ||
}; | ||
return jwt.sign(payload, secretOrPrivateKey, options); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const jwt = require('../jwt-ext'); | ||
|
||
const resMessage = require('./responseMessage'); | ||
const statusCode = require('./statusCode'); | ||
const util = require('./utils'); | ||
|
||
const authUtil = { | ||
LoggedIn: async(req, res, next) => { | ||
var token = req.headers.token; | ||
|
||
if (!token) { | ||
return res.status(statusCode.BAD_REQUEST).json(util.successFalse(resMessage.EMPTY_TOKEN)); | ||
} | ||
const result = jwt.verify(token); | ||
|
||
if(result.isError){ | ||
const {code, json} = result.data; | ||
if(code && json) { | ||
return res.status(code).send(util.successFalse(json)); | ||
} | ||
const err = result.data; | ||
return res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.successFalse(err.message)); | ||
} | ||
|
||
const {userIdx} = result.data; | ||
if (!userIdx){ | ||
return res.status(statusCode.UNAUTHORIZED).send(util.successFalse(resMessage.INVALID_TOKEN)); | ||
} | ||
req.decoded = userIdx; | ||
next(); | ||
}, | ||
}; | ||
|
||
module.exports = authUtil; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
NULL_VALUE: "필요한 값이 없습니다", | ||
|
||
INVALID_TOKEN: "잘못된 형식의 토큰입니다.", | ||
EMPTY_TOKEN: "토큰값이 존재하지 않습니다.", | ||
EXPIRED_TOKEN: "만료된 토큰입니다.", | ||
EMPTY_REFRESH_TOKEN: "재발급 토큰이 존재하지 않습니다.", | ||
CREATE_TOKEN: "토큰 발급 완료.", | ||
REFRESH_TOKEN: "토큰 재발급 완료.", | ||
|
||
NO_SELECT_AUTHORITY: "조회 권한 없음.", | ||
USER_SELECTED: "회원 조회 성공." | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
OK: 200, | ||
CREATED: 201, | ||
NO_CONTENT: 204, | ||
BAD_REQUEST: 400, | ||
UNAUTHORIZED: 401, | ||
FORBIDDEN: 403, | ||
NOT_FOUND: 404, | ||
INTERNAL_SERVER_ERROR: 500, | ||
SERVICE_UNAVAILABLE: 503, | ||
DB_ERROR: 600, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const util = { | ||
successTrue: (message, data) => { | ||
return { | ||
success: true, | ||
message: message, | ||
data: data | ||
} | ||
}, | ||
successFalse: (message) => { | ||
return { | ||
success: false, | ||
message: message | ||
} | ||
} | ||
}; | ||
|
||
module.exports = util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.