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

192 lines
4.8 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2016-04-02 18:10:28 +05:30
include AuthenticatesWithTwoFactor
2017-09-10 17:25:29 +05:30
include Devise::Controllers::Rememberable
2015-09-11 14:41:01 +05:30
protect_from_forgery except: [:kerberos, :saml, :cas3]
2015-09-11 14:41:01 +05:30
2018-10-15 14:42:47 +05:30
def handle_omniauth
omniauth_flow(Gitlab::Auth::OAuth)
2014-09-02 18:07:02 +05:30
end
2018-10-15 14:42:47 +05:30
AuthHelper.providers_for_base_controller.each do |provider|
alias_method provider, :handle_omniauth
2018-03-17 18:26:18 +05:30
end
2018-05-09 12:01:36 +05:30
# Extend the standard implementation to also increment
# the number of failed sign in attempts
def failure
if params[:username].present? && AuthHelper.form_based_provider?(failed_strategy.name)
user = User.by_login(params[:username])
user&.increment_failed_attempts!
end
super
end
2014-09-02 18:07:02 +05:30
# Extend the standard message generation to accept our custom exception
def failure_message
exception = env["omniauth.error"]
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
2018-10-15 14:42:47 +05:30
error.to_s.humanize if error
2014-09-02 18:07:02 +05:30
end
2016-04-02 18:10:28 +05:30
def saml
2018-10-15 14:42:47 +05:30
omniauth_flow(Gitlab::Auth::Saml)
2016-04-02 18:10:28 +05:30
end
2014-09-02 18:07:02 +05:30
def omniauth_error
@provider = params[:provider]
@error = params[:error]
2017-08-17 22:00:37 +05:30
render 'errors/omniauth_error', layout: "oauth_error", status: 422
2014-09-02 18:07:02 +05:30
end
def cas3
ticket = params['ticket']
if ticket
handle_service_ticket oauth['provider'], ticket
end
2018-03-17 18:26:18 +05:30
handle_omniauth
end
2017-08-17 22:00:37 +05:30
def authentiq
if params['sid']
handle_service_ticket oauth['provider'], params['sid']
end
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
handle_omniauth
end
2018-03-26 14:24:53 +05:30
def auth0
if oauth['uid'].blank?
fail_auth0_login
else
handle_omniauth
end
end
2014-09-02 18:07:02 +05:30
private
2018-10-15 14:42:47 +05:30
def omniauth_flow(auth_module, identity_linker: nil)
2014-09-02 18:07:02 +05:30
if current_user
2015-09-11 14:41:01 +05:30
log_audit_event(current_user, with: oauth['provider'])
2014-09-02 18:07:02 +05:30
2018-10-15 14:42:47 +05:30
identity_linker ||= auth_module::IdentityLinker.new(current_user, oauth)
identity_linker.link
if identity_linker.changed?
redirect_identity_linked
elsif identity_linker.failed?
redirect_identity_link_failed(identity_linker.error_message)
else
redirect_identity_exists
end
else
sign_in_user_flow(auth_module::User)
2014-09-02 18:07:02 +05:30
end
2018-10-15 14:42:47 +05:30
end
def redirect_identity_exists
redirect_to after_sign_in_path_for(current_user)
end
def redirect_identity_link_failed(error_message)
redirect_to profile_account_path, notice: "Authentication failed: #{error_message}"
end
def redirect_identity_linked
redirect_to profile_account_path, notice: 'Authentication method updated'
2014-09-02 18:07:02 +05:30
end
def handle_service_ticket(provider, ticket)
2018-03-27 19:54:05 +05:30
Gitlab::Auth::OAuth::Session.create provider, ticket
session[:service_tickets] ||= {}
session[:service_tickets][provider] = ticket
end
2018-10-15 14:42:47 +05:30
def sign_in_user_flow(auth_user_class)
auth_user = auth_user_class.new(oauth)
user = auth_user.find_and_update!
if auth_user.valid_sign_in?
log_audit_event(user, with: oauth['provider'])
set_remember_me(user)
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
if user.two_factor_enabled?
prompt_for_two_factor(user)
2016-08-24 12:49:21 +05:30
else
2018-10-15 14:42:47 +05:30
sign_in_and_redirect(user)
2016-08-24 12:49:21 +05:30
end
2016-04-02 18:10:28 +05:30
else
2018-10-15 14:42:47 +05:30
fail_login(user)
2016-04-02 18:10:28 +05:30
end
2018-10-15 14:42:47 +05:30
rescue Gitlab::Auth::OAuth::User::SigninDisabledForProviderError
handle_disabled_provider
rescue Gitlab::Auth::OAuth::User::SignupDisabledError
handle_signup_error
2016-04-02 18:10:28 +05:30
end
2016-06-02 11:05:42 +05:30
def handle_signup_error
2018-03-27 19:54:05 +05:30
label = Gitlab::Auth::OAuth::Provider.label_for(oauth['provider'])
2016-06-02 11:05:42 +05:30
message = "Signing in using your #{label} account without a pre-existing GitLab account is not allowed."
2018-03-17 18:26:18 +05:30
if Gitlab::CurrentSettings.allow_signup?
2016-06-02 11:05:42 +05:30
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
2014-09-02 18:07:02 +05:30
def oauth
@oauth ||= request.env['omniauth.auth']
end
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
def fail_login(user)
error_message = user.errors.full_messages.to_sentence
2017-09-10 17:25:29 +05:30
return redirect_to omniauth_error_path(oauth['provider'], error: error_message)
end
2018-03-17 18:26:18 +05:30
2018-03-26 14:24:53 +05:30
def fail_auth0_login
flash[:alert] = 'Wrong extern UID provided. Make sure Auth0 is configured correctly.'
redirect_to new_user_session_path
end
2018-03-17 18:26:18 +05:30
def handle_disabled_provider
2018-03-27 19:54:05 +05:30
label = Gitlab::Auth::OAuth::Provider.label_for(oauth['provider'])
2018-03-17 18:26:18 +05:30
flash[:alert] = "Signing in using #{label} has been disabled"
redirect_to new_user_session_path
end
2015-09-11 14:41:01 +05:30
def log_audit_event(user, options = {})
2017-09-10 17:25:29 +05:30
AuditEventService.new(user, user, options)
.for_authentication.security_event
end
2018-10-15 14:42:47 +05:30
def set_remember_me(user)
return unless remember_me?
if user.two_factor_enabled?
params[:remember_me] = '1'
else
remember_me(user)
end
end
2017-09-10 17:25:29 +05:30
def remember_me?
request_params = request.env['omniauth.params']
(request_params['remember_me'] == '1') if request_params.present?
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
end