provider.rb 2.0 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12
module Gitlab
  module Auth
    module OAuth
      class Provider
        LABELS = {
          "github"         => "GitHub",
          "gitlab"         => "GitLab.com",
          "google_oauth2"  => "Google"
        }.freeze

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
        def self.authentication(user, provider)
          return unless user
          return unless enabled?(provider)

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

          authenticator&.new(provider, user)
        end

28 29 30 31 32
        def self.providers
          Devise.omniauth_providers
        end

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

36
          Gitlab::Auth.omniauth_enabled? && providers.include?(name.to_sym)
37 38 39 40 41 42
        end

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

43 44 45 46
        def self.ultraauth_provider?(name)
          name.to_s.eql?('ultraauth')
        end

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        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)
            if Gitlab::Auth::LDAP::Config.valid_provider?(name)
              Gitlab::Auth::LDAP::Config.new(name).options
            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
      end
    end
  end
end