forked from ruby/net-smtp
-
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.
Use net-imap's SASL implementation 🚧[WIP]🚧
This commit adds the `net-imap` as a default fallback for mechanisms that haven't otherwise been added. 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.
- Loading branch information
Showing
7 changed files
with
115 additions
and
17 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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/imap" | ||
|
||
module Net | ||
class SMTP | ||
SASL = Net::IMAP::SASL | ||
|
||
# Experimental | ||
# | ||
# Initialize with a block that runs a command, yielding for continuations. | ||
class SASLClientAdapter < 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 authenticate(...) | ||
super | ||
rescue SMTPServerBusy, SMTPSyntaxError, SMTPFatalError => error | ||
raise SMTPAuthenticationError.new(error.response) | ||
rescue SASL::AuthenticationIncomplete => error | ||
raise error.response.exception_class.new(error.response) | ||
end | ||
|
||
def host; client.address end | ||
def response_errors; RESPONSE_ERRORS end | ||
def sasl_ir_capable?; true end | ||
def drop_connection; client.finish end | ||
def drop_connection!; client.finish 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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Net | ||
class SMTP | ||
|
||
# Curries arguments to SASLAdapter.authenticate. | ||
class AuthSASLCompatibilityAdapter | ||
def initialize(mechanism) @mechanism = mechanism end | ||
def check_args(...) SASL.authenticator(@mechanism, ...) end | ||
def new(smtp) @sasl_adapter = SASLClientAdapter.new(smtp); self end | ||
def auth(...) @sasl_adapter.authenticate(@mechanism, ...) end | ||
end | ||
|
||
Authenticator.auth_classes.default_proc = ->hash, mechanism { | ||
hash[mechanism] = AuthSASLCompatibilityAdapter.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