class HaveAPI::Authentication::Chain

Authentication chain. At every request, authenticate is called to authenticate user.

Public Class Methods

new(server) click to toggle source
# File lib/haveapi/authentication/chain.rb, line 5
def initialize(server)
  @server = server
  @chain = {}
  @instances = {}
end

Public Instance Methods

<<(provider) click to toggle source

Register authentication ‘provider` for all available API versions. `provider` may also be an Array of providers.

# File lib/haveapi/authentication/chain.rb, line 81
def <<(provider)
  @chain[:all] ||= []

  if provider.is_a?(Array)
    provider.each { |p| @chain[:all] << p }
  else
    @chain[:all] << provider
  end

  self
end
[](v) click to toggle source

Return provider list for version ‘v`. Used for registering providers to specific version, e.g.

api.auth_chain[1] << MyAuthProvider
# File lib/haveapi/authentication/chain.rb, line 74
def [](v)
  @chain[v] ||= []
  @chain[v]
end
authenticate(v, *args) click to toggle source

Iterate through authentication providers registered for version ‘v` until authentication is successful or the end is reached and user is not authenticated. Authentication provider can deny the user access by calling Base#deny.

# File lib/haveapi/authentication/chain.rb, line 38
def authenticate(v, *args)
  catch(:return) do
    return unless @instances[v]

    @instances[v].each do |provider|
      u = provider.authenticate(*args)
      return u if u
    end
  end

  nil
end
describe(context) click to toggle source
# File lib/haveapi/authentication/chain.rb, line 51
def describe(context)
  ret = {}

  return ret unless @instances[context.version]

  @instances[context.version].each do |provider|
    ret[provider.name] = provider.describe

    next unless provider.resource_module

    ret[provider.name][:resources] = {}

    @server.routes[context.version][:authentication][provider.name][:resources].each do |r, children|
      ret[provider.name][:resources][r.resource_name.underscore.to_sym] = r.describe(children, context)
    end
  end

  ret
end
empty?() click to toggle source
# File lib/haveapi/authentication/chain.rb, line 93
def empty?
  @chain.empty?
end
setup(versions) click to toggle source
# File lib/haveapi/authentication/chain.rb, line 11
def setup(versions)
  versions.each do |v|
    @instances[v] ||= []

    @chain[v] && @chain[v].each { |p| register_provider(v, p) }
  end

  return unless @chain[:all]

  @chain[:all].each do |p|
    @instances.each_key { |v| register_provider(v, p) }
  end

  # @chain.each do |p|
  #   @instances << p.new(@server)
  #
  #   parts = p.to_s.split('::')
  #   mod = Kernel.const_get((parts[0..-2] << 'Resources').join('::'))
  #
  #   @server.add_module(mod, prefix: parts[-2].tableize) if mod
  # end
end

Protected Instance Methods

register_provider(v, p) click to toggle source
# File lib/haveapi/authentication/chain.rb, line 99
def register_provider(v, p)
  instance = p.new(@server, v)
  @instances[v] << instance

  @server.add_auth_routes(v, instance, prefix: instance.name.to_s)

  resource_module = instance.resource_module
  return if resource_module.nil?

  @server.add_auth_module(
    v,
    instance.name,
    resource_module,
    prefix: instance.name.to_s
  )
end