-
Notifications
You must be signed in to change notification settings - Fork 30
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
5 changed files
with
146 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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 | ||
|
||
# Translates +user+ to +username+ and +type+ kwarg to the first arg. | ||
def authenticate(typearg = nil, *args, | ||
type: nil, user: nil, | ||
**kwargs, &block) | ||
kwargs[:username] ||= user if user | ||
type ||= typearg || DEFAULT_AUTH_TYPE | ||
args, kwargs = backward_compatible_auth_args(type, *args, **kwargs) | ||
super(type, *args, **kwargs) | ||
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 | ||
|
||
private | ||
|
||
# Temporarily adapt for differences between SMTP and Net::IMAP::SASL: | ||
# | ||
# * Net::IMAP::SASL adapters don't handle the +secret+ keyword. | ||
# * Net::IMAP::SASL::LoginAdapter and CramMd5Adapter don't accept kwargs! | ||
# | ||
# These are reasonable changes and Net::IMAP::SASL can be updated. | ||
def backward_compatible_auth_args(type, *args, secret: nil, **kwargs) | ||
if secret | ||
secret_type = type.match?(/\AX?OAUTH/i) ? :oauth2_token : :password | ||
kwargs[secret_type] ||= secret | ||
end | ||
if type.match?(/\A(?:LOGIN|CRAM[-_]MD5)\z/i) | ||
usernames = [kwargs.delete(:authcid), kwargs.delete(:username)] | ||
secrets = [kwargs.delete(:password)] | ||
args[0] ||= usernames.compact.first | ||
args[1] ||= secrets.compact.first | ||
end | ||
[args, kwargs] | ||
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(...) 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