Class: HaveAPI::Authentication::Chain
- Inherits:
-
Object
- Object
- HaveAPI::Authentication::Chain
- Defined in:
- lib/haveapi/authentication/chain.rb
Overview
Authentication chain. At every request, #authenticate is called to authenticate user.
Instance Method Summary collapse
-
#<<(provider) ⇒ Object
Register authentication ‘provider` for all available API versions.
-
#[](v) ⇒ Object
Return provider list for version ‘v`.
-
#authenticate(v, *args) ⇒ Object
Iterate through authentication providers registered for version ‘v` until authentication is successful or the end is reached and user is not authenticated.
- #describe(context) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(server) ⇒ Chain
constructor
A new instance of Chain.
- #setup(versions) ⇒ Object
Constructor Details
#initialize(server) ⇒ Chain
Returns a new instance of Chain.
5 6 7 8 9 |
# File 'lib/haveapi/authentication/chain.rb', line 5 def initialize(server) @server = server @chain = {} @instances = {} end |
Instance Method Details
#<<(provider) ⇒ Object
Register authentication ‘provider` for all available API versions. `provider` may also be an Array of providers.
81 82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
Return provider list for version ‘v`. Used for registering providers to specific version, e.g.
api.auth_chain[1] << MyAuthProvider
74 75 76 77 |
# File 'lib/haveapi/authentication/chain.rb', line 74 def [](v) @chain[v] ||= [] @chain[v] end |
#authenticate(v, *args) ⇒ Object
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.
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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? ⇒ Boolean
93 94 95 |
# File 'lib/haveapi/authentication/chain.rb', line 93 def empty? @chain.empty? end |
#setup(versions) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |