Skip to content

♻️ Simplify lazy-loaded SASL::{Name}Authenticator registration #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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