BigW Consortium Gitlab

two_factor_auths_controller.rb 3.48 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
    @account_string = account_string
26
    setup_u2f_registration
27 28 29
  end

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

35
      render 'create'
36 37 38
    else
      @error = 'Invalid pin code'
      @qr_code = build_qr_code
39 40 41 42 43 44 45 46
      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
47
    @u2f_registration = U2fRegistration.register(current_user, u2f_app_id, u2f_registration_params, session[:challenges])
48

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

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

64
  def destroy
65
    current_user.disable_two_factor!
66 67 68

    redirect_to profile_account_path
  end
69

70
  def skip
71
    if two_factor_grace_period_expired?
72 73 74 75 76 77 78
      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

79 80 81
  private

  def build_qr_code
82
    uri = current_user.otp_provisioning_uri(account_string, issuer: issuer_host)
83 84
    RQRCode::render_qrcode(uri, :svg, level: :m, unit: 3)
  end
85

86 87 88 89
  def account_string
    "#{issuer_host}:#{current_user.email}"
  end

90 91 92
  def issuer_host
    Gitlab.config.gitlab.host
  end
93 94 95 96 97

  # 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
98
    @u2f_registrations = current_user.u2f_registrations
99 100 101
    u2f = U2F::U2F.new(u2f_app_id)

    registration_requests = u2f.registration_requests
102
    sign_requests = u2f.authentication_requests(@u2f_registrations.map(&:key_handle))
103 104 105 106
    session[:challenges] = registration_requests.map(&:challenge)

    gon.push(u2f: { challenges: session[:challenges], app_id: u2f_app_id,
                    register_requests: registration_requests,
107
                    sign_requests: sign_requests })
108
  end
109 110 111 112

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