2014-07-11 06:24:11 -04:00
|
|
|
class SessionsController < Devise::SessionsController
|
2015-05-14 14:24:05 -04:00
|
|
|
include AuthenticatesWithTwoFactor
|
2015-05-09 17:04:02 -04:00
|
|
|
|
2015-05-14 14:24:05 -04:00
|
|
|
prepend_before_action :authenticate_with_two_factor, only: [:create]
|
2015-08-12 11:20:01 -04:00
|
|
|
prepend_before_action :store_redirect_path, only: [:new]
|
2015-05-27 11:40:21 -04:00
|
|
|
before_action :auto_sign_in_with_provider, only: [:new]
|
2015-03-30 21:19:01 -04:00
|
|
|
|
2014-07-11 06:24:11 -04:00
|
|
|
def new
|
2014-10-13 07:39:54 -04:00
|
|
|
if Gitlab.config.ldap.enabled
|
2014-10-14 07:11:53 -04:00
|
|
|
@ldap_servers = Gitlab::LDAP::Config.servers
|
2015-08-31 06:59:52 -04:00
|
|
|
else
|
|
|
|
@ldap_servers = []
|
2014-10-13 07:39:54 -04:00
|
|
|
end
|
|
|
|
|
2014-07-11 06:24:11 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-04-08 14:26:04 -04:00
|
|
|
super do |resource|
|
2015-05-08 20:41:53 -04:00
|
|
|
# User has successfully signed in, so clear any unused reset token
|
2015-04-08 14:26:04 -04:00
|
|
|
if resource.reset_password_token.present?
|
|
|
|
resource.update_attributes(reset_password_token: nil,
|
|
|
|
reset_password_sent_at: nil)
|
|
|
|
end
|
2015-07-03 07:54:50 -04:00
|
|
|
authenticated_with = user_params[:otp_attempt] ? "two-factor" : "standard"
|
|
|
|
log_audit_event(current_user, with: authenticated_with)
|
2015-04-08 14:26:04 -04:00
|
|
|
end
|
2014-07-11 06:24:11 -04:00
|
|
|
end
|
2015-03-30 21:19:01 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-05-05 22:16:45 -04:00
|
|
|
def user_params
|
|
|
|
params.require(:user).permit(:login, :password, :remember_me, :otp_attempt)
|
|
|
|
end
|
|
|
|
|
2015-05-08 20:41:53 -04:00
|
|
|
def find_user
|
|
|
|
if user_params[:login]
|
|
|
|
User.by_login(user_params[:login])
|
|
|
|
elsif user_params[:otp_attempt] && session[:otp_user_id]
|
|
|
|
User.find(session[:otp_user_id])
|
|
|
|
end
|
|
|
|
end
|
2015-08-12 11:20:01 -04:00
|
|
|
|
|
|
|
def store_redirect_path
|
|
|
|
redirect_path =
|
|
|
|
if request.referer.present? && (params['redirect_to_referer'] == 'yes')
|
|
|
|
referer_uri = URI(request.referer)
|
|
|
|
if referer_uri.host == Gitlab.config.gitlab.host
|
|
|
|
referer_uri.path
|
|
|
|
else
|
|
|
|
request.fullpath
|
|
|
|
end
|
|
|
|
else
|
|
|
|
request.fullpath
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prevent a 'you are already signed in' message directly after signing:
|
|
|
|
# we should never redirect to '/users/sign_in' after signing in successfully.
|
|
|
|
unless redirect_path == new_user_session_path
|
|
|
|
store_location_for(:redirect, redirect_path)
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 20:41:53 -04:00
|
|
|
|
2015-05-05 22:16:45 -04:00
|
|
|
def authenticate_with_two_factor
|
2015-05-08 20:41:53 -04:00
|
|
|
user = self.resource = find_user
|
|
|
|
|
2015-06-19 15:14:37 -04:00
|
|
|
return unless user && user.two_factor_enabled?
|
2015-03-30 21:19:01 -04:00
|
|
|
|
2015-05-08 20:41:53 -04:00
|
|
|
if user_params[:otp_attempt].present? && session[:otp_user_id]
|
|
|
|
if valid_otp_attempt?(user)
|
|
|
|
# Remove any lingering user data from login
|
|
|
|
session.delete(:otp_user_id)
|
|
|
|
|
2015-05-09 17:04:02 -04:00
|
|
|
sign_in(user) and return
|
2015-05-05 22:16:45 -04:00
|
|
|
else
|
2015-05-09 17:02:06 -04:00
|
|
|
flash.now[:alert] = 'Invalid two-factor code.'
|
2015-03-30 21:19:01 -04:00
|
|
|
render :two_factor and return
|
|
|
|
end
|
|
|
|
else
|
2015-05-08 20:41:53 -04:00
|
|
|
if user && user.valid_password?(user_params[:password])
|
2015-05-14 14:24:05 -04:00
|
|
|
prompt_for_two_factor(user)
|
2015-03-30 21:19:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-05 22:16:45 -04:00
|
|
|
|
2015-05-27 11:40:21 -04:00
|
|
|
def auto_sign_in_with_provider
|
|
|
|
provider = Gitlab.config.omniauth.auto_sign_in_with_provider
|
|
|
|
return unless provider.present?
|
|
|
|
|
|
|
|
# Auto sign in with an Omniauth provider only if the standard "you need to sign-in" alert is
|
|
|
|
# registered or no alert at all. In case of another alert (such as a blocked user), it is safer
|
|
|
|
# to do nothing to prevent redirection loops with certain Omniauth providers.
|
|
|
|
return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
|
|
|
|
|
|
|
|
# Prevent alert from popping up on the first page shown after authentication.
|
|
|
|
flash[:alert] = nil
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
redirect_to user_omniauth_authorize_path(provider.to_sym)
|
2015-05-27 11:40:21 -04:00
|
|
|
end
|
|
|
|
|
2015-05-08 20:41:53 -04:00
|
|
|
def valid_otp_attempt?(user)
|
2015-09-19 21:16:18 -04:00
|
|
|
user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
|
2015-05-08 20:41:53 -04:00
|
|
|
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
|
2015-05-05 22:16:45 -04:00
|
|
|
end
|
2015-07-03 07:54:50 -04:00
|
|
|
|
|
|
|
def log_audit_event(user, options = {})
|
|
|
|
AuditEventService.new(user, user, options).
|
|
|
|
for_authentication.security_event
|
|
|
|
end
|
2014-07-11 06:24:11 -04:00
|
|
|
end
|