Skip to content

Commit cfb8caf

Browse files
authored
🔀 Merge pull request #177 from ruby/sasl/kwargs-on-all-authenticators
♻️ Allow keyword args on all SASL authenticators
2 parents e64dbc8 + 87c1fe9 commit cfb8caf

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

lib/net/imap/sasl/digest_md5_authenticator.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Net::IMAP::SASL::DigestMD5Authenticator
4141
attr_reader :authzid
4242

4343
# :call-seq:
44-
# new(username, password, authzid = nil) -> authenticator
44+
# new(username, password, authzid = nil, **options) -> authenticator
45+
# new(username:, password:, authzid: nil, **options) -> authenticator
4546
#
4647
# Creates an Authenticator for the "+DIGEST-MD5+" SASL mechanism.
4748
#
@@ -55,7 +56,12 @@ class Net::IMAP::SASL::DigestMD5Authenticator
5556
# * +warn_deprecation+ — Set to +false+ to silence the warning.
5657
#
5758
# See the documentation for each attribute for more details.
58-
def initialize(username, password, authzid = nil, warn_deprecation: true)
59+
def initialize(user = nil, pass = nil, authz = nil,
60+
username: nil, password: nil, authzid: nil,
61+
warn_deprecation: true, **)
62+
username ||= user or raise ArgumentError, "missing username"
63+
password ||= pass or raise ArgumentError, "missing password"
64+
authzid ||= authz
5965
if warn_deprecation
6066
warn "WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC6331."
6167
# TODO: recommend SCRAM instead.

lib/net/imap/sasl/external_authenticator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ExternalAuthenticator
2929
# #authzid is an optional identity to act as or on behalf of.
3030
#
3131
# Any other keyword parameters are quietly ignored.
32-
def initialize(authzid: nil)
32+
def initialize(authzid: nil, **)
3333
@authzid = authzid&.to_str&.encode "UTF-8"
3434
if @authzid&.match?(/\u0000/u) # also validates UTF8 encoding
3535
raise ArgumentError, "contains NULL"

lib/net/imap/sasl/plain_authenticator.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class Net::IMAP::SASL::PlainAuthenticator
3737
attr_reader :authzid
3838

3939
# :call-seq:
40-
# new(username, password, authzid: nil) -> authenticator
40+
# new(username, password, authzid: nil, **) -> authenticator
41+
# new(username:, password:, authzid: nil, **) -> authenticator
4142
#
4243
# Creates an Authenticator for the "+PLAIN+" SASL mechanism.
4344
#
@@ -50,10 +51,13 @@ class Net::IMAP::SASL::PlainAuthenticator
5051
# * #authzid ― Alternate identity to act as or on behalf of. Optional.
5152
#
5253
# See attribute documentation for more details.
53-
def initialize(username, password, authzid: nil)
54-
raise ArgumentError, "username contains NULL" if username&.include?(NULL)
55-
raise ArgumentError, "password contains NULL" if password&.include?(NULL)
56-
raise ArgumentError, "authzid contains NULL" if authzid&.include?(NULL)
54+
def initialize(user = nil, pass = nil,
55+
username: nil, password: nil, authzid: nil, **)
56+
username ||= user or raise ArgumentError, "missing username"
57+
password ||= pass or raise ArgumentError, "missing password"
58+
raise ArgumentError, "username contains NULL" if username.include?(NULL)
59+
raise ArgumentError, "password contains NULL" if password.include?(NULL)
60+
raise ArgumentError, "authzid contains NULL" if authzid&.include?(NULL)
5761
@username = username
5862
@password = password
5963
@authzid = authzid

lib/net/imap/sasl/xoauth2_authenticator.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Net::IMAP::SASL::XOAuth2Authenticator
3232
attr_reader :oauth2_token
3333

3434
# :call-seq:
35-
# :call-seq:
36-
# new(username, oauth2_token) -> authenticator
35+
# new(username, oauth2_token, **) -> authenticator
36+
# new(username:, oauth2_token:, **) -> authenticator
3737
#
3838
# Creates an Authenticator for the "+XOAUTH2+" SASL mechanism, as specified by
3939
# Google[https://developers.google.com/gmail/imap/xoauth2-protocol],
@@ -47,9 +47,15 @@ class Net::IMAP::SASL::XOAuth2Authenticator
4747
# the service for #username.
4848
#
4949
# See the documentation for each attribute for more details.
50-
def initialize(username, oauth2_token)
51-
@username = username
52-
@oauth2_token = oauth2_token
50+
def initialize(user = nil, token = nil, username: nil, oauth2_token: nil, **)
51+
@username = username || user or
52+
raise ArgumentError, "missing username"
53+
@oauth2_token = oauth2_token || token or
54+
raise ArgumentError, "missing oauth2_token"
55+
[username, user].compact.count == 1 or
56+
raise ArgumentError, "conflicting values for username"
57+
[oauth2_token, token].compact.count == 1 or
58+
raise ArgumentError, "conflicting values for oauth2_token"
5359
end
5460

5561
# :call-seq:

test/net/imap/test_imap_authenticators.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ def test_xoauth2_response
9595
)
9696
end
9797

98+
def test_xoauth2_kwargs
99+
assert_equal(
100+
"user=arg\1auth=Bearer kwarg\1\1",
101+
xoauth2("arg", oauth2_token: "kwarg").process(nil)
102+
)
103+
assert_equal(
104+
"user=user\1auth=Bearer kwarg\1\1",
105+
xoauth2(username: "user", oauth2_token: "kwarg").process(nil)
106+
)
107+
end
108+
98109
def test_xoauth2_supports_initial_response
99110
assert xoauth2("foo", "bar").initial_response?
100111
assert Net::IMAP::SASL.initial_response?(xoauth2("foo", "bar"))

0 commit comments

Comments
 (0)