2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
|
2017-08-17 22:00:37 +05:30
|
|
|
skip_before_action :check_two_factor_requirement
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def show
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
Users::UpdateService.new(current_user, user: current_user).execute!
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
if two_factor_authentication_required? && !current_user.two_factor_enabled?
|
2017-08-17 22:00:37 +05:30
|
|
|
two_factor_authentication_reason(
|
|
|
|
global: lambda do
|
|
|
|
flash.now[:alert] =
|
2019-05-18 00:54:41 +05:30
|
|
|
s_('The global settings require you to enable Two-Factor Authentication for your account.')
|
2017-08-17 22:00:37 +05:30
|
|
|
end,
|
|
|
|
group: lambda do |groups|
|
2019-05-18 00:54:41 +05:30
|
|
|
flash.now[:alert] = groups_notification(groups)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
unless two_factor_grace_period_expired?
|
2016-04-02 18:10:28 +05:30
|
|
|
grace_period_deadline = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
|
2019-05-18 00:54:41 +05:30
|
|
|
flash.now[:alert] = flash.now[:alert] + s_(" You need to do this before %{grace_period_deadline}.") % { grace_period_deadline: l(grace_period_deadline) }
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
@qr_code = build_qr_code
|
2017-08-17 22:00:37 +05:30
|
|
|
@account_string = account_string
|
2016-06-16 23:09:34 +05:30
|
|
|
setup_u2f_registration
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-09-25 12:07:36 +05:30
|
|
|
if current_user.validate_and_consume_otp!(params[:pin_code])
|
2018-03-17 18:26:18 +05:30
|
|
|
Users::UpdateService.new(current_user, user: current_user, otp_required_for_login: true).execute! do |user|
|
2017-09-10 17:25:29 +05:30
|
|
|
@codes = user.generate_otp_backup_codes!
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
render 'create'
|
|
|
|
else
|
2019-05-18 00:54:41 +05:30
|
|
|
@error = s_('Invalid pin code')
|
2015-09-11 14:41:01 +05:30
|
|
|
@qr_code = build_qr_code
|
2016-06-16 23:09:34 +05:30
|
|
|
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-09-13 17:45:13 +05:30
|
|
|
@u2f_registration = U2fRegistration.register(current_user, u2f_app_id, u2f_registration_params, session[:challenges])
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
if @u2f_registration.persisted?
|
|
|
|
session.delete(:challenges)
|
2019-05-18 00:54:41 +05:30
|
|
|
redirect_to profile_two_factor_auth_path, notice: s_("Your U2F device was registered!")
|
2016-06-16 23:09:34 +05:30
|
|
|
else
|
|
|
|
@qr_code = build_qr_code
|
|
|
|
setup_u2f_registration
|
|
|
|
render :show
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def codes
|
2018-03-17 18:26:18 +05:30
|
|
|
Users::UpdateService.new(current_user, user: current_user).execute! do |user|
|
2017-09-10 17:25:29 +05:30
|
|
|
@codes = user.generate_otp_backup_codes!
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
current_user.disable_two_factor!
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
redirect_to profile_account_path, status: :found
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def skip
|
|
|
|
if two_factor_grace_period_expired?
|
2019-05-18 00:54:41 +05:30
|
|
|
redirect_to new_profile_two_factor_auth_path, alert: s_('Cannot skip two factor authentication setup')
|
2016-01-14 18:37:52 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
session[:skip_two_factor] = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
|
2016-01-14 18:37:52 +05:30
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def build_qr_code
|
2017-08-17 22:00:37 +05:30
|
|
|
uri = current_user.otp_provisioning_uri(account_string, issuer: issuer_host)
|
|
|
|
RQRCode.render_qrcode(uri, :svg, level: :m, unit: 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_string
|
|
|
|
"#{issuer_host}:#{current_user.email}"
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def issuer_host
|
|
|
|
Gitlab.config.gitlab.host
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
# 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-09-13 17:45:13 +05:30
|
|
|
@u2f_registrations = current_user.u2f_registrations
|
2016-06-16 23:09:34 +05:30
|
|
|
u2f = U2F::U2F.new(u2f_app_id)
|
|
|
|
|
|
|
|
registration_requests = u2f.registration_requests
|
2016-09-13 17:45:13 +05:30
|
|
|
sign_requests = u2f.authentication_requests(@u2f_registrations.map(&:key_handle))
|
2016-06-16 23:09:34 +05:30
|
|
|
session[:challenges] = registration_requests.map(&:challenge)
|
|
|
|
|
|
|
|
gon.push(u2f: { challenges: session[:challenges], app_id: u2f_app_id,
|
|
|
|
register_requests: registration_requests,
|
2016-08-24 12:49:21 +05:30
|
|
|
sign_requests: sign_requests })
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def u2f_registration_params
|
|
|
|
params.require(:u2f_registration).permit(:device_response, :name)
|
|
|
|
end
|
2019-05-18 00:54:41 +05:30
|
|
|
|
|
|
|
def groups_notification(groups)
|
|
|
|
group_links = groups.map { |group| view_context.link_to group.full_name, group_path(group) }.to_sentence
|
|
|
|
leave_group_links = groups.map { |group| view_context.link_to (s_("leave %{group_name}") % { group_name: group.full_name }), leave_group_members_path(group), remote: false, method: :delete}.to_sentence
|
|
|
|
|
|
|
|
s_(%{The group settings for %{group_links} require you to enable Two-Factor Authentication for your account. You can %{leave_group_links}.})
|
|
|
|
.html_safe % { group_links: group_links.html_safe, leave_group_links: leave_group_links.html_safe }
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|