From 74c124d9847869b26197692d274ac27578e05d7f Mon Sep 17 00:00:00 2001 From: nick evans Date: Tue, 13 Jun 2023 18:25:52 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplify=20SASL::{Name}Aut?= =?UTF-8?q?henticator=20registration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In addition to selecting the constant based on the name, this also lazy loads the authenticator definitions. Most authenticators are never used, so this avoids the cost of loading them all. --- lib/net/imap/sasl/authenticators.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/net/imap/sasl/authenticators.rb b/lib/net/imap/sasl/authenticators.rb index a8e183cbd..ef193df48 100644 --- a/lib/net/imap/sasl/authenticators.rb +++ b/lib/net/imap/sasl/authenticators.rb @@ -33,11 +33,11 @@ class Authenticators def initialize(use_defaults: false) @authenticators = {} if use_defaults - add_authenticator "Plain", PlainAuthenticator - add_authenticator "XOAuth2", XOAuth2Authenticator - add_authenticator "Login", LoginAuthenticator # deprecated - add_authenticator "Cram-MD5", CramMD5Authenticator # deprecated - add_authenticator "Digest-MD5", DigestMD5Authenticator # deprecated + add_authenticator "Plain" + add_authenticator "XOAuth2" + add_authenticator "Login" # deprecated + add_authenticator "Cram-MD5" # deprecated + add_authenticator "Digest-MD5" # deprecated end end @@ -60,8 +60,16 @@ def names; @authenticators.keys end # When only a single argument is given, the authenticator class will be # lazily loaded from Net::IMAP::SASL::#{name}Authenticator (case is # preserved and non-alphanumeric characters are removed.. - def add_authenticator(name, authenticator) + def add_authenticator(name, authenticator = nil) key = name.upcase.to_sym + 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 @authenticators[key] = authenticator end