2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
# == AuthenticatesWithTwoFactor
|
|
|
|
#
|
|
|
|
# Controller concern to handle two-factor authentication
|
|
|
|
module AuthenticatesWithTwoFactor
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# Store the user's ID in the session for later retrieval and render the
|
|
|
|
# two factor code prompt
|
|
|
|
#
|
|
|
|
# The user must have been authenticated with a valid login and password
|
|
|
|
# before calling this method!
|
|
|
|
#
|
|
|
|
# user - User record
|
|
|
|
#
|
|
|
|
# Returns nil
|
|
|
|
def prompt_for_two_factor(user)
|
2018-10-15 14:42:47 +05:30
|
|
|
# Set @user for Devise views
|
|
|
|
@user = user # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return handle_locked_user(user) unless user.can?(:log_in)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
session[:otp_user_id] = user.id
|
2020-11-24 15:15:51 +05:30
|
|
|
session[:user_password_hash] = Digest::SHA256.hexdigest(user.encrypted_password)
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
add_gon_variables
|
2023-05-27 22:25:52 +05:30
|
|
|
setup_webauthn_authentication(user)
|
2020-09-03 11:15:55 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
render 'devise/sessions/two_factor'
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def handle_locked_user(user)
|
|
|
|
clear_two_factor_attempt!
|
|
|
|
|
|
|
|
locked_user_redirect(user)
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def locked_user_redirect(user)
|
2020-10-24 23:57:45 +05:30
|
|
|
redirect_to new_user_session_path, alert: locked_user_redirect_alert(user)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def authenticate_with_two_factor
|
|
|
|
user = self.resource = find_user
|
2020-03-13 15:44:24 +05:30
|
|
|
return handle_locked_user(user) unless user.can?(:log_in)
|
2020-11-24 15:15:51 +05:30
|
|
|
return handle_changed_user(user) if user_password_changed?(user)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if user_params[:otp_attempt].present? && session[:otp_user_id]
|
2016-06-16 23:09:34 +05:30
|
|
|
authenticate_with_two_factor_via_otp(user)
|
|
|
|
elsif user_params[:device_response].present? && session[:otp_user_id]
|
2023-05-27 22:25:52 +05:30
|
|
|
authenticate_with_two_factor_via_webauthn(user)
|
2016-06-16 23:09:34 +05:30
|
|
|
elsif user && user.valid_password?(user_params[:password])
|
|
|
|
prompt_for_two_factor(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def locked_user_redirect_alert(user)
|
2020-10-24 23:57:45 +05:30
|
|
|
if user.access_locked?
|
|
|
|
_('Your account is locked.')
|
|
|
|
elsif !user.confirmed?
|
|
|
|
I18n.t('devise.failure.unconfirmed')
|
|
|
|
else
|
2021-04-29 21:17:54 +05:30
|
|
|
_('Invalid login or password')
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def clear_two_factor_attempt!
|
|
|
|
session.delete(:otp_user_id)
|
2020-11-24 15:15:51 +05:30
|
|
|
session.delete(:user_password_hash)
|
2020-09-03 11:15:55 +05:30
|
|
|
session.delete(:challenge)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def authenticate_with_two_factor_via_otp(user)
|
|
|
|
if valid_otp_attempt?(user)
|
|
|
|
# Remove any lingering user data from login
|
2020-09-03 11:15:55 +05:30
|
|
|
clear_two_factor_attempt!
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
remember_me(user) if user_params[:remember_me] == '1'
|
2018-03-17 18:26:18 +05:30
|
|
|
user.save!
|
2019-10-12 21:52:04 +05:30
|
|
|
sign_in(user, message: :two_factor_authenticated, event: :authentication)
|
2016-06-16 23:09:34 +05:30
|
|
|
else
|
2022-11-25 23:54:43 +05:30
|
|
|
send_two_factor_otp_attempt_failed_email(user)
|
2021-01-03 14:25:43 +05:30
|
|
|
handle_two_factor_failure(user, 'OTP', _('Invalid two-factor code.'))
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def authenticate_with_two_factor_via_webauthn(user)
|
|
|
|
if Webauthn::AuthenticateService.new(user, user_params[:device_response], session[:challenge]).execute
|
|
|
|
handle_two_factor_success(user)
|
2016-06-16 23:09:34 +05:30
|
|
|
else
|
2021-01-03 14:25:43 +05:30
|
|
|
handle_two_factor_failure(user, 'WebAuthn', _('Authentication via WebAuthn device failed.'))
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Setup in preparation of communication with a U2F (universal 2nd factor) device
|
|
|
|
# Actual communication is performed using a Javascript API
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-16 23:09:34 +05:30
|
|
|
def setup_u2f_authentication(user)
|
|
|
|
key_handles = user.u2f_registrations.pluck(:key_handle)
|
|
|
|
u2f = U2F::U2F.new(u2f_app_id)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
if key_handles.present?
|
|
|
|
sign_requests = u2f.authentication_requests(key_handles)
|
2016-08-24 12:49:21 +05:30
|
|
|
session[:challenge] ||= u2f.challenge
|
|
|
|
gon.push(u2f: { challenge: session[:challenge], app_id: u2f_app_id,
|
|
|
|
sign_requests: sign_requests })
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def setup_webauthn_authentication(user)
|
|
|
|
if user.webauthn_registrations.present?
|
|
|
|
|
|
|
|
webauthn_registration_ids = user.webauthn_registrations.pluck(:credential_xid)
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
get_options = WebAuthn::Credential.options_for_get(
|
|
|
|
allow: webauthn_registration_ids,
|
|
|
|
user_verification: 'discouraged',
|
|
|
|
extensions: { appid: WebAuthn.configuration.origin }
|
|
|
|
)
|
2020-11-24 15:15:51 +05:30
|
|
|
session[:challenge] = get_options.challenge
|
2023-01-13 00:05:48 +05:30
|
|
|
gon.push(webauthn: { options: Gitlab::Json.dump(get_options) })
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-09-03 11:15:55 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def handle_two_factor_success(user)
|
|
|
|
# Remove any lingering user data from login
|
|
|
|
clear_two_factor_attempt!
|
|
|
|
|
|
|
|
remember_me(user) if user_params[:remember_me] == '1'
|
|
|
|
sign_in(user, message: :two_factor_authenticated, event: :authentication)
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def handle_two_factor_failure(user, method, message)
|
2020-11-24 15:15:51 +05:30
|
|
|
user.increment_failed_attempts!
|
2021-09-30 23:02:18 +05:30
|
|
|
log_failed_two_factor(user, method)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=#{method}")
|
2021-01-03 14:25:43 +05:30
|
|
|
flash.now[:alert] = message
|
2020-11-24 15:15:51 +05:30
|
|
|
prompt_for_two_factor(user)
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def send_two_factor_otp_attempt_failed_email(user)
|
|
|
|
user.notification_service.two_factor_otp_attempt_failed(user, request.remote_ip)
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def log_failed_two_factor(user, method)
|
2021-01-03 14:25:43 +05:30
|
|
|
# overridden in EE
|
|
|
|
end
|
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
def handle_changed_user(user)
|
|
|
|
clear_two_factor_attempt!
|
|
|
|
|
|
|
|
redirect_to new_user_session_path, alert: _('An error occurred. Please sign in again.')
|
|
|
|
end
|
|
|
|
|
|
|
|
# If user has been updated since we validated the password,
|
|
|
|
# the password might have changed.
|
2020-11-24 15:15:51 +05:30
|
|
|
def user_password_changed?(user)
|
|
|
|
return false unless session[:user_password_hash]
|
|
|
|
|
|
|
|
Digest::SHA256.hexdigest(user.encrypted_password) != session[:user_password_hash]
|
2020-09-03 11:15:55 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
AuthenticatesWithTwoFactor.prepend_mod_with('AuthenticatesWithTwoFactor')
|