Module: VerifiesWithEmail

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::DateHelper, VerifiesWithEmailHelper
Included in:
SessionsController
Defined in:
app/controllers/concerns/verifies_with_email.rb

Overview

VerifiesWithEmail

Controller concern to handle verification by email

Constant Summary collapse

VERIFICATION_REASON_UNTRUSTED_IP =
'sign in from untrusted IP address'
VERIFICATION_REASON_NEW_TOKEN_NEEDED =
'new unlock token needed'
VERIFICATION_REASON_EMAIL_OTP =
'email_otp'
VERIFICATION_REASON_LOCK_RESEND =
'resend lock verification code'
VERIFICATION_REASON_EMAIL_OTP_RESEND =
'resend email_otp code'

Instance Method Summary collapse

Methods included from VerifiesWithEmailHelper

#permitted_to_skip_email_otp_in_grace_period?, #treat_as_locked?, #trusted_ip_address?

Instance Method Details

#fallback_to_email_otpObject



128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/concerns/verifies_with_email.rb', line 128

def fallback_to_email_otp
  return respond_422 unless user = find_user

  if fallback_to_email_otp_permitted?(user)
    clear_two_factor_attempt!
    session[:verification_user_id] = user.id
    resend_verification_code
  else
    render json: { success: false, message: _('Not permitted.') }, status: :bad_request
  end
end

#resend_verification_codeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/concerns/verifies_with_email.rb', line 38

def resend_verification_code
  # Only a user who has started email verification can request a
  # resend
  return unless user = find_verification_user

  if send_rate_limited?(user)
    render_send_rate_limited
  else
    # If an email is provided, validate that it belongs to the user.
    # It is nil otherwise.
    secondary_email = fetch_confirmed_user_secondary_email(user, email_params[:email])
    primary_email = user.email

    # Both `send_` methods will regenerate the respective code, making
    # the old one invalid.
    # Only send email OTP when they're not locked and the FF is still
    # enabled.
    if !treat_as_locked?(user)
      if Feature.enabled?(:email_based_mfa, user)
        send_otp_with_email(
          user,
          secondary_email: secondary_email,
          reason: VERIFICATION_REASON_EMAIL_OTP_RESEND
        )
      else
        render json: {
          status: :failure,
          message: s_('IdentityVerification|Email Verification has ' \
            'been disabled and resending a code is not required. ' \
            'Log in again.')
        }
        return
      end
    # Only lock & send when they are locked.
    else
      lock_and_send_verification_instructions(
        user,
        secondary_email: secondary_email,
        reason: VERIFICATION_REASON_LOCK_RESEND
      )
    end

    if secondary_email.present? && secondary_email != primary_email
      send_notification_to_primary_email(primary_email, secondary_email)
    end

    render json: { status: :success }
  end
end

#skip_verification_confirmationObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/concerns/verifies_with_email.rb', line 111

def skip_verification_confirmation
  if permitted_to_view_skip_verification_confirmation?
    render 'skip_verification_confirmation',
      layout: 'minimal',
      locals: {
        redirect_url: (current_user),
        email_otp_required_after: current_user.email_otp_required_after
      }

    # remove verification_user_id from session to indicate that skip verification workflow is done
    # this ensures the confirmation page cannot be visited by user manually navigating to this path
    session.delete(:verification_user_id)
  else
    render json: { status: :failure }
  end
end

#skip_verification_for_nowObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/concerns/verifies_with_email.rb', line 95

def skip_verification_for_now
  return respond_422 unless user = find_verification_user
  return render_403 unless permitted_to_skip_email_otp_in_grace_period?(user)

  handle_verification_success(
    user,
    :skipped,
    'user chose to skip verification in grace period'
  )

  render json: {
    status: :success,
    redirect_path: users_skip_verification_confirmation_path
  }
end

#successful_verificationObject



88
89
90
91
92
93
# File 'app/controllers/concerns/verifies_with_email.rb', line 88

def successful_verification
  session.delete(:verification_user_id)
  @redirect_url = (current_user) # rubocop:disable Gitlab/ModuleWithInstanceVariables

  render layout: 'minimal'
end

#verify_with_emailObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/concerns/verifies_with_email.rb', line 21

def verify_with_email
  return unless user = find_user || find_verification_user
  return unless user.active?

  if session[:verification_user_id] && token = verification_params[:verification_token].presence
    # The verification token is submitted, verify it
    verify_token(user, token)
  elsif require_email_verification_enabled?(user)
    # Limit the amount of password guesses, since we now display the email verification page
    # when the password is correct, which could be a giveaway when brute-forced.
    return  if check_rate_limit!(:user_sign_in, scope: user) { true }

    # Verify the email if the user has logged in successfully.
    verify_email(user) if user.valid_password?(user_params[:password])
  end
end