debian-mirror-gitlab/app/controllers/concerns/authenticates_with_two_factor.rb

113 lines
3.6 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
# == 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
setup_u2f_authentication(user)
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-03-13 15:44:24 +05:30
flash.now[:alert] = locked_user_redirect_alert(user)
2016-11-03 12:29:30 +05:30
render 'devise/sessions/new'
end
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)
2017-08-17 22:00:37 +05:30
if user_params[:otp_attempt].present? && session[:otp_user_id]
authenticate_with_two_factor_via_otp(user)
elsif user_params[:device_response].present? && session[:otp_user_id]
authenticate_with_two_factor_via_u2f(user)
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)
user.access_locked? ? _('Your account is locked.') : _('Invalid Login or password')
end
def clear_two_factor_attempt!
session.delete(:otp_user_id)
end
def authenticate_with_two_factor_via_otp(user)
if valid_otp_attempt?(user)
# Remove any lingering user data from login
session.delete(:otp_user_id)
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)
else
2016-11-03 12:29:30 +05:30
user.increment_failed_attempts!
2018-03-17 18:26:18 +05:30
Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=OTP")
2019-07-07 11:18:12 +05:30
flash.now[:alert] = _('Invalid two-factor code.')
2016-11-03 12:29:30 +05:30
prompt_for_two_factor(user)
end
end
# Authenticate using the response from a U2F (universal 2nd factor) device
def authenticate_with_two_factor_via_u2f(user)
2016-08-24 12:49:21 +05:30
if U2fRegistration.authenticate(user, u2f_app_id, user_params[:device_response], session[:challenge])
# Remove any lingering user data from login
session.delete(:otp_user_id)
2018-03-17 18:26:18 +05:30
session.delete(:challenge)
2016-09-29 09:46:39 +05:30
remember_me(user) if user_params[:remember_me] == '1'
2019-10-12 21:52:04 +05:30
sign_in(user, message: :two_factor_authenticated, event: :authentication)
else
2016-11-03 12:29:30 +05:30
user.increment_failed_attempts!
2018-03-17 18:26:18 +05:30
Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=U2F")
2019-07-07 11:18:12 +05:30
flash.now[:alert] = _('Authentication via U2F device failed.')
prompt_for_two_factor(user)
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
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
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 })
end
2015-09-11 14:41:01 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-09-11 14:41:01 +05:30
end