omniauth_callbacks_controller.rb 4.1 KB
Newer Older
V
vsizov 已提交
1
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2
  include AuthenticatesWithTwoFactor
A
Alex Lossent 已提交
3

T
tduehr 已提交
4
  protect_from_forgery except: [:kerberos, :saml, :cas3]
A
Alex Lossent 已提交
5

6
  Gitlab.config.omniauth.providers.each do |provider|
D
Dmitriy Zaporozhets 已提交
7 8 9 10
    define_method provider['name'] do
      handle_omniauth
    end
  end
11 12 13 14

  # Extend the standard message generation to accept our custom exception
  def failure_message
    exception = env["omniauth.error"]
15 16 17 18
    error   = exception.error_reason if exception.respond_to?(:error_reason)
    error ||= exception.error        if exception.respond_to?(:error)
    error ||= exception.message      if exception.respond_to?(:message)
    error ||= env["omniauth.error.type"].to_s
19 20
    error.to_s.humanize if error
  end
F
Florian Unglaub 已提交
21

22 23
  # We only find ourselves here
  # if the authentication to LDAP was successful.
V
vsizov 已提交
24
  def ldap
25 26 27 28 29
    ldap_user = Gitlab::LDAP::User.new(oauth)
    ldap_user.save if ldap_user.changed? # will also save new users

    @user = ldap_user.gl_user
    @user.remember_me = params[:remember_me] if ldap_user.persisted?
30

31
    # Do additional LDAP checks for the user filter and EE features
32
    if ldap_user.allowed?
33 34 35 36 37 38
      if @user.two_factor_enabled?
        prompt_for_two_factor(@user)
      else
        log_audit_event(@user, with: :ldap)
        sign_in_and_redirect(@user)
      end
39 40 41
    else
      flash[:alert] = "Access denied for your LDAP account."
      redirect_to new_user_session_path
42
    end
V
vsizov 已提交
43 44
  end

45 46 47 48 49 50 51 52 53 54 55 56 57
  def saml
    if current_user
      log_audit_event(current_user, with: :saml)
      # Update SAML identity if data has changed.
      identity = current_user.identities.find_by(extern_uid: oauth['uid'], provider: :saml)
      if identity.nil?
        current_user.identities.create(extern_uid: oauth['uid'], provider: :saml)
        redirect_to profile_account_path, notice: 'Authentication method updated'
      else
        redirect_to after_sign_in_path_for(current_user)
      end
    else
      saml_user = Gitlab::Saml::User.new(oauth)
58
      saml_user.save if saml_user.changed?
59 60 61 62
      @user = saml_user.gl_user

      continue_login_process
    end
63 64
  rescue Gitlab::OAuth::SignupDisabledError
    handle_signup_error
65 66
  end

67 68 69 70 71 72
  def omniauth_error
    @provider = params[:provider]
    @error = params[:error]
    render 'errors/omniauth_error', layout: "errors", status: 422
  end

T
tduehr 已提交
73 74 75 76 77 78 79 80
  def cas3
    ticket = params['ticket']
    if ticket
      handle_service_ticket oauth['provider'], ticket
    end
    handle_omniauth
  end

F
Florian Unglaub 已提交
81 82 83 84
  private

  def handle_omniauth
    if current_user
85 86
      # Add new authentication method
      current_user.identities.find_or_create_by(extern_uid: oauth['uid'], provider: oauth['provider'])
V
Valery Sizov 已提交
87
      log_audit_event(current_user, with: oauth['provider'])
88
      redirect_to profile_account_path, notice: 'Authentication method updated'
F
Florian Unglaub 已提交
89
    else
90 91 92
      oauth_user = Gitlab::OAuth::User.new(oauth)
      oauth_user.save
      @user = oauth_user.gl_user
F
Florian Unglaub 已提交
93

94
      continue_login_process
F
Florian Unglaub 已提交
95
    end
G
Guilherme Garnier 已提交
96
  rescue Gitlab::OAuth::SignupDisabledError
97
    handle_signup_error
F
Florian Unglaub 已提交
98
  end
D
Dmitriy Zaporozhets 已提交
99

100
  def handle_service_ticket(provider, ticket)
T
tduehr 已提交
101 102 103 104 105
    Gitlab::OAuth::Session.create provider, ticket
    session[:service_tickets] ||= {}
    session[:service_tickets][provider] = ticket
  end

106 107 108 109 110 111 112 113 114 115 116 117
  def continue_login_process
    # Only allow properly saved users to login.
    if @user.persisted? && @user.valid?
      log_audit_event(@user, with: oauth['provider'])
      sign_in_and_redirect(@user)
    else
      error_message = @user.errors.full_messages.to_sentence

      redirect_to omniauth_error_path(oauth['provider'], error: error_message) and return
    end
  end

118 119 120 121 122 123 124 125 126 127 128 129 130
  def handle_signup_error
    label = Gitlab::OAuth::Provider.label_for(oauth['provider'])
    message = "Signing in using your #{label} account without a pre-existing GitLab account is not allowed."

    if current_application_settings.signup_enabled?
      message << " Create a GitLab account first, and then connect it to your #{label} account."
    end

    flash[:notice] = message

    redirect_to new_user_session_path
  end

D
Dmitriy Zaporozhets 已提交
131 132 133
  def oauth
    @oauth ||= request.env['omniauth.auth']
  end
V
Valery Sizov 已提交
134 135 136 137 138

  def log_audit_event(user, options = {})
    AuditEventService.new(user, user, options).
      for_authentication.security_event
  end
V
vsizov 已提交
139
end