verifyReauthentication checks if the nonce provided is valid
(nonce string, tx *storage.Connection, config *conf.GlobalConfiguration, user *models.User)
| 68 | |
| 69 | // verifyReauthentication checks if the nonce provided is valid |
| 70 | func (a *API) verifyReauthentication(nonce string, tx *storage.Connection, config *conf.GlobalConfiguration, user *models.User) error { |
| 71 | if user.ReauthenticationToken == "" || user.ReauthenticationSentAt == nil { |
| 72 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeReauthenticationNotValid, InvalidNonceMessage) |
| 73 | } |
| 74 | var isValid bool |
| 75 | if user.GetEmail() != "" { |
| 76 | tokenHash := crypto.GenerateTokenHash(user.GetEmail(), nonce) |
| 77 | isValid = isOtpValid(tokenHash, user.ReauthenticationToken, user.ReauthenticationSentAt, config.Mailer.OtpExp) |
| 78 | } else if user.GetPhone() != "" { |
| 79 | if config.Sms.IsTwilioVerifyProvider() { |
| 80 | smsProvider, _ := sms_provider.GetSmsProvider(*config) |
| 81 | if err := smsProvider.(*sms_provider.TwilioVerifyProvider).VerifyOTP(string(user.Phone), nonce); err != nil { |
| 82 | return apierrors.NewForbiddenError(apierrors.ErrorCodeOTPExpired, "Token has expired or is invalid").WithInternalError(err) |
| 83 | } |
| 84 | return nil |
| 85 | } else { |
| 86 | tokenHash := crypto.GenerateTokenHash(user.GetPhone(), nonce) |
| 87 | isValid = isOtpValid(tokenHash, user.ReauthenticationToken, user.ReauthenticationSentAt, config.Sms.OtpExp) |
| 88 | } |
| 89 | } else { |
| 90 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeReauthenticationNotValid, "Reauthentication requires an email or a phone number") |
| 91 | } |
| 92 | if !isValid { |
| 93 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeReauthenticationNotValid, InvalidNonceMessage) |
| 94 | } |
| 95 | if err := user.ConfirmReauthentication(tx); err != nil { |
| 96 | return apierrors.NewInternalServerError("Error during reauthentication").WithInternalError(err) |
| 97 | } |
| 98 | return nil |
| 99 | } |
no test coverage detected