Skip to content

Commit

Permalink
Add XOAuth2 authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
ssunday committed Sep 28, 2022
1 parent 64b75b6 commit 4e028e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/net/imap/authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ def authenticators
require_relative "authenticators/login"
require_relative "authenticators/cram_md5"
require_relative "authenticators/digest_md5"
require_relative "authenticators/xoauth2"
20 changes: 20 additions & 0 deletions lib/net/imap/authenticators/xoauth2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class Net::IMAP::XOauth2Authenticator
def process(_data)
build_oauth2_string(@user, @oauth2_token)
end

private

def initialize(user, oauth2_token)
@user = user
@oauth2_token = oauth2_token
end

def build_oauth2_string(user, oauth2_token)
format("user=%s\1auth=Bearer %s\1\1".encode('us-ascii'), user, oauth2_token)
end

Net::IMAP.add_authenticator 'XOAUTH2', self
end
12 changes: 11 additions & 1 deletion test/net/imap/test_imap_authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def test_plain_no_null_chars
assert_raise(ArgumentError) { plain("u", "p", authzid: "bad\0authz") }
end

# ----------------------
# XOAUTH2
# ----------------------

def test_xoauth2
assert_equal(
"user=username\1auth=Bearer token\1\1".encode('us-ascii'),
Net::IMAP::XOauth2Authenticator.new("username", "token").process(nil)
)
end

# ----------------------
# LOGIN (obsolete)
# ----------------------
Expand Down Expand Up @@ -128,5 +139,4 @@ def test_digest_md5_authenticator
)
)
end

end

0 comments on commit 4e028e9

Please sign in to comment.