Skip to content

Commit 45f893a

Browse files
committed
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.
1 parent d519d69 commit 45f893a

File tree

6 files changed

+114
-17
lines changed

6 files changed

+114
-17
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
- name: Install dependencies
2020
run: bundle install
2121
- name: Run test
22-
run: rake test
22+
run: bundle exec rake test

lib/net/smtp.rb

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ class SMTPUnsupportedCommand < ProtocolError
175175
#
176176
# The Net::SMTP class supports the \SMTP extension for SASL Authentication
177177
# [RFC4954[https://www.rfc-editor.org/rfc/rfc4954.html]] and the following
178-
# SASL mechanisms: +PLAIN+, +LOGIN+ _(deprecated)_, and +CRAM-MD5+
179-
# _(deprecated)_.
178+
# SASL mechanisms: +ANONYMOUS+, +EXTERNAL+, +OAUTHBEARER+, +PLAIN+,
179+
# +SCRAM-SHA-1+, +SCRAM-SHA-256+, and +XOAUTH2+.
180180
#
181181
# To use \SMTP authentication, pass extra arguments to
182182
# SMTP.start or SMTP#start.
@@ -185,10 +185,38 @@ class SMTPUnsupportedCommand < ProtocolError
185185
# Net::SMTP.start('your.smtp.server', 25,
186186
# user: 'Your Account', secret: 'Your Password', authtype: :plain)
187187
#
188-
# Support for other SASL mechanisms—such as +EXTERNAL+, +OAUTHBEARER+,
189-
# +SCRAM-SHA-256+, and +XOAUTH2+—will be added in a future release.
188+
# # SCRAM-SHA-256
189+
# Net::SMTP.start("your.smtp.server", 25,
190+
# user: "authentication identity", secret: password,
191+
# authtype: :scram_sha_256)
192+
# Net::SMTP.start("your.smtp.server", 25,
193+
# auth: {type: :scram_sha_256,
194+
# username: "authentication identity",
195+
# password: password,
196+
# authzid: "authorization identity"}) # optional
190197
#
191-
# The +LOGIN+ and +CRAM-MD5+ mechanisms are still available for backwards
198+
# # OAUTHBEARER
199+
# Net::SMTP.start("your.smtp.server", 25,
200+
# auth: {type: :oauthbearer,
201+
# oauth2_token: oauth2_access_token,
202+
# authzid: "authorization identity", # optional
203+
# host: "your.smtp.server", # optional
204+
# port: 25}) # optional
205+
#
206+
# # XOAUTH2
207+
# Net::SMTP.start("your.smtp.server", 25,
208+
# user: "username", secret: oauth2_access_token, authtype: :xoauth2)
209+
# Net::SMTP.start("your.smtp.server", 25,
210+
# auth: {type: :xoauth2,
211+
# username: "username",
212+
# oauth2_token: oauth2_token})
213+
#
214+
# # EXTERNAL
215+
# Net::SMTP.start("your.smtp.server", 587,
216+
# starttls: :always, ssl_context_params: ssl_ctx_params,
217+
# authtype: "external")
218+
#
219+
# +DIGEST-MD5+, +LOGIN+, and +CRAM-MD5+ are still available for backwards
192220
# compatibility, but are deprecated and should be avoided.
193221
#
194222
class SMTP < Protocol
@@ -920,8 +948,9 @@ def auth(authtype = DEFAULT_AUTH_TYPE, *args, **kwargs, &block)
920948

921949
private
922950

923-
def check_auth_args(type_arg = nil, *args, type: nil, **kwargs)
951+
def check_auth_args(type_arg = nil, *args, type: nil, user: nil, **kwargs)
924952
type ||= type_arg || DEFAULT_AUTH_TYPE
953+
kwargs[:username] ||= user if user
925954
klass = Authenticator.auth_class(type) or
926955
raise ArgumentError, "wrong authentication type #{type}"
927956
klass.check_args(*args, **kwargs)
@@ -1036,6 +1065,21 @@ def get_response(reqline)
10361065
recv_response()
10371066
end
10381067

1068+
# Returns a successful Response.
1069+
#
1070+
# Yields continuation data and replies to the server using the block result.
1071+
#
1072+
# Raises an exception for any non-successful, non-continuation response.
1073+
def send_command_with_continuations(*args)
1074+
server_resp = get_response args.join(" ")
1075+
while server_resp.continue?
1076+
client_resp = yield server_resp.string.strip.split(nil, 2).last
1077+
server_resp = get_response client_resp
1078+
end
1079+
server_resp.success? or raise server_resp.exception_class.new(server_resp)
1080+
server_resp
1081+
end
1082+
10391083
private
10401084

10411085
def validate_line(line)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
5+
module Net
6+
class SMTP
7+
SASL = Net::IMAP::SASL
8+
9+
# Experimental
10+
#
11+
# Initialize with a block that runs a command, yielding for continuations.
12+
class SASLClientAdapter < SASL::ClientAdapter
13+
include SASL::ProtocolAdapters::SMTP
14+
15+
RESPONSE_ERRORS = [
16+
SMTPAuthenticationError,
17+
SMTPServerBusy,
18+
SMTPSyntaxError,
19+
SMTPFatalError,
20+
].freeze
21+
22+
def initialize(...)
23+
super
24+
@command_proc ||= client.method(:send_command_with_continuations)
25+
end
26+
27+
def authenticate(...)
28+
super
29+
rescue SMTPServerBusy, SMTPSyntaxError, SMTPFatalError => error
30+
raise SMTPAuthenticationError.new(error.response)
31+
rescue SASL::AuthenticationIncomplete => error
32+
raise error.response.exception_class.new(error.response)
33+
end
34+
35+
def host; client.address end
36+
def response_errors; RESPONSE_ERRORS end
37+
def sasl_ir_capable?; true end
38+
def drop_connection; client.finish end
39+
def drop_connection!; client.finish end
40+
end
41+
end
42+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class SMTP
5+
6+
# Curries arguments to SASLAdapter.authenticate.
7+
class AuthSASLCompatibilityAdapter
8+
def initialize(mechanism) @mechanism = mechanism end
9+
def check_args(...) SASL.authenticator(@mechanism, ...) end
10+
def new(smtp) @sasl_adapter = SASLClientAdapter.new(smtp); self end
11+
def auth(...) @sasl_adapter.authenticate(@mechanism, ...) end
12+
end
13+
14+
Authenticator.auth_classes.default_proc = ->hash, mechanism {
15+
hash[mechanism] = AuthSASLCompatibilityAdapter.new(mechanism)
16+
}
17+
18+
end
19+
end

net-smtp.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
2626
spec.require_paths = ["lib"]
2727

2828
spec.add_dependency "net-protocol"
29+
30+
spec.add_dependency "net-imap", ">= 0.4.2" # experimental SASL support
2931
end

test/net/smtp/test_smtp.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,6 @@ def test_start_auth_cram_md5
530530
assert_raise Net::SMTPAuthenticationError do
531531
Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :cram_md5){}
532532
end
533-
534-
port = fake_server_start(auth: 'CRAM-MD5')
535-
smtp = Net::SMTP.new('localhost', port)
536-
auth_cram_md5 = Net::SMTP::AuthCramMD5.new(smtp)
537-
auth_cram_md5.define_singleton_method(:digest_class) { raise '"openssl" or "digest" library is required' }
538-
Net::SMTP::AuthCramMD5.define_singleton_method(:new) { |_| auth_cram_md5 }
539-
e = assert_raise RuntimeError do
540-
smtp.start(user: 'account', password: 'password', authtype: :cram_md5){}
541-
end
542-
assert_equal('"openssl" or "digest" library is required', e.message)
543533
end
544534

545535
def test_start_instance

0 commit comments

Comments
 (0)