forked from ruby/net-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚 Move SASL.authenticator registry from Net::IMAP
The original methods (`Net::IMAP.authenticator` and `Net::IMAP.add_authenticator`) both still work, by delegation to the new methods. But they have been deprecated and will issue warnings.
- Loading branch information
Showing
5 changed files
with
140 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
module Net::IMAP::SASL | ||
|
||
# Registry for SASL authenticators | ||
# | ||
# Registered authenticators must respond to +#new+ or +#call+ (e.g. a class or | ||
# a proc), receiving any credentials and options and returning an | ||
# authenticator instance. The returned object represents a single | ||
# authentication exchange and <em>must not</em> be reused for multiple | ||
# authentication attempts. | ||
# | ||
# An authenticator instance object must respond to +#process+, receiving the | ||
# server's challenge and returning the client's response. Optionally, it may | ||
# also respond to +#initial_response?+ and +#done?+. When | ||
# +#initial_response?+ returns +true+, +#process+ may be called the first | ||
# time with +nil+. When +#done?+ returns +false+, the exchange is incomplete | ||
# and an exception should be raised if the exchange terminates prematurely. | ||
# | ||
# See the source for PlainAuthenticator, XOAuth2Authenticator, and | ||
# ScramSHA1Authenticator for examples. | ||
class Authenticators | ||
|
||
# Create a new Authenticators registry. | ||
# | ||
# This class is usually not instantiated directly. Use SASL.authenticators | ||
# to reuse the default global registry. | ||
# | ||
# By default, the registry will be empty--without any registrations. When | ||
# +add_defaults+ is +true+, authenticators for all standard mechanisms will | ||
# be registered. | ||
# | ||
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 | ||
end | ||
end | ||
|
||
# Returns the names of all registered SASL mechanisms. | ||
def names; @authenticators.keys end | ||
|
||
# :call-seq: | ||
# add_authenticator(mechanism) | ||
# add_authenticator(mechanism, authenticator_class) | ||
# add_authenticator(mechanism, authenticator_proc) | ||
# | ||
# Registers an authenticator for #authenticator to use. +mechanism+ is the | ||
# name of the | ||
# {SASL mechanism}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml] | ||
# implemented by +authenticator_class+ (for instance, <tt>"PLAIN"</tt>). | ||
# | ||
# If +mechanism+ refers to an existing authenticator, a warning will be | ||
# printed and the old authenticator will be replaced. | ||
# | ||
# 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(auth_type, authenticator) | ||
@authenticators[auth_type] = authenticator | ||
end | ||
|
||
# :call-seq: | ||
# authenticator(mechanism, ...) -> auth_session | ||
# | ||
# Builds an authenticator instance using the authenticator registered to | ||
# +mechanism+. The returned object represents a single authentication | ||
# exchange and <em>must not</em> be reused for multiple authentication | ||
# attempts. | ||
# | ||
# All arguments (except +mechanism+) are forwarded to the registered | ||
# authenticator's +#new+ or +#call+ method. Each authenticator must | ||
# document its own arguments. | ||
# | ||
# [Note] | ||
# This method is intended for internal use by connection protocol code | ||
# only. Protocol client users should see refer to their client's | ||
# documentation, e.g. Net::IMAP#authenticate. | ||
def authenticator(mechanism, ...) | ||
auth = @authenticators.fetch(mechanism.upcase) do | ||
raise ArgumentError, 'unknown auth type - "%s"' % mechanism | ||
end | ||
auth.respond_to?(:new) ? auth.new(...) : auth.call(...) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters