Skip to content

Commit

Permalink
Merge pull request #63 from ssunday/add-xoauth2-authenticator
Browse files Browse the repository at this point in the history
Add XOAUTH2 authenticator
  • Loading branch information
shugo authored Sep 29, 2022
2 parents 64b75b6 + 16937b7 commit a0d6724
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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", 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",
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 a0d6724

Please sign in to comment.