Skip to content

Commit

Permalink
Fix issue when enabling two factor
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
kwesidev committed Dec 15, 2023
1 parent 119c886 commit 4f672fc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (usrCtrl *UserController) EnableTwoFactor(w http.ResponseWriter, r *http.Re
return
}
userId := utilities.GetUserIdFromHttpConext(r)
if enableTwoFactorRequest.Type == "TOTP" {
if enableTwoFactorRequest.Method == "TOTP" {
totpResponse, err := usrCtrl.userService.EnableTwoFactorTOTP(userId)
if err != nil {
utilities.JSONError(w, "Failed to Enable Two Factor (TOTP)", http.StatusBadRequest)
Expand All @@ -102,7 +102,7 @@ func (usrCtrl *UserController) EnableTwoFactor(w http.ResponseWriter, r *http.Re
utilities.JSONResponse(w, totpResponse)
return
} else {
err := usrCtrl.userService.EnableTwoFactor(userId, enableTwoFactorRequest.Type)
err := usrCtrl.userService.EnableTwoFactor(userId, enableTwoFactorRequest.Method)
if err != nil {
utilities.JSONError(w, "Failed to Enabled Two Factor EMAIL OR SMS ", http.StatusBadRequest)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/models/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type User struct {
Metadata JSONB `json:"metadata"`
}
type EnableTwoFactorRequest struct {
Type string `json:"type"`
Method string `json:"method"`
}
type EnableTOTPResponse struct {
URL string `json:"url"`
Expand Down
1 change: 1 addition & 0 deletions internal/services/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ var (
ErrServer = errors.New("Server Error, Try again later")
ErrPassCode = errors.New("Invalid Passcode")
ErrStrongPassword = errors.New("Password must be at least 8 characters and must contain special characters")
ErrTOTPExists = errors.New("TOTP Already Enabled ")
)
5 changes: 4 additions & 1 deletion internal/services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func (usrSrv *UserService) DeleteToken(userId int, refreshToken string) (bool, e
// EnableTOTP
func (usrSrv *UserService) EnableTwoFactorTOTP(userId int) (*models.EnableTOTPResponse, error) {
userDetails := usrSrv.Get(userId)
if userDetails.TwoFactorMethod == "TOTP" {
return nil, ErrTOTPExists
}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: os.Getenv("ISSUER_NAME"),
AccountName: userDetails.Username,
Expand All @@ -255,7 +258,7 @@ func (usrSrv *UserService) EnableTwoFactorTOTP(userId int) (*models.EnableTOTPRe
`UPDATE
users
SET
two_factor_enabled = true , two_factor_type = 'TOTP',
two_factor_enabled = true , two_factor_method = 'TOTP',
totp_secret = $1, totp_url = $2 , totp_created = NOW()
WHERE
id = $3
Expand Down

0 comments on commit 4f672fc

Please sign in to comment.