2015-07-02 10:33:38 -04:00
|
|
|
module AuthHelper
|
2015-12-23 05:17:25 -05:00
|
|
|
PROVIDERS_WITH_ICONS = %w(twitter github gitlab bitbucket google_oauth2 facebook azure_oauth2).freeze
|
2015-09-24 13:52:20 -04:00
|
|
|
FORM_BASED_PROVIDERS = [/\Aldap/, 'crowd'].freeze
|
2015-07-02 10:33:38 -04:00
|
|
|
|
|
|
|
def ldap_enabled?
|
|
|
|
Gitlab.config.ldap.enabled
|
|
|
|
end
|
|
|
|
|
2016-02-02 17:23:34 -05:00
|
|
|
def omniauth_enabled?
|
|
|
|
Gitlab.config.omniauth.enabled
|
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
def provider_has_icon?(name)
|
|
|
|
PROVIDERS_WITH_ICONS.include?(name.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_providers
|
|
|
|
Gitlab::OAuth::Provider.providers
|
|
|
|
end
|
|
|
|
|
|
|
|
def label_for_provider(name)
|
|
|
|
Gitlab::OAuth::Provider.label_for(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def form_based_provider?(name)
|
|
|
|
FORM_BASED_PROVIDERS.any? { |pattern| pattern === name.to_s }
|
|
|
|
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
|
|
|
|
|
2016-05-04 06:35:03 -04:00
|
|
|
def enabled_button_based_providers
|
|
|
|
disabled_providers = current_application_settings.disabled_oauth_sign_in_sources || []
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def auth_active?(provider)
|
|
|
|
current_user.identities.exists?(provider: provider.to_s)
|
|
|
|
end
|
|
|
|
|
2015-12-23 21:02:52 -05:00
|
|
|
def two_factor_skippable?
|
|
|
|
current_application_settings.require_two_factor_authentication &&
|
2016-06-06 00:38:42 -04:00
|
|
|
!current_user.two_factor_enabled? &&
|
2015-12-23 21:02:52 -05:00
|
|
|
current_application_settings.two_factor_grace_period &&
|
|
|
|
!two_factor_grace_period_expired?
|
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_grace_period_expired?
|
|
|
|
current_user.otp_grace_period_started_at &&
|
|
|
|
(current_user.otp_grace_period_started_at + current_application_settings.two_factor_grace_period.hours) < Time.current
|
|
|
|
end
|
|
|
|
|
2015-07-02 10:33:38 -04:00
|
|
|
extend self
|
|
|
|
end
|