Skip to content

Commit a0d6724

Browse files
authored
Merge pull request #63 from ssunday/add-xoauth2-authenticator
Add XOAUTH2 authenticator
2 parents 64b75b6 + 16937b7 commit a0d6724

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ imap.expunge
5151

5252
## Development
5353

54-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
5555

5656
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
5757

5858
## Contributing
5959

6060
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/net-imap.
61-

lib/net/imap/authenticators.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ def authenticators
4545
require_relative "authenticators/login"
4646
require_relative "authenticators/cram_md5"
4747
require_relative "authenticators/digest_md5"
48+
require_relative "authenticators/xoauth2"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
class Net::IMAP::XOauth2Authenticator
4+
def process(_data)
5+
build_oauth2_string(@user, @oauth2_token)
6+
end
7+
8+
private
9+
10+
def initialize(user, oauth2_token)
11+
@user = user
12+
@oauth2_token = oauth2_token
13+
end
14+
15+
def build_oauth2_string(user, oauth2_token)
16+
format("user=%s\1auth=Bearer %s\1\1", user, oauth2_token)
17+
end
18+
19+
Net::IMAP.add_authenticator 'XOAUTH2', self
20+
end

test/net/imap/test_imap_authenticators.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def test_plain_no_null_chars
2929
assert_raise(ArgumentError) { plain("u", "p", authzid: "bad\0authz") }
3030
end
3131

32+
# ----------------------
33+
# XOAUTH2
34+
# ----------------------
35+
36+
def test_xoauth2
37+
assert_equal(
38+
"user=username\1auth=Bearer token\1\1",
39+
Net::IMAP::XOauth2Authenticator.new("username", "token").process(nil)
40+
)
41+
end
42+
3243
# ----------------------
3344
# LOGIN (obsolete)
3445
# ----------------------
@@ -128,5 +139,4 @@ def test_digest_md5_authenticator
128139
)
129140
)
130141
end
131-
132142
end

0 commit comments

Comments
 (0)