Skip to content

Commit

Permalink
[auth] login auth status
Browse files Browse the repository at this point in the history
  • Loading branch information
Noverish committed May 6, 2024
1 parent cbc5dfb commit c39bc43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
35 changes: 19 additions & 16 deletions hyunsub-auth/src/main/kotlin/kim/hyunsub/auth/bo/auth/LoginBo.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kim.hyunsub.auth.bo.auth

import jakarta.servlet.http.HttpServletResponse
import kim.hyunsub.auth.model.AuthStatus
import kim.hyunsub.auth.model.LoginFailureSession
import kim.hyunsub.auth.model.dto.auth.LoginApiError
import kim.hyunsub.auth.model.dto.auth.LoginParams
import kim.hyunsub.auth.model.dto.auth.LoginResult
import kim.hyunsub.auth.repository.UserRepository
Expand All @@ -12,8 +12,7 @@ import kim.hyunsub.auth.service.LoginFailureSessionService
import kim.hyunsub.auth.service.PasswordService
import kim.hyunsub.auth.service.RsaKeyService
import kim.hyunsub.auth.service.TokenService
import kim.hyunsub.common.web.error.ErrorCode
import kim.hyunsub.common.web.error.ErrorCodeException
import kim.hyunsub.common.util.takeIfNotEmpty
import org.springframework.stereotype.Service

@Service
Expand All @@ -35,43 +34,47 @@ class LoginBo(
password = rsaKeyService.decrypt(params.password),
)

val result = try {
loginInternal(newParams, remoteAddr, session)
} catch (e: ErrorCodeException) {
val result = loginInternal(newParams, remoteAddr, session)

if (result.status != AuthStatus.SUCCESS) {
session.failCnt += 1
loginFailureSessionService.update(remoteAddr, session)
val payload = LoginApiError(session.needCaptcha)
throw ErrorCodeException(e.errorCode, payload)
} else {
loginFailureSessionService.delete(remoteAddr)
}

val cookie = cookieGenerator.generateLoginCookie(result.token, params.remember)
res.addCookie(cookie)
if (result.token != null) {
val cookie = cookieGenerator.generateLoginCookie(result.token, params.remember)
res.addCookie(cookie)
}

val langCookie = cookieGenerator.generateLanguageCookie(result.lang)
res.addCookie(langCookie)

return result
return result.copy(needCaptcha = session.needCaptcha)
}

private fun loginInternal(params: LoginParams, remoteAddr: String, session: LoginFailureSession): LoginResult {
if (session.needCaptcha) {
val captcha = params.captcha ?: throw ErrorCodeException(ErrorCode.CAPTCHA_REQUIRED)
val captcha = params.captcha.takeIfNotEmpty()
?: return LoginResult(AuthStatus.CAPTCHA_FAILURE)

val captchaSuccess = captchaService.verify(captcha, remoteAddr)
if (!captchaSuccess) {
throw ErrorCodeException(ErrorCode.CAPTCHA_FAILURE)
return LoginResult(AuthStatus.CAPTCHA_FAILURE)
}
}

val user = userRepository.findByUsername(params.username)
?: throw ErrorCodeException(ErrorCode.NOT_EXIST_USER)
?: return LoginResult(AuthStatus.NOT_EXIST_USER)

if (passwordService.isWrong(user, params.password)) {
throw ErrorCodeException(ErrorCode.NOT_EXIST_USER)
return LoginResult(AuthStatus.NOT_EXIST_USER)
}

val token = tokenService.issue(user)
return LoginResult(
idNo = user.idNo,
status = AuthStatus.SUCCESS,
token = token,
lang = user.lang,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ enum class AuthStatus(
INVALID_LENGTH_PASSWORD(2000),
WRONG_PASSWORD(2001),
CURRENT_PASSWORD(2002),

NOT_EXIST_USER(3000),
CAPTCHA_FAILURE(3001),
;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kim.hyunsub.auth.model.dto.auth

import kim.hyunsub.auth.model.AuthStatus
import kim.hyunsub.auth.model.UserLanguage

data class LoginResult(
val idNo: String,
val token: String,
val lang: UserLanguage?,
val status: AuthStatus,
val needCaptcha: Boolean = false,
val token: String? = null,
val lang: UserLanguage? = null,
)

0 comments on commit c39bc43

Please sign in to comment.