(conn *storage.Connection, params *VerifyParams)
| 641 | } |
| 642 | |
| 643 | func (a *API) verifyTokenHash(conn *storage.Connection, params *VerifyParams) (*models.User, error) { |
| 644 | config := a.config |
| 645 | |
| 646 | var user *models.User |
| 647 | var err error |
| 648 | switch params.Type { |
| 649 | case mail.EmailOTPVerification: |
| 650 | // need to find user by confirmation token or recovery token with the token hash |
| 651 | user, err = models.FindUserByConfirmationOrRecoveryToken(conn, params.TokenHash) |
| 652 | case mail.SignupVerification, mail.InviteVerification: |
| 653 | user, err = models.FindUserByConfirmationToken(conn, params.TokenHash) |
| 654 | case mail.RecoveryVerification, mail.MagicLinkVerification: |
| 655 | user, err = models.FindUserByRecoveryToken(conn, params.TokenHash) |
| 656 | case mail.EmailChangeVerification: |
| 657 | user, err = models.FindUserByEmailChangeToken(conn, params.TokenHash) |
| 658 | default: |
| 659 | return nil, apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Invalid email verification type") |
| 660 | } |
| 661 | |
| 662 | if err != nil { |
| 663 | if models.IsNotFoundError(err) { |
| 664 | return nil, apierrors.NewForbiddenError(apierrors.ErrorCodeOTPExpired, "Email link is invalid or has expired").WithInternalError(err) |
| 665 | } |
| 666 | return nil, apierrors.NewInternalServerError("Database error finding user from email link").WithInternalError(err) |
| 667 | } |
| 668 | |
| 669 | if user.IsBanned() { |
| 670 | return nil, apierrors.NewForbiddenError(apierrors.ErrorCodeUserBanned, "User is banned") |
| 671 | } |
| 672 | |
| 673 | var isExpired bool |
| 674 | switch params.Type { |
| 675 | case mail.EmailOTPVerification: |
| 676 | sentAt := user.ConfirmationSentAt |
| 677 | params.Type = "signup" |
| 678 | if user.RecoveryToken == params.TokenHash { |
| 679 | sentAt = user.RecoverySentAt |
| 680 | params.Type = "magiclink" |
| 681 | } |
| 682 | isExpired = isOtpExpired(sentAt, config.Mailer.OtpExp) |
| 683 | case mail.SignupVerification, mail.InviteVerification: |
| 684 | isExpired = isOtpExpired(user.ConfirmationSentAt, config.Mailer.OtpExp) |
| 685 | case mail.RecoveryVerification, mail.MagicLinkVerification: |
| 686 | isExpired = isOtpExpired(user.RecoverySentAt, config.Mailer.OtpExp) |
| 687 | case mail.EmailChangeVerification: |
| 688 | isExpired = isOtpExpired(user.EmailChangeSentAt, config.Mailer.OtpExp) |
| 689 | } |
| 690 | |
| 691 | if isExpired { |
| 692 | return nil, apierrors.NewForbiddenError(apierrors.ErrorCodeOTPExpired, "Email link is invalid or has expired").WithInternalMessage("email link has expired") |
| 693 | } |
| 694 | |
| 695 | return user, nil |
| 696 | } |
| 697 | |
| 698 | // verifyUserAndToken verifies the token associated to the user based on the verify type |
| 699 | func (a *API) verifyUserAndToken(conn *storage.Connection, params *VerifyParams, aud string) (*models.User, error) { |
no test coverage detected