Skip to content

Commit 7e3148c

Browse files
committed
🔧 Add config option for sasl_ir
This config option becomes the default value for the `#authenticate` kwarg with the same name.
1 parent 962671d commit 7e3148c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

lib/net/imap.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ def starttls(**options)
12121212
end
12131213

12141214
# :call-seq:
1215-
# authenticate(mechanism, *, sasl_ir: true, registry: Net::IMAP::SASL.authenticators, **, &) -> ok_resp
1215+
# authenticate(mechanism, *, sasl_ir: config.sasl_ir, registry: Net::IMAP::SASL.authenticators, **, &) -> ok_resp
12161216
#
12171217
# Sends an {AUTHENTICATE command [IMAP4rev1 §6.2.2]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.2]
12181218
# to authenticate the client. If successful, the connection enters the
@@ -1317,7 +1317,9 @@ def starttls(**options)
13171317
# Previously cached #capabilities will be cleared when this method
13181318
# completes. If the TaggedResponse to #authenticate includes updated
13191319
# capabilities, they will be cached.
1320-
def authenticate(mechanism, *creds, sasl_ir: true, **props, &callback)
1320+
def authenticate(mechanism, *creds,
1321+
sasl_ir: config.sasl_ir,
1322+
**props, &callback)
13211323
mechanism = mechanism.to_s.tr("_", "-").upcase
13221324
authenticator = SASL.authenticator(mechanism, *creds, **props, &callback)
13231325
cmdargs = ["AUTHENTICATE", mechanism]

lib/net/imap/config.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def self.[](config) # :nodoc: unfinished API
104104
# The default value is +5+ seconds.
105105
attr_accessor :idle_response_timeout, type: Integer
106106

107+
# :markup: markdown
108+
#
109+
# Whether to use the +SASL-IR+ extension with IMAP#authenticate.
110+
#
111+
# | Starting with version | The default value is |
112+
# |-----------------------|------------------------------------------|
113+
# | _original_ | +false+ <em>(extension unsupported)</em> |
114+
# | v0.4 | +true+ <em>(support added)</em> |
115+
attr_accessor :sasl_ir, type: :boolean
116+
107117
# Creates a new config object and initialize its attribute with +attrs+.
108118
#
109119
# If +parent+ is not given, the global config is used by default.
@@ -119,6 +129,7 @@ def initialize(parent = Config.global, **attrs)
119129
debug: false,
120130
open_timeout: 30,
121131
idle_response_timeout: 5,
132+
sasl_ir: true,
122133
).freeze
123134

124135
@global = default.new

test/net/imap/test_imap.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,23 @@ def test_id
930930
end
931931
end
932932

933+
test("#authenticate never sends an initial response " \
934+
"when config.sasl_ir: false") do
935+
[true, false].each do |server_support|
936+
with_fake_server(
937+
preauth: false, cleartext_auth: true, sasl_ir: server_support
938+
) do |server, imap|
939+
imap.config.sasl_ir = false
940+
imap.authenticate("PLAIN", "test_user", "test-password")
941+
cmd, cont = 2.times.map { server.commands.pop }
942+
assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args]
943+
assert_equal(["\x00test_user\x00test-password"].pack("m0"),
944+
cont[:continuation].strip)
945+
assert_empty server.commands
946+
end
947+
end
948+
end
949+
933950
test("#authenticate never sends an initial response " \
934951
"when the mechanism does not support client-first") do
935952
with_fake_server(

0 commit comments

Comments
 (0)