Sha256: 95de4fd307cc7a34e263b0bc7f620ef04f9b3a4305af7498bb49ac49b1163ed3

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Hadley::Authz

  autoload :Basic, 'hadley/authz/basic'
  autoload :Bearer, 'hadley/authz/bearer'

  def warden
    env['warden']
  end

  module StrategyBuilder

    def build(name, config)
      strategy = self.create_strategy(name)
      self.register_strategy(name, strategy)
      self.set_config(strategy, config)
    end

    protected

    def create_strategy(name)
      class_name = Hadley::Utils.camelize(name.to_s)
      if self.const_defined?(class_name)
        self.const_get(class_name) 
      else
        self.const_set(class_name, Class.new(self))
      end
    end

    def register_strategy(name, strategy)
      full_name = "afid_#{name}".to_sym
      if Warden::Strategies[full_name].nil?
        Warden::Strategies.add(full_name, strategy) 
      end
    end

    def set_config(strategy, config)
      strategy.const_set("CONFIG", config) unless strategy.const_defined?("CONFIG")
    end

  end

  class Strategy < Warden::Strategies::Base
    extend StrategyBuilder

    def config
      self.class.const_get("CONFIG")
    end
  end

  Warden::Config.send(:include, Hadley::Authz::Basic::ConfigExtension)
  Warden::Config.send(:include, Hadley::Authz::Bearer::ConfigExtension)

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hadley-0.0.2 lib/hadley/authz.rb
hadley-0.0.1 lib/hadley/authz.rb