BigW Consortium Gitlab

sessions_controller.rb 4.48 KB
Newer Older
1
class SessionsController < Devise::SessionsController
2
  include AuthenticatesWithTwoFactor
3
  include Devise::Controllers::Rememberable
4
  include Recaptcha::ClientHelper
5

6
  skip_before_action :check_two_factor_requirement, only: [:destroy]
7

8
  prepend_before_action :check_initial_setup, only: [:new]
9 10
  prepend_before_action :authenticate_with_two_factor,
    if: :two_factor_enabled?, only: [:create]
11
  prepend_before_action :store_redirect_path, only: [:new]
12

13
  before_action :auto_sign_in_with_provider, only: [:new]
14
  before_action :load_recaptcha
15

16
  def new
17
    set_minimum_password_length
Douwe Maan committed
18 19 20 21 22 23
    @ldap_servers =
      if Gitlab.config.ldap.enabled
        Gitlab::LDAP::Config.servers
      else
        []
      end
24

25 26 27 28
    super
  end

  def create
29
    super do |resource|
30
      # User has successfully signed in, so clear any unused reset token
31 32 33 34
      if resource.reset_password_token.present?
        resource.update_attributes(reset_password_token: nil,
                                   reset_password_sent_at: nil)
      end
35 36
      # hide the signed-in notification
      flash[:notice] = nil
37
      log_audit_event(current_user, with: authentication_method)
38
      log_user_activity(current_user)
39
    end
40
  end
41

42 43 44 45 46 47
  def destroy
    super
    # hide the signed_out notice
    flash[:notice] = nil
  end

48 49
  private

50
  def login_counter
51
    @login_counter ||= Gitlab::Metrics.counter(:user_session_logins, 'User sign in count')
52 53
  end

54 55 56
  # 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
57
    return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
58 59 60 61 62

    user = User.admins.last

    return unless user && user.require_password?

63
    Users::UpdateService.new(user).execute do |user|
64 65
      @token = user.generate_reset_token
    end
66

67
    redirect_to edit_user_password_path(reset_password_token: @token),
68 69 70
      notice: "Please create a password for your new account."
  end

71
  def user_params
72
    params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
73 74
  end

75
  def find_user
76
    if session[:otp_user_id]
77
      User.find(session[:otp_user_id])
78 79
    elsif user_params[:login]
      User.by_login(user_params[:login])
80 81
    end
  end
82

83 84 85 86 87
  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
88
          referer_uri.request_uri
89 90 91 92 93 94 95 96 97
        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.
98
    unless URI(redirect_path).path == new_user_session_path
99 100 101
      store_location_for(:redirect, redirect_path)
    end
  end
102

103 104 105 106
  def two_factor_enabled?
    find_user.try(:two_factor_enabled?)
  end

107 108 109 110
  def auto_sign_in_with_provider
    provider = Gitlab.config.omniauth.auto_sign_in_with_provider
    return unless provider.present?

111 112 113 114
    # If a "auto_sign_in" query parameter is set to a falsy value, don't auto sign-in.
    # Otherwise, the default is to auto sign-in.
    return if Gitlab::Utils.to_boolean(params[:auto_sign_in]) == false

115 116
    # 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
117 118
    # to do nothing to prevent redirection loops with certain Omniauth providers.
    return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
119

120
    # Prevent alert from popping up on the first page shown after authentication.
121 122
    flash[:alert] = nil

123
    redirect_to omniauth_authorize_path(:user, provider)
124 125
  end

126
  def valid_otp_attempt?(user)
127
    user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
128
      user.invalidate_otp_backup_code!(user_params[:otp_attempt])
129
  end
130 131

  def log_audit_event(user, options = {})
132 133
    AuditEventService.new(user, user, options)
      .for_authentication.security_event
134
  end
135

136
  def log_user_activity(user)
137
    login_counter.increment
138 139 140
    Users::ActivityService.new(user, 'login').execute
  end

141 142 143
  def load_recaptcha
    Gitlab::Recaptcha.load_configurations!
  end
144 145 146 147 148 149 150 151 152 153

  def authentication_method
    if user_params[:otp_attempt]
      "two-factor"
    elsif user_params[:device_response]
      "two-factor-via-u2f-device"
    else
      "standard"
    end
  end
154
end