debian-mirror-gitlab/app/controllers/profiles/two_factor_auths_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

229 lines
7.5 KiB
Ruby
Raw Normal View History

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
2021-11-11 11:23:49 +05:30
before_action :ensure_verified_primary_email, only: [:show, :create]
2023-04-23 21:23:45 +05:30
before_action :validate_current_password, only: [:create, :codes, :destroy, :create_webauthn], if: :current_password_required?
2022-06-21 17:19:12 +05:30
before_action :update_current_user_otp!, only: [:show]
2021-09-30 23:02:18 +05:30
2021-11-11 11:23:49 +05:30
helper_method :current_password_required?
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
feature_category :system_access
2021-01-03 14:25:43 +05:30
def show
2022-11-25 23:54:43 +05:30
setup_show_page
2015-09-11 14:41:01 +05:30
end
def create
2021-01-03 14:25:43 +05:30
otp_validation_result =
2022-07-16 23:28:13 +05:30
::Users::ValidateManualOtpService.new(current_user).execute(params[:pin_code])
2023-04-23 21:23:45 +05:30
validated = (otp_validation_result[:status] == :success)
2021-01-03 14:25:43 +05:30
2023-04-23 21:23:45 +05:30
if validated && current_user.otp_backup_codes? && Feature.enabled?(:webauthn_without_totp)
ActiveSession.destroy_all_but_current(current_user, session)
Users::UpdateService.new(current_user, user: current_user, otp_required_for_login: true).execute!
redirect_to profile_two_factor_auth_path, notice: _("Your Time-based OTP device was registered!")
elsif validated
2020-09-03 11:15:55 +05:30
ActiveSession.destroy_all_but_current(current_user, session)
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
2021-11-11 11:23:49 +05:30
helpers.dismiss_two_factor_auth_recovery_settings_check
2021-10-27 15:23:28 +05:30
2015-09-11 14:41:01 +05:30
render 'create'
else
2021-12-11 22:18:48 +05:30
@error = { message: _('Invalid pin code.') }
2015-09-11 14:41:01 +05:30
@qr_code = build_qr_code
2022-06-21 17:19:12 +05:30
@account_string = account_string
2023-05-27 22:25:52 +05:30
setup_webauthn_registration
2020-11-24 15:15:51 +05:30
render 'show'
end
end
2020-11-24 15:15:51 +05:30
def create_webauthn
@webauthn_registration = Webauthn::RegisterService.new(current_user, device_registration_params, session[:challenge]).execute
2023-04-23 21:23:45 +05:30
notice = _("Your WebAuthn device was registered!")
2020-11-24 15:15:51 +05:30
if @webauthn_registration.persisted?
session.delete(:challenge)
2023-04-23 21:23:45 +05:30
if Feature.enabled?(:webauthn_without_totp)
if current_user.otp_backup_codes?
redirect_to profile_two_factor_auth_path, notice: notice
else
Users::UpdateService.new(current_user, user: current_user).execute! do |user|
@codes = current_user.generate_otp_backup_codes!
end
helpers.dismiss_two_factor_auth_recovery_settings_check
flash[:notice] = notice
render 'create'
end
else
redirect_to profile_two_factor_auth_path, notice: notice
end
2020-11-24 15:15:51 +05:30
else
@qr_code = build_qr_code
setup_webauthn_registration
render :show
end
end
2015-09-11 14:41:01 +05:30
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!
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
helpers.dismiss_two_factor_auth_recovery_settings_check
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
end
def destroy
2020-11-24 15:15:51 +05:30
result = TwoFactor::DestroyService.new(current_user, user: current_user).execute
2015-09-11 14:41:01 +05:30
2020-11-24 15:15:51 +05:30
if result[:status] == :success
redirect_to profile_account_path, status: :found, notice: s_('Two-factor authentication has been disabled successfully!')
else
redirect_to profile_account_path, status: :found, alert: result[:message]
end
2015-09-11 14:41:01 +05:30
end
def skip
if two_factor_grace_period_expired?
2023-03-04 22:38:38 +05:30
redirect_to new_profile_two_factor_auth_path, alert: _('Cannot skip two factor authentication setup')
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
redirect_to root_path
end
end
2015-09-11 14:41:01 +05:30
private
2022-06-21 17:19:12 +05:30
def update_current_user_otp!
if current_user.needs_new_otp_secret?
current_user.update_otp_secret!
end
unless current_user.otp_grace_period_started_at && two_factor_grace_period
current_user.otp_grace_period_started_at = Time.current
end
Users::UpdateService.new(current_user, user: current_user).execute!
end
2021-09-30 23:02:18 +05:30
def validate_current_password
2023-04-23 21:23:45 +05:30
return if Feature.disabled?(:webauthn_without_totp) && params[:action] == 'create_webauthn'
2021-09-30 23:02:18 +05:30
return if current_user.valid_password?(params[:current_password])
current_user.increment_failed_attempts!
2023-04-23 21:23:45 +05:30
error_message = { message: _('You must provide a valid current password.') }
if params[:action] == 'create_webauthn'
@webauthn_error = error_message
else
@error = error_message
end
2022-11-25 23:54:43 +05:30
setup_show_page
render 'show'
2021-09-30 23:02:18 +05:30
end
2021-11-11 11:23:49 +05:30
def current_password_required?
2021-12-07 22:27:20 +05:30
!current_user.password_automatically_set? && current_user.allow_password_authentication_for_web?
2021-11-11 11:23:49 +05:30
end
2015-09-11 14:41:01 +05:30
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
2020-11-24 15:15:51 +05:30
def device_registration_params
params.require(:device_registration).permit(:device_response, :name)
end
def setup_webauthn_registration
@registrations = webauthn_registrations
@webauthn_registration ||= WebauthnRegistration.new
unless current_user.webauthn_xid
current_user.user_detail.update!(webauthn_xid: WebAuthn.generate_user_id)
end
options = webauthn_options
session[:challenge] = options.challenge
gon.push(webauthn: { options: options, app_id: u2f_app_id })
end
def webauthn_registrations
current_user.webauthn_registrations.map do |webauthn_registration|
{
2023-03-04 22:38:38 +05:30
name: webauthn_registration.name,
created_at: webauthn_registration.created_at,
delete_path: profile_webauthn_registration_path(webauthn_registration)
2020-11-24 15:15:51 +05:30
}
end
end
def webauthn_options
WebAuthn::Credential.options_for_create(
user: { id: current_user.webauthn_xid, name: current_user.username },
2023-03-17 16:20:25 +05:30
exclude: current_user.webauthn_registrations.map(&:credential_xid),
2020-11-24 15:15:51 +05:30
authenticator_selection: { user_verification: 'discouraged' },
rp: { name: 'GitLab' }
)
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
def groups_notification(groups)
group_links = groups.map { |group| view_context.link_to group.full_name, group_path(group) }.to_sentence
2022-08-27 11:52:29 +05:30
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
2019-07-07 11:18:12 +05:30
2023-03-04 22:38:38 +05:30
s_(%(The group settings for %{group_links} require you to enable Two-Factor Authentication for your account. You can %{leave_group_links}.))
2020-11-24 15:15:51 +05:30
.html_safe % { group_links: group_links.html_safe, leave_group_links: leave_group_links.html_safe }
2019-07-07 11:18:12 +05:30
end
2021-11-11 11:23:49 +05:30
def ensure_verified_primary_email
unless current_user.two_factor_enabled? || current_user.primary_email_verified?
redirect_to profile_emails_path, notice: s_('You need to verify your primary email first before enabling Two-Factor Authentication.')
end
end
2022-11-25 23:54:43 +05:30
def setup_show_page
if two_factor_authentication_required? && !current_user.two_factor_enabled?
two_factor_authentication_reason(
global: lambda do
flash.now[:alert] =
_('The global settings require you to enable Two-Factor Authentication for your account.')
end,
group: lambda do |groups|
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
flash.now[:alert] = flash.now[:alert] + _(" You need to do this before %{grace_period_deadline}.") % { grace_period_deadline: l(grace_period_deadline) }
end
end
@qr_code = build_qr_code
@account_string = account_string
2023-06-20 00:43:36 +05:30
setup_webauthn_registration
2022-11-25 23:54:43 +05:30
end
2015-09-11 14:41:01 +05:30
end