BigW Consortium Gitlab

two_factor_auths_controller.rb 3.43 KB
Newer Older
1
class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
2
  skip_before_action :check_2fa_requirement
3

4
  def show
5
    unless current_user.otp_secret
6
      current_user.otp_secret = User.generate_otp_secret(32)
7
    end
8

9 10 11
    unless current_user.otp_grace_period_started_at && two_factor_grace_period
      current_user.otp_grace_period_started_at = Time.current
    end
12

13
    current_user.save! if current_user.changed?
14

15
    if two_factor_authentication_required? && !current_user.two_factor_enabled?
16
      if two_factor_grace_period_expired?
17
        flash.now[:alert] = 'You must enable Two-Factor Authentication for your account.'
18 19
      else
        grace_period_deadline = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
20
        flash.now[:alert] = "You must enable Two-Factor Authentication for your account before #{l(grace_period_deadline)}."
21
      end
22 23
    end

24
    @qr_code = build_qr_code
25
    setup_u2f_registration
26 27 28
  end

  def create
29
    if current_user.validate_and_consume_otp!(params[:pin_code])
30
      current_user.otp_required_for_login = true
31
      @codes = current_user.generate_otp_backup_codes!
32
      current_user.save!
33

34
      render 'create'
35 36 37
    else
      @error = 'Invalid pin code'
      @qr_code = build_qr_code
38 39 40 41 42 43 44 45
      setup_u2f_registration
      render 'show'
    end
  end

  # A U2F (universal 2nd factor) device's information is stored after successful
  # registration, which is then used while 2FA authentication is taking place.
  def create_u2f
46
    @u2f_registration = U2fRegistration.register(current_user, u2f_app_id, u2f_registration_params, session[:challenges])
47

48 49
    if @u2f_registration.persisted?
      session.delete(:challenges)
50
      redirect_to profile_two_factor_auth_path, notice: "Your U2F device was registered!"
51 52 53 54
    else
      @qr_code = build_qr_code
      setup_u2f_registration
      render :show
55
    end
56 57
  end

58
  def codes
59
    @codes = current_user.generate_otp_backup_codes!
60 61 62
    current_user.save!
  end

63
  def destroy
64
    current_user.disable_two_factor!
65 66 67

    redirect_to profile_account_path
  end
68

69
  def skip
70
    if two_factor_grace_period_expired?
71 72 73 74 75 76 77
      redirect_to new_profile_two_factor_auth_path, alert: 'Cannot skip two factor authentication setup'
    else
      session[:skip_tfa] = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
      redirect_to root_path
    end
  end

78 79 80
  private

  def build_qr_code
81
    issuer = "#{issuer_host} | #{current_user.email}"
82 83 84
    uri = current_user.otp_provisioning_uri(current_user.email, issuer: issuer)
    RQRCode::render_qrcode(uri, :svg, level: :m, unit: 3)
  end
85 86 87 88

  def issuer_host
    Gitlab.config.gitlab.host
  end
89 90 91 92 93

  # Setup in preparation of communication with a U2F (universal 2nd factor) device
  # Actual communication is performed using a Javascript API
  def setup_u2f_registration
    @u2f_registration ||= U2fRegistration.new
94
    @u2f_registrations = current_user.u2f_registrations
95 96 97
    u2f = U2F::U2F.new(u2f_app_id)

    registration_requests = u2f.registration_requests
98
    sign_requests = u2f.authentication_requests(@u2f_registrations.map(&:key_handle))
99 100 101 102
    session[:challenges] = registration_requests.map(&:challenge)

    gon.push(u2f: { challenges: session[:challenges], app_id: u2f_app_id,
                    register_requests: registration_requests,
103
                    sign_requests: sign_requests })
104
  end
105 106 107 108

  def u2f_registration_params
    params.require(:u2f_registration).permit(:device_response, :name)
  end
109
end