lib/net/imap/sasl/authenticators.rb in net-imap-0.4.18 vs lib/net/imap/sasl/authenticators.rb in net-imap-0.5.0
- old
+ new
@@ -19,10 +19,14 @@
#
# See the source for PlainAuthenticator, XOAuth2Authenticator, and
# ScramSHA1Authenticator for examples.
class Authenticators
+ # Normalize the mechanism name as an uppercase string, with underscores
+ # converted to dashes.
+ def self.normalize_name(mechanism) -(mechanism.to_s.upcase.tr(?_, ?-)) end
+
# Create a new Authenticators registry.
#
# This class is usually not instantiated directly. Use SASL.authenticators
# to reuse the default global registry.
#
@@ -63,30 +67,30 @@
#
# When only a single argument is given, the authenticator class will be
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
# preserved and non-alphanumeric characters are removed..
def add_authenticator(name, authenticator = nil)
- key = -name.to_s.upcase.tr(?_, ?-)
authenticator ||= begin
class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym
auth_class = nil
->(*creds, **props, &block) {
auth_class ||= Net::IMAP::SASL.const_get(class_name)
auth_class.new(*creds, **props, &block)
}
end
+ key = Authenticators.normalize_name(name)
@authenticators[key] = authenticator
end
# Removes the authenticator registered for +name+
def remove_authenticator(name)
- key = -name.to_s.upcase.tr(?_, ?-)
+ key = Authenticators.normalize_name(name)
@authenticators.delete(key)
end
def mechanism?(name)
- key = -name.to_s.upcase.tr(?_, ?-)
+ key = Authenticators.normalize_name(name)
@authenticators.key?(key)
end
# :call-seq:
# authenticator(mechanism, ...) -> auth_session
@@ -103,10 +107,10 @@
# [Note]
# This method is intended for internal use by connection protocol code
# only. Protocol client users should see refer to their client's
# documentation, e.g. Net::IMAP#authenticate.
def authenticator(mechanism, ...)
- key = -mechanism.to_s.upcase.tr(?_, ?-)
+ key = Authenticators.normalize_name(mechanism)
auth = @authenticators.fetch(key) do
raise ArgumentError, 'unknown auth type - "%s"' % key
end
auth.respond_to?(:new) ? auth.new(...) : auth.call(...)
end