File: auth_mechanism_adapter.rb

package info (click to toggle)
ruby-amqp 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,508 kB
  • sloc: ruby: 8,272; sh: 11; makefile: 10
file content (69 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# encoding: utf-8

module AMQP
  # Provides a flexible method for encoding AMQP credentials. PLAIN and
  # EXTERNAL are provided by this gem. In order to implement a new
  # authentication mechanism, create a subclass like so:
  #
  #   class MyAuthMechanism < AMQP::Async::AuthMechanismAdapter
  #     auth_mechanism "X-MYAUTH"
  #
  #     def encode_credentials(username, password)
  #       # ...
  #     end
  #   end
  class AuthMechanismAdapter

    # Find and instantiate an AuthMechanismAdapter.
    #
    # @param [Adapter] adapter The Adapter for which to encode credentials.
    # @return [AuthMechanismAdapter] An AuthMechanismAdapter that can encode
    #   credentials for the Adapter.
    # @raise [NotImplementedError] The Adapter's mechanism does not
    #   correspond to any known AuthMechanismAdapter.
    def self.for_adapter(adapter)
      registry[adapter.mechanism].new adapter
    end

    protected

      # Used by subclasses to declare the mechanisms that an
      # AuthMechanismAdapter understands.
      #
      # @param [Array<String>] mechanisms One or more mechanisms that can be
      #   handled by the subclass.
      def self.auth_mechanism(*mechanisms)
        mechanisms.each {|mechanism| registry[mechanism] = self}
      end

    private

      # Accesses the registry of AuthMechanismAdapter subclasses. Keys in
      # this hash are the names of the authentication mechanisms; values are
      # the classes that handle them. Referencing an unknown mechanism from
      # this Hash will raise NotImplementedError.
      def self.registry
        @@registry ||= Hash.new {raise NotImplementedError}
      end

    public

      # The Adapter that this AuthMechanismAdapter operates on behalf of.
      attr_reader :adapter

    private

      # Create a new AuthMechanismAdapter. Users of this class should instead
      # retrieve an instance through for_adapter.
      def initialize(adapter)
        @adapter = adapter
      end
  end
end

# pre-require builtin auth mechanisms
Dir[File.join(File.dirname(__FILE__),
              File.basename(__FILE__, '.rb'),
              '*')].each do |f|
  require f
end