2015-05-06 17:35:55 +00:00
|
|
|
class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
|
2015-12-24 20:58:46 +00:00
|
|
|
skip_before_action :check_2fa_requirement
|
2015-12-18 20:29:13 +00:00
|
|
|
|
2015-03-28 00:01:24 +00:00
|
|
|
def new
|
2015-03-28 00:53:08 +00:00
|
|
|
unless current_user.otp_secret
|
2015-05-23 22:47:53 +00:00
|
|
|
current_user.otp_secret = User.generate_otp_secret(32)
|
2015-03-28 00:53:08 +00:00
|
|
|
end
|
2015-12-24 20:58:46 +00:00
|
|
|
|
2015-12-24 02:02:52 +00:00
|
|
|
unless current_user.otp_grace_period_started_at && two_factor_grace_period
|
|
|
|
current_user.otp_grace_period_started_at = Time.current
|
|
|
|
end
|
2015-12-24 20:58:46 +00:00
|
|
|
|
2015-12-24 02:02:52 +00:00
|
|
|
current_user.save! if current_user.changed?
|
2015-03-28 00:53:08 +00:00
|
|
|
|
2016-02-29 18:58:36 +00:00
|
|
|
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-12-24 04:04:41 +00:00
|
|
|
end
|
|
|
|
|
2015-03-28 00:53:08 +00:00
|
|
|
@qr_code = build_qr_code
|
2015-03-28 00:01:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-09-20 01:16:18 +00:00
|
|
|
if current_user.validate_and_consume_otp!(params[:pin_code])
|
2015-06-19 19:14:37 +00:00
|
|
|
current_user.two_factor_enabled = true
|
2015-04-02 18:57:36 +00:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2015-03-28 00:53:08 +00:00
|
|
|
current_user.save!
|
2015-03-28 00:01:24 +00:00
|
|
|
|
2015-04-02 18:57:36 +00:00
|
|
|
render 'create'
|
2015-03-28 00:53:08 +00:00
|
|
|
else
|
|
|
|
@error = 'Invalid pin code'
|
|
|
|
@qr_code = build_qr_code
|
2015-05-22 22:33:44 +00:00
|
|
|
|
2015-03-28 00:53:08 +00:00
|
|
|
render 'new'
|
|
|
|
end
|
2015-03-28 00:01:24 +00:00
|
|
|
end
|
|
|
|
|
2015-03-31 19:42:17 +00:00
|
|
|
def codes
|
2015-04-02 18:57:36 +00:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2015-03-31 19:42:17 +00:00
|
|
|
current_user.save!
|
|
|
|
end
|
|
|
|
|
2015-03-28 00:01:24 +00:00
|
|
|
def destroy
|
2015-07-10 20:08:39 +00:00
|
|
|
current_user.disable_two_factor!
|
2015-03-28 00:01:24 +00:00
|
|
|
|
|
|
|
redirect_to profile_account_path
|
|
|
|
end
|
2015-03-28 00:53:08 +00:00
|
|
|
|
2015-12-24 02:02:52 +00:00
|
|
|
def skip
|
2015-12-24 04:04:41 +00:00
|
|
|
if two_factor_grace_period_expired?
|
2015-12-24 02:02:52 +00:00
|
|
|
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-03-28 00:53:08 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def build_qr_code
|
2015-06-15 22:12:32 +00:00
|
|
|
issuer = "#{issuer_host} | #{current_user.email}"
|
2015-03-28 00:53:08 +00:00
|
|
|
uri = current_user.otp_provisioning_uri(current_user.email, issuer: issuer)
|
|
|
|
RQRCode::render_qrcode(uri, :svg, level: :m, unit: 3)
|
|
|
|
end
|
2015-06-15 22:12:32 +00:00
|
|
|
|
|
|
|
def issuer_host
|
|
|
|
Gitlab.config.gitlab.host
|
|
|
|
end
|
2015-03-28 00:01:24 +00:00
|
|
|
end
|