File: dsl.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 (48 lines) | stat: -rw-r--r-- 1,438 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
47
48
# frozen_string_literal: true

require 'rack/auth/basic'
require 'active_support/concern'

module Grape
  module Middleware
    module Auth
      module DSL
        extend ActiveSupport::Concern

        module ClassMethods
          # Add an authentication type to the API. Currently
          # only `:http_basic`, `:http_digest` are supported.
          def auth(type = nil, options = {}, &block)
            if type
              namespace_inheritable(:auth, options.reverse_merge(type: type.to_sym, proc: block))
              use Grape::Middleware::Auth::Base, namespace_inheritable(:auth)
            else
              namespace_inheritable(:auth)
            end
          end

          # Add HTTP Basic authorization to the API.
          #
          # @param [Hash] options A hash of options.
          # @option options [String] :realm "API Authorization" The HTTP Basic realm.
          def http_basic(options = {}, &block)
            options[:realm] ||= 'API Authorization'
            auth :http_basic, options, &block
          end

          def http_digest(options = {}, &block)
            options[:realm] ||= 'API Authorization'

            if options[:realm].respond_to?(:values_at)
              options[:realm][:opaque] ||= 'secret'
            else
              options[:opaque] ||= 'secret'
            end

            auth :http_digest, options, &block
          end
        end
      end
    end
  end
end