diff --git a/README.md b/README.md index bd037064..e08f8355 100644 --- a/README.md +++ b/README.md @@ -51,11 +51,10 @@ imap.expunge ## Development -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. +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. 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). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/net-imap. - diff --git a/lib/net/imap/authenticators.rb b/lib/net/imap/authenticators.rb index 0aa864fc..d6f5ff69 100644 --- a/lib/net/imap/authenticators.rb +++ b/lib/net/imap/authenticators.rb @@ -45,3 +45,4 @@ def authenticators require_relative "authenticators/login" require_relative "authenticators/cram_md5" require_relative "authenticators/digest_md5" +require_relative "authenticators/xoauth2" diff --git a/lib/net/imap/authenticators/xoauth2.rb b/lib/net/imap/authenticators/xoauth2.rb new file mode 100644 index 00000000..41057020 --- /dev/null +++ b/lib/net/imap/authenticators/xoauth2.rb @@ -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", user, oauth2_token) + end + + Net::IMAP.add_authenticator 'XOAUTH2', self +end diff --git a/test/net/imap/test_imap_authenticators.rb b/test/net/imap/test_imap_authenticators.rb index 97519615..4e3acd24 100644 --- a/test/net/imap/test_imap_authenticators.rb +++ b/test/net/imap/test_imap_authenticators.rb @@ -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", + Net::IMAP::XOauth2Authenticator.new("username", "token").process(nil) + ) + end + # ---------------------- # LOGIN (obsolete) # ---------------------- @@ -128,5 +139,4 @@ def test_digest_md5_authenticator ) ) end - end