File: credentials_encoder.rb

package info (click to toggle)
ruby-bunny 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 10,256; sh: 70; makefile: 8
file content (55 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (6)
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
module Bunny
  # Contains credentials encoding implementations for various
  # authentication strategies.
  module Authentication
    # Base credentials encoder. Subclasses implement credentials encoding for
    # a particular authentication mechanism (PLAIN, EXTERNAL, etc).
    #
    # @api plugin
    class CredentialsEncoder

      #
      # API
      #

      # Session that uses this encoder
      # @return [Bunny::Session]
      attr_reader :session

      # Instantiates a new encoder for the authentication mechanism
      # used by the provided session.
      #
      # @return [Bunny::CredentialsEncoder]
      def self.for_session(session)
        registry[session.mechanism].new(session)
      end

      # @private
      def self.registry
        @@registry ||= Hash.new { raise NotImplementedError }
      end

      # Registers an encoder for authentication mechanism
      # @api plugin
      def self.auth_mechanism(*mechanisms)
        mechanisms.each do |m|
          registry[m] = self
        end
      end

      # Encodes provided credentials according to the specific authentication
      # mechanism
      # @return [String] Encoded credentials
      def encode_credentials(username, challenge)
        raise NotImplementedError.new("Subclasses must override this method")
      end

      protected

      def initialize(session)
        @session = session
      end

    end
  end
end