2015-09-11 14:41:01 +05:30
|
|
|
class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
|
2016-01-14 18:37:52 +05:30
|
|
|
skip_before_action :check_2fa_requirement
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def new
|
|
|
|
unless current_user.otp_secret
|
|
|
|
current_user.otp_secret = User.generate_otp_secret(32)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless current_user.otp_grace_period_started_at && two_factor_grace_period
|
|
|
|
current_user.otp_grace_period_started_at = Time.current
|
|
|
|
end
|
|
|
|
|
|
|
|
current_user.save! if current_user.changed?
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
if two_factor_authentication_required?
|
|
|
|
if two_factor_grace_period_expired?
|
|
|
|
flash.now[:alert] = 'You must enable Two-factor Authentication for your account.'
|
|
|
|
else
|
|
|
|
grace_period_deadline = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
|
|
|
|
flash.now[:alert] = "You must enable Two-factor Authentication for your account before #{l(grace_period_deadline)}."
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
@qr_code = build_qr_code
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-09-25 12:07:36 +05:30
|
|
|
if current_user.validate_and_consume_otp!(params[:pin_code])
|
2015-09-11 14:41:01 +05:30
|
|
|
current_user.two_factor_enabled = true
|
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
|
|
|
current_user.save!
|
|
|
|
|
|
|
|
render 'create'
|
|
|
|
else
|
|
|
|
@error = 'Invalid pin code'
|
|
|
|
@qr_code = build_qr_code
|
|
|
|
|
|
|
|
render 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def codes
|
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
|
|
|
current_user.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
current_user.disable_two_factor!
|
|
|
|
|
|
|
|
redirect_to profile_account_path
|
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def skip
|
|
|
|
if two_factor_grace_period_expired?
|
|
|
|
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
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def build_qr_code
|
|
|
|
issuer = "#{issuer_host} | #{current_user.email}"
|
|
|
|
uri = current_user.otp_provisioning_uri(current_user.email, issuer: issuer)
|
|
|
|
RQRCode::render_qrcode(uri, :svg, level: :m, unit: 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def issuer_host
|
|
|
|
Gitlab.config.gitlab.host
|
|
|
|
end
|
|
|
|
end
|