Class: Gitlab::Auth::OAuth::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/auth/o_auth/provider.rb

Constant Summary collapse

LABELS =
{
  "alicloud" => "AliCloud",
  "dingtalk" => "DingTalk",
  "github" => "GitHub",
  "gitlab" => "GitLab.com",
  "google_oauth2" => "Google",
  "azure_oauth2" => "Azure AD",
  "azure_activedirectory_v2" => "Azure AD v2",
  'atlassian_oauth2' => 'Atlassian'
}.freeze

Class Method Summary collapse

Class Method Details

.authentication(user, provider) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/auth/o_auth/provider.rb', line 18

def self.authentication(user, provider)
  return unless user
  return unless enabled?(provider)

  authenticator =
    case provider
    when /crowd/
      Gitlab::Auth::Crowd::Authentication
    when /^ldap/
      Gitlab::Auth::Ldap::Authentication
    when 'database'
      Gitlab::Auth::Database::Authentication
    end

  authenticator&.new(provider, user)
end

.config_for(name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gitlab/auth/o_auth/provider.rb', line 62

def self.config_for(name)
  name = name.to_s
  if ldap_provider?(name)
    if Gitlab::Auth::Ldap::Config.valid_provider?(name)
      Gitlab::Auth::Ldap::Config.new(name).options
    else
      nil
    end
  else
    provider = Gitlab.config.omniauth.providers.find do |provider|
      provider.name == name || (provider.name == 'openid_connect' && provider.dig(:args, :name) == name)
    end
    merge_provider_args_with_defaults!(provider)

    provider
  end
end

.enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/gitlab/auth/o_auth/provider.rb', line 39

def self.enabled?(name)
  return true if name == 'database'
  return true if self.ldap_provider?(name) && providers.include?(name.to_sym)

  Gitlab::Auth.omniauth_enabled? && providers.include?(name.to_sym)
end

.icon_for(name) ⇒ Object



86
87
88
89
90
# File 'lib/gitlab/auth/o_auth/provider.rb', line 86

def self.icon_for(name)
  name = name.to_s
  config = config_for(name)
  config && config['icon']
end

.label_for(name) ⇒ Object



80
81
82
83
84
# File 'lib/gitlab/auth/o_auth/provider.rb', line 80

def self.label_for(name)
  name = name.to_s
  config = config_for(name)
  (config && config['label']) || LABELS[name] || name.titleize
end

.ldap_provider?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/gitlab/auth/o_auth/provider.rb', line 46

def self.ldap_provider?(name)
  name.to_s.start_with?('ldap')
end

.merge_provider_args_with_defaults!(provider) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/gitlab/auth/o_auth/provider.rb', line 92

def self.merge_provider_args_with_defaults!(provider)
  return unless provider

  provider['args'] ||= {}

  defaults = Gitlab::OmniauthInitializer.default_arguments_for(provider['name'])
  provider['args'].deep_merge!(defaults.deep_stringify_keys)
end

.providersObject



35
36
37
# File 'lib/gitlab/auth/o_auth/provider.rb', line 35

def self.providers
  ::Devise.omniauth_providers
end

.sync_profile_from_provider?(provider) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/auth/o_auth/provider.rb', line 50

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