Skip to content

Commit

Permalink
minor fix: change auth error code
Browse files Browse the repository at this point in the history
  • Loading branch information
cho4036 committed May 31, 2023
1 parent 393efa5 commit 3b27131
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/delivery/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (u UserHandler) UpdateMyProfile(w http.ResponseWriter, r *http.Request) {
err = u.usecase.ValidateAccount(requestUserInfo.GetUserId(), input.Password, requestUserInfo.GetOrganizationId())
if err != nil {
log.ErrorfWithContext(r.Context(), "error is :%s(%T)", err.Error(), err)
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "A_INVALID_ID_PASSWORD", ""))
ErrorJSON(w, r, err)
return
}

Expand Down
12 changes: 6 additions & 6 deletions internal/usecase/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (u *AuthUsecase) Login(accountId string, password string, organizationId st
// Authentication with DB
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
return domain.User{}, httpErrors.NewBadRequestError(err, "A_INVALID_ID_PASSWORD", "")
return domain.User{}, httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if !helper.CheckPasswordHash(user.Password, password) {
return domain.User{}, httpErrors.NewBadRequestError(fmt.Errorf("Mismatch password"), "A_INVALID_ID_PASSWORD", "")
return domain.User{}, httpErrors.NewBadRequestError(fmt.Errorf("Mismatch password"), "A_INVALID_PASSWORD", "")
}
var accountToken *domain.User
// Authentication with Keycloak
Expand All @@ -123,7 +123,7 @@ func (u *AuthUsecase) Login(accountId string, password string, organizationId st
}
if err != nil {
//TODO: implement not found handling
return domain.User{}, httpErrors.NewBadRequestError(err, "A_INVALID_ID_PASSWORD", "")
return domain.User{}, err
}

// Insert token
Expand All @@ -148,7 +148,7 @@ func (u *AuthUsecase) FindId(code string, email string, userName string, organiz
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.NameFilter(userName), u.userRepository.EmailFilter(email))
if err != nil && users == nil {
return "", httpErrors.NewBadRequestError(err, "A_NO_USER", "")
return "", httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return "", httpErrors.NewInternalServerError(err, "", "")
Expand Down Expand Up @@ -179,7 +179,7 @@ func (u *AuthUsecase) FindPassword(code string, accountId string, email string,
u.userRepository.AccountIdFilter(accountId), u.userRepository.NameFilter(userName),
u.userRepository.EmailFilter(email))
if err != nil && users == nil {
return httpErrors.NewBadRequestError(err, "A_NO_USER", "")
return httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return httpErrors.NewInternalServerError(err, "", "")
Expand Down Expand Up @@ -247,7 +247,7 @@ func (u *AuthUsecase) VerifyIdentity(accountId string, email string, userName st
u.userRepository.EmailFilter(email))
}
if err != nil && users == nil {
return httpErrors.NewBadRequestError(err, "A_NO_USER", "")
return httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return httpErrors.NewInternalServerError(err, "", "")
Expand Down
18 changes: 9 additions & 9 deletions internal/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (u *UserUsecase) RenewalPasswordExpiredTime(ctx context.Context, userId uui
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status != http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}
Expand All @@ -72,7 +72,7 @@ func (u *UserUsecase) RenewalPasswordExpiredTimeByAccountId(ctx context.Context,
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status != http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}
Expand All @@ -87,13 +87,13 @@ func (u *UserUsecase) ResetPassword(userId uuid.UUID) error {
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
}
userInKeycloak, err := u.kc.GetUser(user.Organization.ID, user.AccountId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (u *UserUsecase) ResetPasswordByAccountId(accountId string, organizationId
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}
Expand All @@ -142,11 +142,11 @@ func (u *UserUsecase) ResetPasswordByAccountId(accountId string, organizationId
func (u *UserUsecase) ValidateAccount(userId uuid.UUID, password string, organizationId string) error {
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
_, err = u.kc.Login(user.AccountId, password, organizationId)
if err != nil {
return httpErrors.NewBadRequestError(fmt.Errorf("invalid authentication"), "A_INVALID_ID_PASSWORD", "")
return httpErrors.NewBadRequestError(fmt.Errorf("invalid password"), "A_INVALID_PASSWORD", "")
}
return nil
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId s
return httpErrors.NewBadRequestError(fmt.Errorf("new password is same with origin password"), "A_SAME_OLD_PASSWORD", "")
}
if _, err := u.kc.Login(accountId, originPassword, organizationId); err != nil {
return httpErrors.NewBadRequestError(fmt.Errorf("invalid origin password"), "A_INVALID_ID_PASSWORD", "")
return httpErrors.NewBadRequestError(fmt.Errorf("invalid origin password"), "A_INVALID_PASSWORD", "")
}
originUser, err := u.kc.GetUser(organizationId, accountId)
if err != nil {
Expand Down Expand Up @@ -334,7 +334,7 @@ func (u *UserUsecase) Get(userId uuid.UUID) (*domain.User, error) {
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return nil, httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "A_NO_USER", "")
return nil, httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/httpErrors/errorCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ var errorMap = map[ErrorCode]string{
"C_INVALID_ASA_TASK_ID": "유효하지 않은 테스크 아이디입니다. 테스크 아이디를 확인하세요.",

// Auth
"A_INVALID_ID_PASSWORD": "아이디 또는 비밀번호가 일치하지 않습니다.",
"A_INVALID_ID": "아이디가 존재하지 않습니다.",
"A_INVALID_PASSWORD": "비밀번호가 일치하지 않습니다.",
"A_SAME_OLD_PASSWORD": "기존 비밀번호와 동일합니다.",
"A_INVALID_TOKEN": "사용자 토큰 오류",
"A_INVALID_USER_CREDENTIAL": "비밀번호가 일치하지 않습니다.",
"A_INVALID_ORIGIN_PASSWORD": "기존 비밀번호가 일치하지 않습니다.",
"A_MISMATCH_PASSWORD": "비밀번호가 일치하지 않습니다.",
"A_MISMATCH_CODE": "인증번호가 일치하지 않습니다.",
"A_NO_SESSION": "세션 정보를 찾을 수 없습니다.",
"A_NO_USER": "해당 사용자 정보를 찾을 수 없습니다.",
"A_EXPIRED_CODE": "인증번호가 만료되었습니다.",

// User
"U_NO_USER": "해당 사용자 정보를 찾을 수 없습니다.",

// CloudAccount
"CA_INVALID_CLIENT_TOKEN_ID": "유효하지 않은 토큰입니다. AccessKeyId, SecretAccessKey, SessionToken 을 확인후 다시 입력하세요.",
"CA_INVALID_CLOUD_ACCOUNT_NAME": "유효하지 않은 클라우드계정 이름입니다. 클라우드계정 이름을 확인하세요.",
Expand Down

0 comments on commit 3b27131

Please sign in to comment.