Skip to content

Commit

Permalink
refactor: 에러피드백 통합 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelSuho committed Mar 27, 2024
1 parent 1269df9 commit 2664379
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,10 @@ class JwtAuthFilter(

if (request.requestURI.contains("reissue")) {
val refreshToken = jwtTokenUtil.getTokenFromCookie("refreshToken", request)
if (!StringUtils.hasText(refreshToken)) {
throw FilterException(ErrorStatus.UNAUTHORIZED, "헤더에 토큰이 존재하지 않습니다.")
}

if (!jwtTokenUtil.verifyToken(refreshToken) && !refreshTokenService.verifyToken("refreshToken", refreshToken)) {
throw FilterException(ErrorStatus.INVALID_TOKEN, "유효하지 않은 토큰입니다.")
} else {
val authentication = jwtTokenUtil.getAuthentication(refreshToken)
SecurityContextHolder.getContext().authentication = authentication
}
verifyAndAuthenticateToken(refreshToken, "refreshToken")
} else {
val accessToken = jwtTokenUtil.getTokenFromCookie("accessToken", request)
if (!StringUtils.hasText(accessToken)) {
throw FilterException(ErrorStatus.UNAUTHORIZED, "헤더에 토큰이 존재하지 않습니다.")
}

if (!jwtTokenUtil.verifyToken(accessToken) && !refreshTokenService.verifyToken("accessToken", accessToken)) {
throw FilterException(ErrorStatus.INVALID_TOKEN, "유효하지 않은 토큰입니다.")
} else {
val authentication = jwtTokenUtil.getAuthentication(accessToken)
SecurityContextHolder.getContext().authentication = authentication
}
verifyAndAuthenticateToken(accessToken, "accessToken")
}

filterChain.doFilter(request, response)
Expand All @@ -82,6 +64,18 @@ class JwtAuthFilter(
}
}

private fun verifyAndAuthenticateToken(tokenName: String, token: String) {
if (!StringUtils.hasText(token)) {
throw FilterException(ErrorStatus.BAD_REQUEST, "헤더에 토큰이 존재하지 않습니다.")
}
if (!jwtTokenUtil.verifyToken(token) && !refreshTokenService.verifyToken(tokenName, token)) {
throw FilterException(ErrorStatus.INVALID_TOKEN, "유효하지 않은 토큰입니다.")
} else {
val authentication = jwtTokenUtil.getAuthentication(token)
SecurityContextHolder.getContext().authentication = authentication
}
}

private fun handleException(request: HttpServletRequest, response: HttpServletResponse, e: Exception, message: String? = null) {
if (e is FilterException) {
val errorResponse = FilterExceptionResponse.from(e, request.requestURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ class AccountService(
fun revokeAccount(request: HttpServletRequest, email: String) {
val accessToken = jwtTokenUtil.getTokenFromCookie("accessToken", request)
jwtTokenUtil.verifyToken(accessToken)
val account = accountRepository.findByEmail(email) ?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
val account = findByEmail(email)
oauthService.sendRevokeRequest(account.socialAuthToken, account.socialProvider)
deleteMyAccount(account)
refreshTokenService.deleteById(email)
}

@Transactional
fun updateMyAccount(email: String, request: AccountUpdateRequest): AccountResponse {
val account = accountRepository.findByEmail(email) ?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
val account = findByEmail(email)
account.updateNickName(request.nickName)
return AccountResponse(
email = account.email,
Expand All @@ -108,15 +108,15 @@ class AccountService(

@Transactional
fun updateAccountSessionId(sessionId: String) {
val account = accountRepository.findBySessionId(sessionId) ?: throw PregenException(ErrorStatus.DATA_NOT_FOUND)
val account = findByEmail(sessionId)
account.updateSessionId(sessionId)
}

@Transactional
fun reIssueToken(request: HttpServletRequest, response: HttpServletResponse, email: String) {
val refreshToken = jwtTokenUtil.getTokenFromCookie("refreshToken", request)
jwtTokenUtil.verifyToken(refreshToken)
val account = accountRepository.findByEmail(email) ?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
val account = findByEmail(email)
val reissueSocialToken =
oauthService.verifyAndReissueSocialToken(account.socialAuthToken, account.socialProvider)
account.updateSocialAuthToken(reissueSocialToken)
Expand All @@ -133,7 +133,7 @@ class AccountService(
}

fun getMyAccount(email: String): AccountResponse {
val account = accountRepository.findByEmail(email) ?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
val account = findByEmail(email)
return AccountResponse(
email = account.email,
nickName = account.nickName,
Expand All @@ -142,7 +142,7 @@ class AccountService(
}

fun getSessionId(email: String): SessionIdResponse {
val account = accountRepository.findByEmail(email) ?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
val account = findByEmail(email)
return SessionIdResponse(account.sessionId)
}

Expand All @@ -158,4 +158,8 @@ class AccountService(
?.deactivateNextSlideModal()
?: throw IllegalArgumentException("존재하지 않는 계정입니다.")
}

private fun findByEmail(email: String): Account {
return accountRepository.findByEmail(email) ?: throw PregenException(ErrorStatus.DATA_NOT_FOUND, "존재하지 않는 계정입니다.")
}
}

0 comments on commit 2664379

Please sign in to comment.