From e15cd179bc730e1d330804e7f0ca5b9be154f0a2 Mon Sep 17 00:00:00 2001 From: "nicholas a. evans" Date: Wed, 5 Feb 2020 12:32:52 -0500 Subject: [PATCH] net/imap: Extract public Net::IMAP.authenticator Other IMAP libraries (e.g. using EventMachine or async-io) may want to reuse some of Net::IMAP's code. This allows libraries to access Net::IMAP's authenticator registry. --- lib/net/imap.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index ae23c0acf..15179a9a4 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -304,6 +304,16 @@ def self.add_authenticator(auth_type, authenticator) @@authenticators[auth_type] = authenticator end + # Builds an authenticator for Net::IMAP#authenticate. + def self.authenticator(auth_type, *args) + auth_type = auth_type.upcase + unless @@authenticators.has_key?(auth_type) + raise ArgumentError, + format('unknown auth type - "%s"', auth_type) + end + @@authenticators[auth_type].new(*args) + end + # The default port for IMAP connections, port 143 def self.default_port return PORT @@ -408,7 +418,7 @@ def starttls(options = {}, verify = true) # the form "AUTH=LOGIN" or "AUTH=CRAM-MD5". # # Authentication is done using the appropriate authenticator object: - # see @@authenticators for more information on plugging in your own + # see +add_authenticator+ for more information on plugging in your own # authenticator. # # For example: @@ -417,12 +427,7 @@ def starttls(options = {}, verify = true) # # A Net::IMAP::NoResponseError is raised if authentication fails. def authenticate(auth_type, *args) - auth_type = auth_type.upcase - unless @@authenticators.has_key?(auth_type) - raise ArgumentError, - format('unknown auth type - "%s"', auth_type) - end - authenticator = @@authenticators[auth_type].new(*args) + authenticator = self.class.authenticator(auth_type, *args) send_command("AUTHENTICATE", auth_type) do |resp| if resp.instance_of?(ContinuationRequest) data = authenticator.process(resp.data.text.unpack("m")[0])