File: base.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (46 lines) | stat: -rw-r--r-- 990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

require 'rack/auth/basic'

module Grape
  module Middleware
    module Auth
      class Base
        include Helpers

        attr_accessor :options, :app, :env

        def initialize(app, *options)
          @app = app
          @options = options.shift
        end

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

        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
      end
    end
  end
end