-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use net-imap's SASL implementation 🚧[WIP]🚧
This commit converts `#authenticate` to use `net-imap` as a generic fallback for mechanisms that haven't otherwise been added (as subclasses of `Authenticator`). In this commit, the original implementation is still used by `#authenticate` for the `PLAIN`, `LOGIN`, and `CRAM-MD5` mechanisms. Every other mechanism supported by `net-imap` v0.4.0 is added here: * `ANONYMOUS` * `DIGEST-MD5` _(deprecated)_ * `EXTERNAL` * `OAUTHBEARER` * `SCRAM-SHA-1` and `SCRAM-SHA-256` * `XOAUTH` **TODO:** Ideally, `net-smtp` and `net-imap` should both depend on a shared `sasl` or `net-sasl` gem, rather than keep the SASL implementation inside one or the other. See ruby/net-imap#23. **TODO:** since we already know the authenticator arguments up-front, we can validate authenticator arguments by simply creating the authenticator object and rely on the its initializer to raise ArgumentError for missing args.
- Loading branch information
Showing
6 changed files
with
164 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/imap" | ||
|
||
module Net | ||
class SMTP | ||
SASL = Net::IMAP::SASL | ||
|
||
class Authenticator | ||
|
||
# Experimental | ||
# | ||
# Initialize with a block that runs a command, yielding for continuations. | ||
class SASLAdapter < SASL::ClientAdapter | ||
include SASL::ProtocolAdapters::SMTP | ||
|
||
RESPONSE_ERRORS = [ | ||
SMTPAuthenticationError, | ||
SMTPServerBusy, | ||
SMTPSyntaxError, | ||
SMTPFatalError, | ||
].freeze | ||
|
||
def initialize(...) | ||
super | ||
@command_proc ||= client.method(:send_command_with_continuations) | ||
end | ||
|
||
def host; client.address end | ||
def response_errors; RESPONSE_ERRORS end | ||
def sasl_ir_capable?; true end # TODO | ||
def auth_capable?(mechanism); client.auth_capable?(mechanism) end | ||
def drop_connection; client.finish end | ||
def drop_connection!; client.finish end # TODO | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Net | ||
class SMTP | ||
|
||
# Curries arguments to SMTP#auth, using the Authenticator API. | ||
# | ||
# Net::SMTP#authenticate still supports the v0.4.0 Authenticator API, so | ||
# Authenticator subclasses can still be added and used with it. This class | ||
# will be used as the default, when no matching Authenticator subclass | ||
# exists. | ||
class CompatibilityAdapter | ||
def initialize(mechanism) @mechanism = mechanism end | ||
def new(smtp) @smtp = smtp; self end | ||
def auth(*args, **kwargs, &block) | ||
args.pop while args.any? && args.last.nil? | ||
@smtp.auth(@mechanism, *args, **kwargs, &block) | ||
end | ||
end | ||
|
||
Authenticator.auth_classes.default_proc = ->_, mechanism { | ||
CompatibilityAdapter.new(mechanism) | ||
} | ||
|
||
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
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