2015-05-06 13:35:55 -04:00
|
|
|
class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
|
2015-12-24 15:58:46 -05:00
|
|
|
skip_before_action :check_2fa_requirement
|
2015-12-18 15:29:13 -05:00
|
|
|
|
2016-06-06 00:44:51 -04:00
|
|
|
def show
|
2015-03-27 20:53:08 -04:00
|
|
|
unless current_user.otp_secret
|
2015-05-23 18:47:53 -04:00
|
|
|
current_user.otp_secret = User.generate_otp_secret(32)
|
2015-03-27 20:53:08 -04:00
|
|
|
end
|
2015-12-24 15:58:46 -05:00
|
|
|
|
2015-12-23 21:02:52 -05: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 15:58:46 -05:00
|
|
|
|
2015-12-23 21:02:52 -05:00
|
|
|
current_user.save! if current_user.changed?
|
2015-03-27 20:53:08 -04:00
|
|
|
|
2016-06-06 00:44:51 -04:00
|
|
|
if two_factor_authentication_required? && !current_user.two_factor_enabled?
|
2016-02-29 13:58:36 -05:00
|
|
|
if two_factor_grace_period_expired?
|
2016-06-06 00:44:51 -04:00
|
|
|
flash.now[:alert] = 'You must enable Two-Factor Authentication for your account.'
|
2016-02-29 13:58:36 -05:00
|
|
|
else
|
|
|
|
grace_period_deadline = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
|
2016-06-06 00:44:51 -04:00
|
|
|
flash.now[:alert] = "You must enable Two-Factor Authentication for your account before #{l(grace_period_deadline)}."
|
2016-02-29 13:58:36 -05:00
|
|
|
end
|
2015-12-23 23:04:41 -05:00
|
|
|
end
|
|
|
|
|
2015-03-27 20:53:08 -04:00
|
|
|
@qr_code = build_qr_code
|
2016-10-17 10:39:14 -04:00
|
|
|
@account_string = account_string
|
2016-06-06 00:44:51 -04:00
|
|
|
setup_u2f_registration
|
2015-03-27 20:01:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-09-19 21:16:18 -04:00
|
|
|
if current_user.validate_and_consume_otp!(params[:pin_code])
|
2016-06-06 00:44:51 -04:00
|
|
|
current_user.otp_required_for_login = true
|
2015-04-02 14:57:36 -04:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2015-03-27 20:53:08 -04:00
|
|
|
current_user.save!
|
2015-03-27 20:01:24 -04:00
|
|
|
|
2015-04-02 14:57:36 -04:00
|
|
|
render 'create'
|
2015-03-27 20:53:08 -04:00
|
|
|
else
|
|
|
|
@error = 'Invalid pin code'
|
|
|
|
@qr_code = build_qr_code
|
2016-06-06 00:44:51 -04:00
|
|
|
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
|
2016-08-16 12:18:36 -04:00
|
|
|
@u2f_registration = U2fRegistration.register(current_user, u2f_app_id, u2f_registration_params, session[:challenges])
|
2015-05-22 18:33:44 -04:00
|
|
|
|
2016-06-06 00:44:51 -04:00
|
|
|
if @u2f_registration.persisted?
|
|
|
|
session.delete(:challenges)
|
2016-08-16 12:18:36 -04:00
|
|
|
redirect_to profile_two_factor_auth_path, notice: "Your U2F device was registered!"
|
2016-06-06 00:44:51 -04:00
|
|
|
else
|
|
|
|
@qr_code = build_qr_code
|
|
|
|
setup_u2f_registration
|
|
|
|
render :show
|
2015-03-27 20:53:08 -04:00
|
|
|
end
|
2015-03-27 20:01:24 -04:00
|
|
|
end
|
|
|
|
|
2015-03-31 15:42:17 -04:00
|
|
|
def codes
|
2015-04-02 14:57:36 -04:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2015-03-31 15:42:17 -04:00
|
|
|
current_user.save!
|
|
|
|
end
|
|
|
|
|
2015-03-27 20:01:24 -04:00
|
|
|
def destroy
|
2015-07-10 16:08:39 -04:00
|
|
|
current_user.disable_two_factor!
|
2015-03-27 20:01:24 -04:00
|
|
|
|
|
|
|
redirect_to profile_account_path
|
|
|
|
end
|
2015-03-27 20:53:08 -04:00
|
|
|
|
2015-12-23 21:02:52 -05:00
|
|
|
def skip
|
2015-12-23 23:04:41 -05:00
|
|
|
if two_factor_grace_period_expired?
|
2015-12-23 21:02:52 -05: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-27 20:53:08 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def build_qr_code
|
2016-10-17 10:39:14 -04:00
|
|
|
uri = current_user.otp_provisioning_uri(account_string, issuer: issuer_host)
|
2015-03-27 20:53:08 -04:00
|
|
|
RQRCode::render_qrcode(uri, :svg, level: :m, unit: 3)
|
|
|
|
end
|
2015-06-15 18:12:32 -04:00
|
|
|
|
2016-10-17 10:39:14 -04:00
|
|
|
def account_string
|
|
|
|
"#{issuer_host}:#{current_user.email}"
|
|
|
|
end
|
|
|
|
|
2015-06-15 18:12:32 -04:00
|
|
|
def issuer_host
|
|
|
|
Gitlab.config.gitlab.host
|
|
|
|
end
|
2016-06-06 00:44:51 -04:00
|
|
|
|
|
|
|
# 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
|
2016-08-16 12:18:36 -04:00
|
|
|
@u2f_registrations = current_user.u2f_registrations
|
2016-06-06 00:44:51 -04:00
|
|
|
u2f = U2F::U2F.new(u2f_app_id)
|
|
|
|
|
|
|
|
registration_requests = u2f.registration_requests
|
2016-08-18 02:58:36 -04:00
|
|
|
sign_requests = u2f.authentication_requests(@u2f_registrations.map(&:key_handle))
|
2016-06-06 00:44:51 -04:00
|
|
|
session[:challenges] = registration_requests.map(&:challenge)
|
|
|
|
|
|
|
|
gon.push(u2f: { challenges: session[:challenges], app_id: u2f_app_id,
|
|
|
|
register_requests: registration_requests,
|
2016-07-11 03:30:22 -04:00
|
|
|
sign_requests: sign_requests })
|
2016-06-06 00:44:51 -04:00
|
|
|
end
|
2016-08-16 12:18:36 -04:00
|
|
|
|
|
|
|
def u2f_registration_params
|
|
|
|
params.require(:u2f_registration).permit(:device_response, :name)
|
|
|
|
end
|
2015-03-27 20:01:24 -04:00
|
|
|
end
|