Skip to content

Commit

Permalink
Merge pull request #67 from cho4036/minor_fix
Browse files Browse the repository at this point in the history
bug fix. 비밀번호 입력 관련 API의 에러코드 추가
  • Loading branch information
ktkfree authored Jun 1, 2023
2 parents c1efde6 + 3b27131 commit 52d0b27
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 40 deletions.
12 changes: 7 additions & 5 deletions internal/aws/ses/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ func SendEmailForGeneratingOrganization(client *awsSes.Client, organizationId st
targetEmailAddress string, userAccountId string, randomPassword string) error {
subject := "[TKS] 조직이 생성되었습니다."
body := "조직이 생성되었습니다. \n" +
"조직 ID: " + organizationId + "\n" +
"조직 이름: " + organizationName + "\n\n" +
"아래 관리자 계정 정보로 로그인 후 사용바랍니다.\n" +
"관리자 ID: " + userAccountId + "\n" +
"관리자 이름: admin\n" +
"조직코드: " + organizationId + "\n" +
"이름: " + organizationName + "\n" +
"관리자 아이디: " + userAccountId + "\n" +
"관리자 이름: admin\n\n" +
"아래 관리자 계정 정보로 로그인 후 사용하시기 바랍니다.\n" +
"조직코드: " + organizationId + "\n" +
"아이디: " + userAccountId + "\n" +
"비밀번호: " + randomPassword + "\n\n" +
thanksContent

Expand Down
3 changes: 1 addition & 2 deletions internal/delivery/http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
user, err := h.usecase.Login(input.AccountId, input.Password, input.OrganizationId)
if err != nil {
log.ErrorfWithContext(r.Context(), "error is :%s(%T)", err.Error(), err)

ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
ErrorJSON(w, r, err)
return
}

Expand Down
6 changes: 1 addition & 5 deletions internal/delivery/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +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)
if strings.Contains(err.Error(), "Invalid user credentials") {
ErrorJSON(w, r, httpErrors.NewUnauthorizedError(err, "A_INVALID_USER_CREDENTIAL", ""))
return
}
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
ErrorJSON(w, r, err)
return
}

Expand Down
32 changes: 16 additions & 16 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.NewUnauthorizedError(err, "", "")
return domain.User{}, httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if !helper.CheckPasswordHash(user.Password, password) {
return domain.User{}, httpErrors.NewUnauthorizedError(fmt.Errorf("Mismatch 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.NewUnauthorizedError(err, "", "")
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, "", "")
return "", httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return "", httpErrors.NewInternalServerError(err, "", "")
Expand All @@ -159,13 +159,13 @@ func (u *AuthUsecase) FindId(code string, email string, userName string, organiz
}
emailCode, err := u.authRepository.GetEmailCode(userUuid)
if err != nil {
return "", httpErrors.NewBadRequestError(err, "", "")
return "", httpErrors.NewInternalServerError(err, "", "")
}
if !u.isValidEmailCode(emailCode) {
return "", httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "", "")
if !u.isExpiredEmailCode(emailCode) {
return "", httpErrors.NewBadRequestError(fmt.Errorf("expired code"), "A_EXPIRED_CODE", "")
}
if emailCode.Code != code {
return "", httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "", "")
return "", httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "A_MISMATCH_CODE", "")
}
if err := u.authRepository.DeleteEmailCode(userUuid); err != nil {
return "", httpErrors.NewInternalServerError(err, "", "")
Expand All @@ -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, "", "")
return httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return httpErrors.NewInternalServerError(err, "", "")
Expand All @@ -191,19 +191,19 @@ func (u *AuthUsecase) FindPassword(code string, accountId string, email string,
}
emailCode, err := u.authRepository.GetEmailCode(userUuid)
if err != nil {
return httpErrors.NewBadRequestError(err, "", "")
return httpErrors.NewInternalServerError(err, "", "")
}
if !u.isValidEmailCode(emailCode) {
return httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "", "")
if !u.isExpiredEmailCode(emailCode) {
return httpErrors.NewBadRequestError(fmt.Errorf("expired code"), "A_EXPIRED_CODE", "")
}
if emailCode.Code != code {
return httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "", "")
return httpErrors.NewBadRequestError(fmt.Errorf("invalid code"), "A_MISMATCH_CODE", "")
}
randomPassword := helper.GenerateRandomString(passwordLength)

originUser, err := u.kc.GetUser(organizationId, accountId)
if err != nil {
return err
return httpErrors.NewInternalServerError(err, "", "")
}
originUser.Credentials = &[]gocloak.CredentialRepresentation{
{
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, "", "")
return httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
}
if err != nil {
return httpErrors.NewInternalServerError(err, "", "")
Expand Down Expand Up @@ -300,7 +300,7 @@ func (u *AuthUsecase) SingleSignIn(organizationId, accountId, password string) (
return cookies, nil
}

func (u *AuthUsecase) isValidEmailCode(code repository.CacheEmailCode) bool {
func (u *AuthUsecase) isExpiredEmailCode(code repository.CacheEmailCode) bool {
return !helper.IsDurationExpired(code.UpdatedAt, internal.EmailCodeExpireTime)
}

Expand Down
9 changes: 7 additions & 2 deletions internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,19 @@ func (u *StackUsecase) Get(ctx context.Context, stackId domain.StackId) (out dom
out.PrimaryCluster = true
}

for _, appGroup := range appGroups {
appGroupsInPrimaryCluster, err := u.appGroupRepo.Fetch(domain.ClusterId(organization.PrimaryClusterId))
if err != nil {
return out, err
}

for _, appGroup := range appGroupsInPrimaryCluster {
if appGroup.AppGroupType == domain.AppGroupType_LMA {
applications, err := u.appGroupRepo.GetApplications(appGroup.ID, domain.ApplicationType_GRAFANA)
if err != nil {
return out, err
}
if len(applications) > 0 {
out.GrafanaUrl = applications[0].Endpoint
out.GrafanaUrl = applications[0].Endpoint + "/d/tks-kubernetes/tks-kubernetes-view-cluster-global?var-taco_cluster=" + cluster.ID.String() + "&kiosk"
}
}
}
Expand Down
23 changes: 13 additions & 10 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"), "", "")
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"), "", "")
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"), "", "")
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"), "", "")
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"), "", "")
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}
Expand All @@ -142,10 +142,13 @@ 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 err
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
_, err = u.kc.Login(user.AccountId, password, organizationId)
return err
if err != nil {
return httpErrors.NewBadRequestError(fmt.Errorf("invalid password"), "A_INVALID_PASSWORD", "")
}
return nil
}

func (u *UserUsecase) ValidateAccountByAccountId(accountId string, password string, organizationId string) error {
Expand Down Expand Up @@ -273,10 +276,10 @@ func (u *UserUsecase) CreateAdmin(orgainzationId string, email string) (*domain.
func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId string, originPassword string, newPassword string,
organizationId string) error {
if originPassword == newPassword {
return httpErrors.NewBadRequestError(fmt.Errorf("new password is same with origin password"), "", "")
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"), "", "")
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 @@ -331,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"), "", "")
return nil, httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "U_NO_USER", "")
}
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/httpErrors/errorCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ var errorMap = map[ErrorCode]string{
"C_INVALID_ASA_TASK_ID": "유효하지 않은 테스크 아이디입니다. 테스크 아이디를 확인하세요.",

// Auth
"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_EXPIRED_CODE": "인증번호가 만료되었습니다.",

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

// CloudAccount
"CA_INVALID_CLIENT_TOKEN_ID": "유효하지 않은 토큰입니다. AccessKeyId, SecretAccessKey, SessionToken 을 확인후 다시 입력하세요.",
Expand Down

0 comments on commit 52d0b27

Please sign in to comment.