Skip to content

Commit

Permalink
♻️ Simplify SASL::{Name}Authenticator registration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nevans committed Sep 10, 2023
1 parent 65e5cd6 commit 74c124d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/net/imap/sasl/authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -60,8 +60,16 @@ def names; @authenticators.keys end
# 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)
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

Expand Down

0 comments on commit 74c124d

Please sign in to comment.