2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
module AuthHelper
|
2019-05-29 04:11:27 -04:00
|
|
|
PROVIDERS_WITH_ICONS = %w(twitter github gitlab bitbucket google_oauth2 facebook azure_oauth2 authentiq salesforce).freeze
|
2019-05-05 06:19:14 -04:00
|
|
|
LDAP_PROVIDER = /\Aldap/.freeze
|
2015-07-02 10:33:38 -04:00
|
|
|
|
|
|
|
def ldap_enabled?
|
2018-02-23 07:10:39 -05:00
|
|
|
Gitlab::Auth::LDAP::Config.enabled?
|
2015-07-02 10:33:38 -04:00
|
|
|
end
|
|
|
|
|
2016-02-02 17:23:34 -05:00
|
|
|
def omniauth_enabled?
|
2018-07-13 06:39:31 -04:00
|
|
|
Gitlab::Auth.omniauth_enabled?
|
2016-02-02 17:23:34 -05:00
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def provider_has_icon?(name)
|
|
|
|
PROVIDERS_WITH_ICONS.include?(name.to_s)
|
|
|
|
end
|
|
|
|
|
2019-02-01 00:48:13 -05:00
|
|
|
def qa_class_for_provider(provider)
|
|
|
|
{
|
|
|
|
saml: 'qa-saml-login-button',
|
|
|
|
github: 'qa-github-login-button'
|
|
|
|
}[provider.to_sym]
|
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def auth_providers
|
2018-02-23 07:10:39 -05:00
|
|
|
Gitlab::Auth::OAuth::Provider.providers
|
2015-07-02 10:33:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_for_provider(name)
|
2018-02-23 07:10:39 -05:00
|
|
|
Gitlab::Auth::OAuth::Provider.label_for(name)
|
2015-07-02 10:33:38 -04:00
|
|
|
end
|
|
|
|
|
2018-11-12 16:40:42 -05:00
|
|
|
def form_based_provider_priority
|
|
|
|
['crowd', /^ldap/, 'kerberos']
|
|
|
|
end
|
|
|
|
|
|
|
|
def form_based_provider_with_highest_priority
|
|
|
|
@form_based_provider_with_highest_priority ||= begin
|
|
|
|
form_based_provider_priority.each do |provider_regexp|
|
2019-01-16 07:09:29 -05:00
|
|
|
highest_priority = form_based_providers.find { |provider| provider.match?(provider_regexp) }
|
2018-11-12 16:40:42 -05:00
|
|
|
break highest_priority unless highest_priority.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def form_based_auth_provider_has_active_class?(provider)
|
|
|
|
form_based_provider_with_highest_priority == provider
|
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def form_based_provider?(name)
|
2018-04-22 19:15:48 -04:00
|
|
|
[LDAP_PROVIDER, 'crowd'].any? { |pattern| pattern === name.to_s }
|
2015-07-02 10:33:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def form_based_providers
|
|
|
|
auth_providers.select { |provider| form_based_provider?(provider) }
|
|
|
|
end
|
|
|
|
|
2015-08-31 06:59:52 -04:00
|
|
|
def crowd_enabled?
|
|
|
|
auth_providers.include? :crowd
|
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def button_based_providers
|
|
|
|
auth_providers.reject { |provider| form_based_provider?(provider) }
|
|
|
|
end
|
|
|
|
|
2018-12-04 05:31:11 -05:00
|
|
|
def display_providers_on_profile?
|
|
|
|
button_based_providers.any?
|
|
|
|
end
|
|
|
|
|
2018-04-22 19:15:48 -04:00
|
|
|
def providers_for_base_controller
|
|
|
|
auth_providers.reject { |provider| LDAP_PROVIDER === provider }
|
|
|
|
end
|
|
|
|
|
2016-05-04 06:35:03 -04:00
|
|
|
def enabled_button_based_providers
|
2018-02-02 13:39:55 -05:00
|
|
|
disabled_providers = Gitlab::CurrentSettings.disabled_oauth_sign_in_sources || []
|
2016-05-04 06:35:03 -04:00
|
|
|
|
|
|
|
button_based_providers.map(&:to_s) - disabled_providers
|
|
|
|
end
|
|
|
|
|
2016-05-04 06:36:15 -04:00
|
|
|
def button_based_providers_enabled?
|
2016-05-10 11:48:08 -04:00
|
|
|
enabled_button_based_providers.any?
|
2016-05-04 06:36:15 -04:00
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def provider_image_tag(provider, size = 64)
|
|
|
|
label = label_for_provider(provider)
|
|
|
|
|
|
|
|
if provider_has_icon?(provider)
|
|
|
|
file_name = "#{provider.to_s.split('_').first}_#{size}.png"
|
|
|
|
|
2015-09-16 14:04:09 -04:00
|
|
|
image_tag("auth_buttons/#{file_name}", alt: label, title: "Sign in with #{label}")
|
2015-07-02 10:33:38 -04:00
|
|
|
else
|
|
|
|
label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-07-02 10:33:38 -04:00
|
|
|
def auth_active?(provider)
|
|
|
|
current_user.identities.exists?(provider: provider.to_s)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-07-02 10:33:38 -04:00
|
|
|
|
2019-03-18 10:36:34 -04:00
|
|
|
def unlink_provider_allowed?(provider)
|
|
|
|
IdentityProviderPolicy.new(current_user, provider).can?(:unlink)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_provider_allowed?(provider)
|
|
|
|
IdentityProviderPolicy.new(current_user, provider).can?(:link)
|
2017-03-28 06:33:51 -04:00
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
extend self
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
AuthHelper.prepend_if_ee('EE::AuthHelper') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
|
|
|
|
# The methods added in EE should be available as both class and instance
|
|
|
|
# methods, just like the methods provided by `AuthHelper` itself.
|
|
|
|
AuthHelper.extend_if_ee('EE::AuthHelper')
|