Skip to content

Commit 60f6771

Browse files
committed
♻️ SASL LOGIN: Add "#done?" [🚧 WIP]
The client should raise an error if the command completes successfully but "done?" returns false. Also move to sasl dir and SASL module.
1 parent f992de3 commit 60f6771

File tree

4 files changed

+64
-49
lines changed

4 files changed

+64
-49
lines changed

lib/net/imap/authenticators/login.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/net/imap/sasl.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ module SASL
6767

6868
autoload :CramMD5Authenticator, "#{sasl_dir}/cram_md5_authenticator"
6969
autoload :DigestMD5Authenticator, "#{sasl_dir}/digest_md5_authenticator"
70+
autoload :LoginAuthenticator, "#{sasl_dir}/login_authenticator"
7071

7172
# Authenticators are all lazy loaded
7273
def self.authenticators
7374
@authenticators ||= SASL::Authenticators.new.tap do |registry|
7475
registry.add_authenticator "Plain"
7576
registry.add_authenticator "XOAuth2"
76-
registry.add_authenticator "Login", LoginAuthenticator # deprecated
77+
registry.add_authenticator "Login" # deprecated
7778
registry.add_authenticator "Cram-MD5" # deprecated
7879
registry.add_authenticator "Digest-MD5" # deprecated
7980
end
@@ -109,5 +110,3 @@ def initial_response?(mechanism)
109110
end
110111
end
111112
end
112-
113-
require_relative "authenticators/login"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class IMAP < Protocol
5+
module SASL
6+
7+
# Authenticator for the "+LOGIN+" SASL mechanism. See Net::IMAP#authenticate.
8+
#
9+
# +LOGIN+ authentication sends the password in cleartext.
10+
# RFC3501[https://tools.ietf.org/html/rfc3501] encourages servers to disable
11+
# cleartext authentication until after TLS has been negotiated.
12+
# RFC8314[https://tools.ietf.org/html/rfc8314] recommends TLS version 1.2 or
13+
# greater be used for all traffic, and deprecate cleartext access ASAP. +LOGIN+
14+
# can be secured by TLS encryption.
15+
#
16+
# == Deprecated
17+
#
18+
# The {SASL mechanisms
19+
# registry}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml]
20+
# marks "LOGIN" as obsoleted in favor of "PLAIN". It is included here for
21+
# compatibility with existing servers. See
22+
# {draft-murchison-sasl-login}[https://www.iana.org/go/draft-murchison-sasl-login]
23+
# for both specification and deprecation.
24+
class LoginAuthenticator
25+
26+
STATE_USER = :USER
27+
STATE_PASSWORD = :PASSWORD
28+
STATE_DONE = :DONE
29+
private_constant :STATE_USER, :STATE_PASSWORD, :STATE_DONE
30+
31+
def initialize(user, password, warn_deprecation: true, **_ignored)
32+
if warn_deprecation
33+
warn "WARNING: LOGIN SASL mechanism is deprecated. Use PLAIN instead."
34+
end
35+
@user = user
36+
@password = password
37+
@state = STATE_USER
38+
end
39+
40+
def process(data)
41+
case @state
42+
when STATE_USER
43+
@state = STATE_PASSWORD
44+
@user
45+
when STATE_PASSWORD
46+
@state = STATE_DONE
47+
@password
48+
when STATE_DONE
49+
raise ResponseParseError, data
50+
end
51+
end
52+
53+
def done?; @state == STATE_DONE end
54+
end
55+
end
56+
57+
LoginAuthenticator = SASL::LoginAuthenticator # :nodoc:
58+
deprecate_constant :LoginAuthenticator
59+
60+
end
61+
end

test/net/imap/test_imap_authenticators.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def login(*args, warn_deprecation: false, **kwargs, &block)
102102
end
103103

104104
def test_login_authenticator_matches_mechanism
105-
assert_kind_of(Net::IMAP::LoginAuthenticator, login("n", "p"))
105+
assert_kind_of(Net::IMAP::SASL::LoginAuthenticator, login("n", "p"))
106106
end
107107

108108
def test_login_does_not_support_initial_response

0 commit comments

Comments
 (0)