Skip to content

Commit 0b38655

Browse files
committed
♻️ Support keyword args for SASL XOAUTH2
1 parent 8660f24 commit 0b38655

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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)