gitlab-org--gitlab-foss/app/controllers/profiles/two_factor_auths_controller.rb

133 lines
4.5 KiB
Ruby
Raw Normal View History

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