2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-14 14:24:05 -04:00
|
|
|
# == 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-04-18 10:03:27 -04:00
|
|
|
# Set @user for Devise views
|
|
|
|
@user = user # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2020-01-29 22:08:50 -05:00
|
|
|
return handle_locked_user(user) unless user.can?(:log_in)
|
2016-09-02 09:30:19 -04:00
|
|
|
|
2015-05-14 14:24:05 -04:00
|
|
|
session[:otp_user_id] = user.id
|
2020-09-08 20:08:42 -04:00
|
|
|
session[:user_password_hash] = Digest::SHA256.hexdigest(user.encrypted_password)
|
2020-09-07 08:08:27 -04:00
|
|
|
push_frontend_feature_flag(:webauthn)
|
|
|
|
|
|
|
|
if user.two_factor_webauthn_enabled?
|
|
|
|
setup_webauthn_authentication(user)
|
|
|
|
else
|
|
|
|
setup_u2f_authentication(user)
|
|
|
|
end
|
2020-09-02 11:10:54 -04:00
|
|
|
|
2016-06-06 00:50:39 -04:00
|
|
|
render 'devise/sessions/two_factor'
|
|
|
|
end
|
|
|
|
|
2020-01-29 22:08:50 -05:00
|
|
|
def handle_locked_user(user)
|
|
|
|
clear_two_factor_attempt!
|
|
|
|
|
|
|
|
locked_user_redirect(user)
|
|
|
|
end
|
|
|
|
|
2016-09-02 09:30:19 -04:00
|
|
|
def locked_user_redirect(user)
|
2020-07-29 14:09:50 -04:00
|
|
|
redirect_to new_user_session_path, alert: locked_user_redirect_alert(user)
|
2016-09-02 09:30:19 -04:00
|
|
|
end
|
|
|
|
|
2016-06-06 00:50:39 -04:00
|
|
|
def authenticate_with_two_factor
|
|
|
|
user = self.resource = find_user
|
2020-01-29 22:08:50 -05:00
|
|
|
return handle_locked_user(user) unless user.can?(:log_in)
|
2020-09-08 20:08:42 -04:00
|
|
|
return handle_changed_user(user) if user_password_changed?(user)
|
2016-06-06 00:50:39 -04:00
|
|
|
|
2017-02-28 16:19:52 -05:00
|
|
|
if user_params[:otp_attempt].present? && session[:otp_user_id]
|
2016-06-06 00:50:39 -04:00
|
|
|
authenticate_with_two_factor_via_otp(user)
|
|
|
|
elsif user_params[:device_response].present? && session[:otp_user_id]
|
2020-09-07 08:08:27 -04:00
|
|
|
if user.two_factor_webauthn_enabled?
|
|
|
|
authenticate_with_two_factor_via_webauthn(user)
|
|
|
|
else
|
|
|
|
authenticate_with_two_factor_via_u2f(user)
|
|
|
|
end
|
2016-06-06 00:50:39 -04:00
|
|
|
elsif user && user.valid_password?(user_params[:password])
|
|
|
|
prompt_for_two_factor(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-01-29 22:08:50 -05:00
|
|
|
def locked_user_redirect_alert(user)
|
2020-07-29 14:09:50 -04:00
|
|
|
if user.access_locked?
|
|
|
|
_('Your account is locked.')
|
|
|
|
elsif !user.confirmed?
|
|
|
|
I18n.t('devise.failure.unconfirmed')
|
|
|
|
else
|
2021-03-17 17:11:29 -04:00
|
|
|
_('Invalid login or password')
|
2020-07-29 14:09:50 -04:00
|
|
|
end
|
2020-01-29 22:08:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_two_factor_attempt!
|
|
|
|
session.delete(:otp_user_id)
|
2020-09-08 20:08:42 -04:00
|
|
|
session.delete(:user_password_hash)
|
2020-09-02 11:10:54 -04:00
|
|
|
session.delete(:challenge)
|
2020-01-29 22:08:50 -05:00
|
|
|
end
|
|
|
|
|
2016-06-06 00:50:39 -04:00
|
|
|
def authenticate_with_two_factor_via_otp(user)
|
|
|
|
if valid_otp_attempt?(user)
|
|
|
|
# Remove any lingering user data from login
|
2020-09-02 11:10:54 -04:00
|
|
|
clear_two_factor_attempt!
|
2016-06-06 00:50:39 -04:00
|
|
|
|
|
|
|
remember_me(user) if user_params[:remember_me] == '1'
|
2018-02-09 10:02:11 -05:00
|
|
|
user.save!
|
2019-07-26 03:05:50 -04:00
|
|
|
sign_in(user, message: :two_factor_authenticated, event: :authentication)
|
2016-06-06 00:50:39 -04:00
|
|
|
else
|
2020-09-29 23:09:46 -04:00
|
|
|
handle_two_factor_failure(user, 'OTP', _('Invalid two-factor code.'))
|
2016-06-06 00:50:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticate using the response from a U2F (universal 2nd factor) device
|
|
|
|
def authenticate_with_two_factor_via_u2f(user)
|
2016-07-11 03:32:24 -04:00
|
|
|
if U2fRegistration.authenticate(user, u2f_app_id, user_params[:device_response], session[:challenge])
|
2020-09-07 08:08:27 -04:00
|
|
|
handle_two_factor_success(user)
|
|
|
|
else
|
2020-09-29 23:09:46 -04:00
|
|
|
handle_two_factor_failure(user, 'U2F', _('Authentication via U2F device failed.'))
|
2020-09-07 08:08:27 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-06 00:50:39 -04:00
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
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-06 00:50:39 -04:00
|
|
|
else
|
2020-09-29 23:09:46 -04:00
|
|
|
handle_two_factor_failure(user, 'WebAuthn', _('Authentication via WebAuthn device failed.'))
|
2016-06-06 00:50:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Setup in preparation of communication with a U2F (universal 2nd factor) device
|
|
|
|
# Actual communication is performed using a Javascript API
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-06 00:50:39 -04:00
|
|
|
def setup_u2f_authentication(user)
|
|
|
|
key_handles = user.u2f_registrations.pluck(:key_handle)
|
|
|
|
u2f = U2F::U2F.new(u2f_app_id)
|
2015-05-14 14:24:05 -04:00
|
|
|
|
2016-06-06 00:50:39 -04:00
|
|
|
if key_handles.present?
|
|
|
|
sign_requests = u2f.authentication_requests(key_handles)
|
2016-07-11 03:32:24 -04:00
|
|
|
session[:challenge] ||= u2f.challenge
|
|
|
|
gon.push(u2f: { challenge: session[:challenge], app_id: u2f_app_id,
|
2016-07-11 03:30:22 -04:00
|
|
|
sign_requests: sign_requests })
|
2016-06-06 00:50:39 -04:00
|
|
|
end
|
2015-05-14 14:24:05 -04:00
|
|
|
end
|
2020-09-07 08:08:27 -04:00
|
|
|
|
|
|
|
def setup_webauthn_authentication(user)
|
|
|
|
if user.webauthn_registrations.present?
|
|
|
|
|
|
|
|
webauthn_registration_ids = user.webauthn_registrations.pluck(:credential_xid)
|
|
|
|
|
|
|
|
get_options = WebAuthn::Credential.options_for_get(allow: webauthn_registration_ids,
|
|
|
|
user_verification: 'discouraged',
|
|
|
|
extensions: { appid: WebAuthn.configuration.origin })
|
|
|
|
|
|
|
|
session[:credentialRequestOptions] = get_options
|
|
|
|
session[:challenge] = get_options.challenge
|
|
|
|
gon.push(webauthn: { options: get_options.to_json })
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-09-02 11:10:54 -04:00
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
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
|
|
|
|
|
2020-09-29 23:09:46 -04:00
|
|
|
def handle_two_factor_failure(user, method, message)
|
2020-09-07 08:08:27 -04:00
|
|
|
user.increment_failed_attempts!
|
2020-09-29 23:09:46 -04:00
|
|
|
log_failed_two_factor(user, method, request.remote_ip)
|
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=#{method}")
|
2020-09-29 23:09:46 -04:00
|
|
|
flash.now[:alert] = message
|
2020-09-07 08:08:27 -04:00
|
|
|
prompt_for_two_factor(user)
|
|
|
|
end
|
|
|
|
|
2020-09-29 23:09:46 -04:00
|
|
|
def log_failed_two_factor(user, method, ip_address)
|
|
|
|
# overridden in EE
|
|
|
|
end
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
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-09-08 20:08:42 -04:00
|
|
|
def user_password_changed?(user)
|
|
|
|
return false unless session[:user_password_hash]
|
|
|
|
|
|
|
|
Digest::SHA256.hexdigest(user.encrypted_password) != session[:user_password_hash]
|
2020-09-02 11:10:54 -04:00
|
|
|
end
|
2015-05-14 14:24:05 -04:00
|
|
|
end
|
2020-09-29 23:09:46 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
AuthenticatesWithTwoFactor.prepend_mod_with('AuthenticatesWithTwoFactor')
|