OAuth 1.0 Logo by Chris Messina, CC BY-SA 3.0, via Wikimedia Commons Yukihiro Matsumoto, Ruby Visual Identity Team, CC BY-SA 2.5

Ruby OAuth

OAuth 1.0 is an industry-standard protocol for authorization.

This is a RubyGem for implementing both OAuth 1.0 clients and servers in Ruby applications. See the sibling oauth2 gem for OAuth 2.0 implementations in Ruby.

Status

Project bundle add oauth2
1️⃣ name, license, docs RubyGems.org License: MIT FOSSA RubyDoc.info InchCI
2️⃣ version & activity Gem Version Total Downloads Download Rank Source Code Open PRs Closed PRs <!--Next Version-->
3️⃣ maintanence & linting Maintainability Helpers Depfu Contributors Style Kloc Roll
4️⃣ testing Open Issues Closed Issues Supported Heads Unofficial Support MacOS Windows
5️⃣ coverage & security CodeClimate CodeCov Coveralls Security Policy CodeQL Code Coverage
6️⃣ resources Discussion Get help on Codementor Chat Blog Blog
7️⃣ spread 💖 Liberapay Patrons Sponsor Me Tweet @ Peter 🌏 👼 💻 🌹

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add oauth

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install oauth

OAuth for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of OAuth2 and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.

Security contact information Security Policy

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

For more see SECURITY.md.

Compatibility

Targeted ruby compatibility is non-EOL versions of Ruby, currently 2.7, 3.0, and 3.1. Ruby is limited to 2.4+ in the gemspec, and this will change with minor version bumps, while the gem is still in 0.x, in accordance with the SemVer spec.

The master branch now targets 1.0.x releases. See v0.6-maintenance branch for Ruby >= 2.4. See v0.5-maintenance branch for Ruby >= 2.0.

NOTE: If there is another 0.5.x release it is anticipated to be the last of the 0.5.x series.

Ruby Engine Compatibility Policy This gem is tested against MRI, JRuby, and Truffleruby. Each of those has varying versions that target a specific version of MRI Ruby. This gem should work in the just-listed Ruby engines according to the targeted MRI compatibility in the table below. If you would like to add support for additional engines, first make sure Github Actions supports the engine, then submit a PR to the correct maintenance branch as according to the table below.
Ruby Version Compatibility Policy If something doesn't work on one of these interpreters, it's a bug. This library may inadvertently work (or seem to work) on other Ruby implementations, however support will only be provided for the versions listed above. If you would like this library to support another Ruby version, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.
Ruby OAuth Version Maintenance Branch 🚂 Compatibility Official 💎 Unofficial 💎 Incidental 💎
1️⃣ 1.0.x master Rails 6, 7 2.7, 3.0, 3.1 sorry, not sorry nope
2️⃣ 0.6.x v0.6-maintenance Rails 5, 6, 7 2.7, 3.0, 3.1 2.5, 2.6 2.4
3️⃣ 0.5.x v0.5-maintenance Rails 2, 3, 4, 5, 6, 7 2.7, 3.0, 3.1 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 2.0
4️⃣ older N/A Best of luck to you! Please upgrade! noop

NOTE: Once 1.0 is released, the 0.x series will only receive critical bug and security updates. See SECURITY.md

🚂 NOTE: See notes on Rails in next section.

Basics

This is a ruby library which is intended to be used in creating Ruby Consumer and Service Provider applications. It is NOT a Rails plugin, but could easily be used for the foundation for such a Rails plugin.

This gem was originally extracted from @pelle's oauth-plugin gem. After extraction that gem was made to depend on this gem.

Unfortunately, this gem does have some Rails related bits that are optional to load. You don't need Rails! The Rails bits may be pulled out into a separate gem after the release of version 1.0 of this gem.

Usage

We need to specify the oauth_callback url explicitly, otherwise it defaults to "oob" (Out of Band)

callback_url = "http://127.0.0.1:3000/oauth/callback"

Create a new OAuth::Consumer instance by passing it a configuration hash:

oauth_consumer = OAuth::Consumer.new("key", "secret", site: "https://agree2")

Start the process by requesting a token

request_token = oauth_consumer.get_request_token(oauth_callback: callback_url)

session[:token] = request_token.token
session[:token_secret] = request_token.secret
redirect_to request_token.authorize_url(oauth_callback: callback_url)

When user returns create an access_token

hash = { oauth_token: session[:token], oauth_token_secret: session[:token_secret] }
request_token = OAuth::RequestToken.from_hash(oauth_consumer, hash)
access_token = request_token.get_access_token
# For 3-legged authorization, flow oauth_verifier is passed as param in callback
# access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
@photos = access_token.get("/photos.xml")

Now that you have an access token, you can use Typhoeus to interact with the OAuth provider if you choose.

require "typhoeus"
require "oauth/request_proxy/typhoeus_request"
oauth_params = { consumer: oauth_consumer, token: access_token }
hydra = Typhoeus::Hydra.new
req = Typhoeus::Request.new(uri, options) # :method needs to be specified in options
oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(request_uri: uri))
req.options[:headers]["Authorization"] = oauth_helper.header # Signs the request
hydra.queue(req)
hydra.run
@response = req.response

More Information

  • RubyDoc Documentation: RubyDoc.info
  • Mailing List/Google Group: Mailing List
  • GitHub Discussions: Discussion
  • Live Chat on Gitter: Join the chat at https://gitter.im/oauth-xx/oauth-ruby
  • Maintainer's Blog: Blog

Contributing

See CONTRIBUTING.md

Contributors

Contributors

Made with [contributors-img][contrib-rocks].

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. Compatibility with a major and minor versions of Ruby will only be changed with a major version bump.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency "oauth", "~> 0.6.0"

License

The gem is available as open source under the terms of the MIT License License: MIT. See LICENSE for the [Copyright Notice][copyright-notice-explainer].

Contact

OAuth Ruby has been created and maintained by a large number of talented individuals. The current maintainer is Peter Boling (@pboling).

Comments are welcome. Contact the OAuth Ruby mailing list (Google Group) or GitHub Discussions.

[comment]: <> (Following links are used by README, CONTRIBUTING, Homepage)

[comment]: <> (Following links are used by README, Homepage)