Sha256: 6a9f3044c6dcf3f678bc0a0a20d2b49a364913c4bcf0c58c35824191f03d23ca

Contents?: true

Size: 1011 Bytes

Versions: 6

Compression:

Stored size: 1011 Bytes

Contents

require 'rack/auth/basic'

module Grape
  module Middleware
    module Auth
      class Base
        attr_accessor :options, :app, :env

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

        def context
          env['api.endpoint']
        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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
grape-0.13.0 lib/grape/middleware/auth/base.rb
grape-0.12.0 lib/grape/middleware/auth/base.rb
grape-0.11.0 lib/grape/middleware/auth/base.rb
grape-0.10.1 lib/grape/middleware/auth/base.rb
grape-0.10.0 lib/grape/middleware/auth/base.rb
grape-0.9.0 lib/grape/middleware/auth/base.rb