debian-mirror-gitlab/app/controllers/sessions_controller.rb

131 lines
3.8 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class SessionsController < Devise::SessionsController
2015-09-11 14:41:01 +05:30
include AuthenticatesWithTwoFactor
include Devise::Controllers::Rememberable
include Recaptcha::ClientHelper
2015-09-11 14:41:01 +05:30
2016-04-02 18:10:28 +05:30
skip_before_action :check_2fa_requirement, only: [:destroy]
2016-06-02 11:05:42 +05:30
prepend_before_action :check_initial_setup, only: [:new]
prepend_before_action :authenticate_with_two_factor,
if: :two_factor_enabled?, only: [:create]
2015-09-11 14:41:01 +05:30
prepend_before_action :store_redirect_path, only: [:new]
2016-06-02 11:05:42 +05:30
2015-09-11 14:41:01 +05:30
before_action :auto_sign_in_with_provider, only: [:new]
before_action :load_recaptcha
2015-09-11 14:41:01 +05:30
2014-09-02 18:07:02 +05:30
def new
set_minimum_password_length
2015-09-11 14:41:01 +05:30
if Gitlab.config.ldap.enabled
@ldap_servers = Gitlab::LDAP::Config.servers
2015-09-25 12:07:36 +05:30
else
@ldap_servers = []
2015-09-11 14:41:01 +05:30
end
super
end
def create
super do |resource|
# User has successfully signed in, so clear any unused reset token
if resource.reset_password_token.present?
resource.update_attributes(reset_password_token: nil,
reset_password_sent_at: nil)
end
log_audit_event(current_user, with: authentication_method)
2015-09-11 14:41:01 +05:30
end
end
private
2016-06-02 11:05:42 +05:30
# Handle an "initial setup" state, where there's only one user, it's an admin,
# and they require a password change.
def check_initial_setup
return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
2016-06-02 11:05:42 +05:30
user = User.admins.last
return unless user && user.require_password?
token = user.generate_reset_token
user.save
redirect_to edit_user_password_path(reset_password_token: token),
notice: "Please create a password for your new account."
end
2015-09-11 14:41:01 +05:30
def user_params
params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
2015-09-11 14:41:01 +05:30
end
def find_user
2016-06-02 11:05:42 +05:30
if session[:otp_user_id]
2015-09-11 14:41:01 +05:30
User.find(session[:otp_user_id])
2016-06-02 11:05:42 +05:30
elsif user_params[:login]
User.by_login(user_params[:login])
2015-09-11 14:41:01 +05:30
end
end
2015-09-11 14:41:01 +05:30
def store_redirect_path
2015-04-26 12:48:37 +05:30
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
2014-09-02 18:07:02 +05:30
# Prevent a 'you are already signed in' message directly after signing:
# we should never redirect to '/users/sign_in' after signing in successfully.
2015-09-11 14:41:01 +05:30
unless redirect_path == new_user_session_path
2014-09-02 18:07:02 +05:30
store_location_for(:redirect, redirect_path)
end
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
def two_factor_enabled?
find_user.try(:two_factor_enabled?)
end
2015-09-11 14:41:01 +05:30
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
2015-09-11 14:41:01 +05:30
# to do nothing to prevent redirection loops with certain Omniauth providers.
return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
2015-09-11 14:41:01 +05:30
# Prevent alert from popping up on the first page shown after authentication.
flash[:alert] = nil
2016-09-13 17:45:13 +05:30
redirect_to omniauth_authorize_path(:user, provider)
2015-09-11 14:41:01 +05:30
end
def valid_otp_attempt?(user)
2015-09-25 12:07:36 +05:30
user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
2015-09-11 14:41:01 +05:30
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
end
def log_audit_event(user, options = {})
AuditEventService.new(user, user, options).
for_authentication.security_event
end
def load_recaptcha
Gitlab::Recaptcha.load_configurations!
end
def authentication_method
if user_params[:otp_attempt]
"two-factor"
elsif user_params[:device_response]
"two-factor-via-u2f-device"
else
"standard"
end
end
2014-09-02 18:07:02 +05:30
end