Skip to content

Commit

Permalink
[auth] return auth status in register
Browse files Browse the repository at this point in the history
  • Loading branch information
Noverish committed May 6, 2024
1 parent 35c05dd commit cbc5dfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kim.hyunsub.auth.model.dto.auth.RegisterResult
import kim.hyunsub.auth.model.dto.auth.UserCreateParams
import kim.hyunsub.auth.repository.UserRepository
import kim.hyunsub.auth.service.CaptchaService
import kim.hyunsub.auth.service.IdService
import kim.hyunsub.auth.service.PasswordService
import kim.hyunsub.auth.service.RsaKeyService
import kim.hyunsub.auth.service.UserService
Expand All @@ -20,6 +21,7 @@ class RegisterBo(
private val rsaKeyService: RsaKeyService,
private val userService: UserService,
private val passwordService: PasswordService,
private val idService: IdService,
) {
fun register(params: RegisterParams, remoteAddr: String): RegisterResult {
val username = rsaKeyService.decrypt(params.username)
Expand All @@ -32,11 +34,15 @@ class RegisterBo(

val exist = userRepository.findByUsername(username)
if (exist != null) {
throw ErrorCodeException(ErrorCode.ALREADY_EXIST_USERNAME)
return RegisterResult(AuthStatus.ALREADY_EXIST_ID)
}

if (username.length < 4) {
throw ErrorCodeException(ErrorCode.SHORT_USERNAME)
if (!idService.isValidLength(username)) {
return RegisterResult(AuthStatus.INVALID_LENGTH_ID)
}

if (!idService.isValidPattern(username)) {
return RegisterResult(AuthStatus.INVALID_PATTERN_ID)
}

if (!passwordService.isValidLength(password)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ enum class AuthStatus(
) {
SUCCESS(0),

ALREADY_EXIST_ID(1000),
INVALID_LENGTH_ID(1001),
INVALID_PATTERN_ID(1002),

INVALID_LENGTH_PASSWORD(2000),
WRONG_PASSWORD(2001),
CURRENT_PASSWORD(2002),
Expand Down
14 changes: 14 additions & 0 deletions hyunsub-auth/src/main/kotlin/kim/hyunsub/auth/service/IdService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kim.hyunsub.auth.service

import org.springframework.stereotype.Service

@Service
class IdService {
val regex = Regex("^[A-Za-z0-9_]+$")

fun isValidLength(id: String): Boolean =
id.length in 4..255

fun isValidPattern(id: String): Boolean =
regex.matches(id)
}

0 comments on commit cbc5dfb

Please sign in to comment.