[#34] 회원 / 인증 도메인 재정의 #43

Merged
pricelees merged 73 commits from refactor/#34 into main 2025-09-13 10:13:45 +00:00
Showing only changes of commit efa33a071f - Show all commits

View File

@ -33,50 +33,34 @@ class AuthServiceV2(
request: LoginRequestV2, request: LoginRequestV2,
context: LoginContext context: LoginContext
): LoginSuccessResponse { ): LoginSuccessResponse {
log.info { "[AuthService.login] 로그인 시작: account=${request.account}, type=${request.principalType}" } log.info { "[AuthService.login] 로그인 시작: account=${request.account}, type=${request.principalType}, context=${context}" }
val extraClaims: MutableMap<String, Any> = mutableMapOf() val (credentials, extraClaims) = getCredentials(request)
val credentials: LoginCredentials = when (request.principalType) {
PrincipalType.ADMIN -> {
adminService.findCredentialsByAccount(request.account).also {
extraClaims.put(CLAIM_PERMISSION_KEY, it.permissionLevel)
extraClaims.put(CLAIM_TYPE_KEY, PrincipalType.ADMIN)
}
}
PrincipalType.USER -> {
userService.findCredentialsByAccount(request.account).also {
extraClaims.put(CLAIM_TYPE_KEY, PrincipalType.USER)
}
}
}
try { try {
if (credentials.password != request.password) { verifyPasswordOrThrow(request, credentials)
log.info { "[AuthService.login] 비밀번호 불일치로 인한 로그인 실패: account = ${request.account}" }
throw AuthException(AuthErrorCode.LOGIN_FAILED)
}
val accessToken = jwtUtils.createToken(subject = credentials.id.toString(), claims = extraClaims) val accessToken = jwtUtils.createToken(subject = credentials.id.toString(), claims = extraClaims)
return LoginSuccessResponse(accessToken)
.also { loginHistoryService.createSuccessHistory(credentials.id, request.principalType, context)
log.info { "[AuthService.login] 관리자 로그인 완료: account = ${request.account}, id=${credentials.id}" }
loginHistoryService.createSuccessHistory(credentials.id, PrincipalType.ADMIN, context) return LoginSuccessResponse(accessToken).also {
} log.info { "[AuthService.login] 로그인 완료: account=${request.account}, context=${context}" }
}
} catch (e: Exception) { } catch (e: Exception) {
log.warn { "[AuthService.login] 관리자 로그인 실패: account = ${request.account}, message=${e.message}" } loginHistoryService.createFailureHistory(credentials.id, request.principalType, context)
loginHistoryService.createFailureHistory(credentials.id, PrincipalType.ADMIN, context)
throw e when (e) {
} is AuthException -> {
} log.info { "[AuthService.login] 로그인 실패: account = ${request.account}" }
throw e
}
@Transactional(readOnly = true) else -> {
fun checkLogin(context: CurrentUserContext): CurrentUserContext { log.warn { "[AuthService.login] 로그인 실패: message=${e.message} account = ${request.account}" }
return findContextById(context.id, context.type).also { throw AuthException(AuthErrorCode.TEMPORARY_AUTH_ERROR)
if (it != context) { }
throw AuthException(AuthErrorCode.MEMBER_NOT_FOUND)
} }
} }
} }
@ -97,4 +81,34 @@ class AuthServiceV2(
log.info { "[AuthService.checkLogin] 로그인 확인 완료: id=${id}, type=${type}" } log.info { "[AuthService.checkLogin] 로그인 확인 완료: id=${id}, type=${type}" }
} }
} }
private fun verifyPasswordOrThrow(
request: LoginRequestV2,
credentials: LoginCredentials
) {
if (credentials.password != request.password) {
log.info { "[AuthService.login] 비밀번호 불일치로 인한 로그인 실패: account = ${request.account}" }
throw AuthException(AuthErrorCode.LOGIN_FAILED)
}
}
private fun getCredentials(request: LoginRequestV2): Pair<LoginCredentials, Map<String, Any>> {
val extraClaims: MutableMap<String, Any> = mutableMapOf()
val credentials: LoginCredentials = when (request.principalType) {
PrincipalType.ADMIN -> {
adminService.findCredentialsByAccount(request.account).also {
extraClaims.put(CLAIM_PERMISSION_KEY, it.permissionLevel)
extraClaims.put(CLAIM_TYPE_KEY, PrincipalType.ADMIN)
}
}
PrincipalType.USER -> {
userService.findCredentialsByAccount(request.account).also {
extraClaims.put(CLAIM_TYPE_KEY, PrincipalType.USER)
}
}
}
return credentials to extraClaims
}
} }