Skip to content

Commit 7d99663

Browse files
committed
♻️ Simplify SASL::{Name}Authenticator registration
1 parent b73bbf4 commit 7d99663

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/net/imap/sasl/authenticators.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ def authenticators; @authenticators.dup end
3030
#
3131
# See PlainAuthenticator, XOauth2Authenticator, and DigestMD5Authenticator for
3232
# examples.
33-
def add_authenticator(mechanism, authenticator, warn_overwrite: true)
33+
def add_authenticator(mechanism, authenticator = nil, warn_overwrite: true)
3434
mechanism = mechanism.to_str
3535
upcased = mechanism.upcase
36+
authenticator ||= ->(*creds, **props, &block) {
37+
class_name = "#{mechanism.gsub(/-/, "")}Authenticator"
38+
auth_class = Net::IMAP::SASL.const_get(class_name.to_sym)
39+
add_authenticator(upcased, auth_class, warn_overwrite: false)
40+
auth_class.new(*creds, **props, &block)
41+
}
3642
if warn_overwrite && (original = authenticators[upcased])
3743
warn("%p: replacing %p authenticator: %p" % [ self, upcased, original],
3844
uplevel: 1)

0 commit comments

Comments
 (0)