Sha256: 4e31c29caf5ad57abf5239a0977461e8844585a1d080ac44f43ccc2c9d2edeb5

Contents?: true

Size: 1.84 KB

Versions: 46

Compression:

Stored size: 1.84 KB

Contents

module Kadmin
  module Auth
    class Configuration
      OAuthCredentials = Struct.new(:id, :secret)

      # @return [Kadmin::Auth::Configuration::OAuthCredentials] credentials for OAuth2 authentication; if absent, fallback to :developer provider
      attr_accessor :oauth_credentials

      # @return [Class<Kadmin::Auth::User>] class to use for authenticated users (mostly for resource authorization)
      attr_accessor :user_class

      # @return [Class<Kadmin::Auth::UserStore>] class for user lookup/registration
      attr_accessor :user_store_class

      def initialize
        @oauth_credentials = nil
        @user_class = Kadmin::Auth::User
        @user_store_class = Kadmin::Auth::UserStore
        @enabled = false
        @__omniauth_appended = false
      end

      # @return [Boolean] true if enabled, false otherwise
      def enabled?
        return @enabled
      end

      # Enables authentication and adds OmniAuth middlewares
      def enable!
        unless @enabled
          append_omniauth_middleware
          @enabled = true
        end
      end

      # Disables authentication and removes OmniAuth middlewares
      def disable!
        @enabled = false
      end

      def append_omniauth_middleware
        return if @__omniauth_appended

        @__omniauth_appended = true
        OmniAuth.config.logger = Kadmin.logger
        OmniAuth.config.path_prefix = File.join(Kadmin.config.mount_path, OmniAuth.config.path_prefix)

        provider_args = case Kadmin::Auth.omniauth_provider
        when :google_oauth2
          [:google_oauth2, @oauth_credentials.id, @oauth_credentials.secret]
        else
          [:developer, fields: [:email]]
        end

        Rails.application.config.middleware.use OmniAuth::Builder do
          provider(*provider_args)
        end
      end
      private :append_omniauth_middleware
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
kadmin-1.3.0 lib/kadmin/auth/configuration.rb
kadmin-1.2.1 lib/kadmin/auth/configuration.rb
kadmin-1.2.0 lib/kadmin/auth/configuration.rb
kadmin-1.1.1 lib/kadmin/auth/configuration.rb
kadmin-1.1.0 lib/kadmin/auth/configuration.rb
kadmin-1.0.9 lib/kadmin/auth/configuration.rb
kadmin-1.0.8 lib/kadmin/auth/configuration.rb
kadmin-1.0.7 lib/kadmin/auth/configuration.rb
kadmin-1.0.6 lib/kadmin/auth/configuration.rb
kadmin-1.0.5 lib/kadmin/auth/configuration.rb
kadmin-1.0.4 lib/kadmin/auth/configuration.rb
kadmin-1.0.3 lib/kadmin/auth/configuration.rb
kadmin-1.0.2 lib/kadmin/auth/configuration.rb
kadmin-1.0.1 lib/kadmin/auth/configuration.rb
kadmin-1.0.0 lib/kadmin/auth/configuration.rb
kadmin-0.9.5 lib/kadmin/auth/configuration.rb
kadmin-0.9.4 lib/kadmin/auth/configuration.rb
kadmin-0.8.0 lib/kadmin/auth/configuration.rb
kadmin-0.7.5 lib/kadmin/auth/configuration.rb
kadmin-0.7.4 lib/kadmin/auth/configuration.rb