2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 05:42:35 -04:00
|
|
|
module Gitlab
|
|
|
|
class OmniauthInitializer
|
2020-05-18 08:08:08 -04:00
|
|
|
OAUTH2_TIMEOUT_SECONDS = 10
|
|
|
|
|
2018-03-20 05:42:35 -04:00
|
|
|
def initialize(devise_config)
|
|
|
|
@devise_config = devise_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(providers)
|
2018-03-21 19:25:47 -04:00
|
|
|
providers.each do |provider|
|
2018-07-13 06:39:31 -04:00
|
|
|
name = provider['name'].to_sym
|
|
|
|
|
|
|
|
add_provider_to_devise(name, *arguments_for(provider))
|
|
|
|
setup_provider(name)
|
2018-03-21 19:25:47 -04:00
|
|
|
end
|
2018-03-20 05:42:35 -04:00
|
|
|
end
|
|
|
|
|
2020-05-18 08:08:08 -04:00
|
|
|
class << self
|
|
|
|
def default_arguments_for(provider_name)
|
|
|
|
case provider_name
|
|
|
|
when 'cas3'
|
|
|
|
{ on_single_sign_out: cas3_signout_handler }
|
|
|
|
when 'authentiq'
|
|
|
|
{ remote_sign_out_handler: authentiq_signout_handler }
|
|
|
|
when 'shibboleth'
|
|
|
|
{ fail_with_empty_uid: true }
|
|
|
|
when 'google_oauth2'
|
|
|
|
{ client_options: { connection_opts: { request: { timeout: OAUTH2_TIMEOUT_SECONDS } } } }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cas3_signout_handler
|
|
|
|
lambda do |request|
|
|
|
|
ticket = request.params[:session_index]
|
|
|
|
raise "Service Ticket not found." unless Gitlab::Auth::OAuth::Session.valid?(:cas3, ticket)
|
|
|
|
|
|
|
|
Gitlab::Auth::OAuth::Session.destroy(:cas3, ticket)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authentiq_signout_handler
|
|
|
|
lambda do |request|
|
|
|
|
authentiq_session = request.params['sid']
|
|
|
|
if Gitlab::Auth::OAuth::Session.valid?(:authentiq, authentiq_session)
|
|
|
|
Gitlab::Auth::OAuth::Session.destroy(:authentiq, authentiq_session)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-20 05:42:35 -04:00
|
|
|
private
|
|
|
|
|
2018-07-13 06:39:31 -04:00
|
|
|
def add_provider_to_devise(*args)
|
2018-03-21 19:25:47 -04:00
|
|
|
@devise_config.omniauth(*args)
|
|
|
|
end
|
2018-03-20 05:42:35 -04:00
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
def arguments_for(provider)
|
|
|
|
provider_arguments = []
|
2018-03-20 05:42:35 -04:00
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
%w[app_id app_secret].each do |argument|
|
|
|
|
provider_arguments << provider[argument] if provider[argument]
|
2018-03-20 05:42:35 -04:00
|
|
|
end
|
2018-03-20 13:34:26 -04:00
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
case provider['args']
|
|
|
|
when Array
|
|
|
|
# An Array from the configuration will be expanded.
|
|
|
|
provider_arguments.concat provider['args']
|
|
|
|
when Hash
|
2020-05-18 08:08:08 -04:00
|
|
|
defaults = provider_defaults(provider)
|
|
|
|
hash_arguments = provider['args'].deep_symbolize_keys.deep_merge(defaults)
|
2018-03-20 13:34:26 -04:00
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
# A Hash from the configuration will be passed as is.
|
2019-05-23 14:50:06 -04:00
|
|
|
provider_arguments << normalize_hash_arguments(hash_arguments)
|
2018-03-20 13:34:26 -04:00
|
|
|
end
|
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
provider_arguments
|
|
|
|
end
|
|
|
|
|
2019-05-23 14:50:06 -04:00
|
|
|
def normalize_hash_arguments(args)
|
2020-05-18 08:08:08 -04:00
|
|
|
args.deep_symbolize_keys!
|
2019-05-23 14:50:06 -04:00
|
|
|
|
|
|
|
# Rails 5.1 deprecated the use of string names in the middleware
|
|
|
|
# (https://github.com/rails/rails/commit/83b767ce), so we need to
|
|
|
|
# pass in the actual class to Devise.
|
|
|
|
if args[:strategy_class].is_a?(String)
|
|
|
|
args[:strategy_class] = args[:strategy_class].constantize
|
|
|
|
end
|
|
|
|
|
|
|
|
args
|
|
|
|
end
|
|
|
|
|
2018-03-21 19:25:47 -04:00
|
|
|
def provider_defaults(provider)
|
2020-05-18 08:08:08 -04:00
|
|
|
self.class.default_arguments_for(provider['name'])
|
2018-03-20 13:34:26 -04:00
|
|
|
end
|
2018-07-13 06:39:31 -04:00
|
|
|
|
|
|
|
def omniauth_customized_providers
|
|
|
|
@omniauth_customized_providers ||= build_omniauth_customized_providers
|
|
|
|
end
|
|
|
|
|
|
|
|
# We override this in EE
|
|
|
|
def build_omniauth_customized_providers
|
|
|
|
%i[bitbucket jwt]
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_provider(provider)
|
|
|
|
case provider
|
|
|
|
when :kerberos
|
|
|
|
require 'omniauth-kerberos'
|
|
|
|
when *omniauth_customized_providers
|
|
|
|
require_dependency "omni_auth/strategies/#{provider}"
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 05:42:35 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-28 05:09:34 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::OmniauthInitializer.prepend_mod_with('Gitlab::OmniauthInitializer')
|