Skip to content

Commit

Permalink
♻️ Support keyword args for SASL XOAUTH2
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Sep 20, 2023
1 parent 4cd94c8 commit 319e380
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/net/imap/sasl/xoauth2_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Net::IMAP::SASL::XOAuth2Authenticator
attr_reader :oauth2_token

# :call-seq:
# :call-seq:
# new(username, oauth2_token) -> authenticator
# new(username, oauth2_token, **) -> authenticator
# new(username:, oauth2_token:, **) -> authenticator
#
# Creates an Authenticator for the "+XOAUTH2+" SASL mechanism, as specified by
# Google[https://developers.google.com/gmail/imap/xoauth2-protocol],
Expand All @@ -47,9 +47,15 @@ class Net::IMAP::SASL::XOAuth2Authenticator
# the service for #username.
#
# See the documentation for each attribute for more details.
def initialize(user, oauth2_token)
@user = user
@oauth2_token = oauth2_token
def initialize(user = nil, token = nil, username: nil, oauth2_token: nil, **)
@username = username || user or
raise ArgumentError, "missing username"
@oauth2_token = oauth2_token || token or
raise ArgumentError, "missing oauth2_token"
[username, user].compact.count == 1 or
raise ArgumentError, "conflicting values for username"
[oauth2_token, token].compact.count == 1 or
raise ArgumentError, "conflicting values for oauth2_token"
end

# :call-seq:
Expand All @@ -61,7 +67,7 @@ def initial_response?; true end
# Returns the XOAUTH2 formatted response, which combines the +username+
# with the +oauth2_token+.
def process(_data)
build_oauth2_string(@user, @oauth2_token)
build_oauth2_string(@username, @oauth2_token)
end

private
Expand Down
11 changes: 11 additions & 0 deletions test/net/imap/test_imap_authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ def test_xoauth2_response
)
end

def test_xoauth2_kwargs
assert_equal(
"user=arg\1auth=Bearer kwarg\1\1",
xoauth2("arg", oauth2_token: "kwarg").process(nil)
)
assert_equal(
"user=user\1auth=Bearer kwarg\1\1",
xoauth2(username: "user", oauth2_token: "kwarg").process(nil)
)
end

def test_xoauth2_supports_initial_response
assert xoauth2("foo", "bar").initial_response?
assert Net::IMAP::SASL.initial_response?(xoauth2("foo", "bar"))
Expand Down

0 comments on commit 319e380

Please sign in to comment.