Class: Grape::Middleware::Auth::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/middleware/auth/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/grape/middleware/auth/base.rb', line 9

def initialize(app, options = {})
  @app = app
  @options = options || {}
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



7
8
9
# File 'lib/grape/middleware/auth/base.rb', line 7

def app
  @app
end

#envObject

Returns the value of attribute env.



7
8
9
# File 'lib/grape/middleware/auth/base.rb', line 7

def env
  @env
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/grape/middleware/auth/base.rb', line 7

def options
  @options
end

Instance Method Details

#_call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/grape/middleware/auth/base.rb', line 22

def _call(env)
  self.env = env

  if options.key?(:type)
    auth_proc         = options[:proc]
    auth_proc_context = context

    strategy_info   = Grape::Middleware::Auth::Strategies[options[:type]]

    throw(:error, status: 401, message: 'API Authorization Failed.') unless strategy_info.present?

    strategy = strategy_info.create(@app, options) do |*args|
      auth_proc_context.instance_exec(*args, &auth_proc)
    end

    strategy.call(env)

  else
    app.call(env)
  end
end

#call(env) ⇒ Object



18
19
20
# File 'lib/grape/middleware/auth/base.rb', line 18

def call(env)
  dup._call(env)
end

#contextObject



14
15
16
# File 'lib/grape/middleware/auth/base.rb', line 14

def context
  env['api.endpoint']
end