2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
module OAuth
|
|
|
|
class Provider
|
|
|
|
LABELS = {
|
|
|
|
"github" => "GitHub",
|
|
|
|
"gitlab" => "GitLab.com",
|
2020-04-08 14:13:33 +05:30
|
|
|
"google_oauth2" => "Google",
|
|
|
|
"azure_oauth2" => "Azure AD"
|
2018-03-27 19:54:05 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def self.authentication(user, provider)
|
|
|
|
return unless user
|
|
|
|
return unless enabled?(provider)
|
|
|
|
|
|
|
|
authenticator =
|
|
|
|
case provider
|
|
|
|
when /^ldap/
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Auth::Ldap::Authentication
|
2018-03-27 19:54:05 +05:30
|
|
|
when 'database'
|
|
|
|
Gitlab::Auth::Database::Authentication
|
|
|
|
end
|
|
|
|
|
|
|
|
authenticator&.new(provider, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.providers
|
|
|
|
Devise.omniauth_providers
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enabled?(name)
|
|
|
|
return true if name == 'database'
|
2018-11-18 11:00:15 +05:30
|
|
|
return true if self.ldap_provider?(name) && providers.include?(name.to_sym)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::Auth.omniauth_enabled? && providers.include?(name.to_sym)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.ldap_provider?(name)
|
|
|
|
name.to_s.start_with?('ldap')
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def self.ultraauth_provider?(name)
|
|
|
|
name.to_s.eql?('ultraauth')
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def self.sync_profile_from_provider?(provider)
|
|
|
|
return true if ldap_provider?(provider)
|
|
|
|
|
|
|
|
providers = Gitlab.config.omniauth.sync_profile_from_provider
|
|
|
|
|
|
|
|
if providers.is_a?(Array)
|
|
|
|
providers.include?(provider)
|
|
|
|
else
|
|
|
|
providers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.config_for(name)
|
|
|
|
name = name.to_s
|
|
|
|
if ldap_provider?(name)
|
2020-04-08 14:13:33 +05:30
|
|
|
if Gitlab::Auth::Ldap::Config.valid_provider?(name)
|
|
|
|
Gitlab::Auth::Ldap::Config.new(name).options
|
2018-03-27 19:54:05 +05:30
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Gitlab.config.omniauth.providers.find { |provider| provider.name == name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.label_for(name)
|
|
|
|
name = name.to_s
|
|
|
|
config = config_for(name)
|
|
|
|
(config && config['label']) || LABELS[name] || name.titleize
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def self.icon_for(name)
|
|
|
|
name = name.to_s
|
|
|
|
config = config_for(name)
|
|
|
|
config && config['icon']
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|