OAuth 2.0 is the industry-standard protocol for authorization.
OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications,
desktop applications, mobile phones, and living room devices.
This is a RubyGem for implementing OAuth 2.0 clients and servers in Ruby applications.
See the sibling oauth
gem for OAuth 1.0 implementations in Ruby.
README
of the
supported-only-for-critical-enterprise-security-issues 1-4-stable
branch. Please do not use this, and instead upgrade to version 2!
No further releases of 1.x series are planned! Version 2 has tons of improvements!
If you must continue using 1.4.x please consider purchasing an open source security maintenance contract from Tidelift.
1.4.x Readmes
Version | Release Date | Readme |
---|---|---|
1.4.10 | Jul 1, 2022 | https://github.com/oauth-xx/oauth2/blob/v1.4.10/README.md |
1.4.9 | Feb 20, 2022 | https://github.com/oauth-xx/oauth2/blob/v1.4.9/README.md |
1.4.8 | Feb 18, 2022 | https://github.com/oauth-xx/oauth2/blob/v1.4.8/README.md |
1.4.7 | Mar 19, 2021 | https://github.com/oauth-xx/oauth2/blob/v1.4.7/README.md |
1.4.6 | Mar 19, 2021 | https://github.com/oauth-xx/oauth2/blob/v1.4.6/README.md |
1.4.5 | Mar 18, 2021 | https://github.com/oauth-xx/oauth2/blob/v1.4.5/README.md |
1.4.4 | Feb 12, 2020 | https://github.com/oauth-xx/oauth2/blob/v1.4.4/README.md |
1.4.3 | Jan 29, 2020 | https://github.com/oauth-xx/oauth2/blob/v1.4.3/README.md |
1.4.2 | Oct 1, 2019 | https://github.com/oauth-xx/oauth2/blob/v1.4.2/README.md |
1.4.1 | Oct 13, 2018 | https://github.com/oauth-xx/oauth2/blob/v1.4.1/README.md |
1.4.0 | Jun 9, 2017 | https://github.com/oauth-xx/oauth2/blob/v1.4.0/README.md |
1.3.x Readmes
Version | Release Date | Readme |
---|---|---|
1.3.1 | Mar 3, 2017 | https://github.com/oauth-xx/oauth2/blob/v1.3.1/README.md |
1.3.0 | Dec 27, 2016 | https://github.com/oauth-xx/oauth2/blob/v1.3.0/README.md |
≤= 1.2.x Readmes (2016 and before)
Version | Release Date | Readme |
---|---|---|
1.2.0 | Jun 30, 2016 | https://github.com/oauth-xx/oauth2/blob/v1.2.0/README.md |
1.1.0 | Jan 30, 2016 | https://github.com/oauth-xx/oauth2/blob/v1.1.0/README.md |
1.0.0 | May 23, 2014 | https://github.com/oauth-xx/oauth2/blob/v1.0.0/README.md |
< 1.0.0 | Find here | https://github.com/oauth-xx/oauth2/tags |
Project | oauth2 | |
---|---|---|
1️⃣ | name, license, docs | |
2️⃣ | version & activity | |
3️⃣ | maintanence & linting | |
4️⃣ | testing | |
5️⃣ | coverage & security | |
6️⃣ | resources | |
7️⃣ | spread 💖 | 🌏 👼 💻 🌹 |
gem install oauth2
Or inside a Gemfile
gem 'oauth2'
And then execute in a shell:
bundle
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.
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
For more see SECURITY.md.
Targeted ruby compatibility is 2.7, 3.0 and 3.1. Compatibility is further distinguished by
supported and unsupported versions of Ruby.
This gem will work with Ruby versions back to 1.9, though it remains unsupported.
Ruby is limited to 1.9+ in the gemspec for the 1.4.x series and is be 2.2+ for next major version releases (see master
branch).
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 OAuth2 Version | Maintenance Branch | Supported Officially | Supported Unofficially | Supported Incidentally | |
---|---|---|---|---|---|
1️⃣ | 2.0.x | master |
2.7, 3.0, 3.1 | 2.5, 2.6 | 2.2, 2.3, 2.4 |
2️⃣ | 1.4.x | 1-4-stable |
2.5, 2.6, 2.7, 3.0, 3.1 | 2.1, 2.2, 2.3, 2.4 | 1.9, 2.0 |
3️⃣ | older | N/A | Best of luck to you! | Please upgrade! |
NOTE: The 1.4 series will only receive critical security updates. See SECURITY.md
require 'oauth2'
client = OAuth2::Client.new('client_id', 'client_secret', :site => 'https://example.org')
client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
# => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:8080/oauth2/callback"
token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:8080/oauth2/callback', :headers => {'Authorization' => 'Basic some_password'})
response = token.get('/api/resource', :params => {'query_foo' => 'bar'})
response.class.name
# => OAuth2::Response
Debugging
Set an environment variable, however you would normally do that.
# will log both request and response, including bodies
ENV['OAUTH_DEBUG'] = 'true'
By default, debug output will go to $stdout
. This can be overridden when
initializing your OAuth2::Client.
require 'oauth2'
client = OAuth2::Client.new(
'client_id',
'client_secret',
:site => 'https://example.org',
:logger => Logger.new('example.log', 'weekly')
)
The AccessToken
methods #get
, #post
, #put
and #delete
and the generic #request
will return an instance of the #OAuth2::Response class.
This instance contains a #parsed
method that will parse the response body and
return a Hash if the Content-Type
is application/x-www-form-urlencoded
or if
the body is a JSON object. It will return an Array if the body is a JSON
array. Otherwise, it will return the original body string.
The original response body, headers, and status can be accessed via their respective methods.
If you have an existing Access Token for a user, you can initialize an instance
using various class methods including the standard new, from_hash
(if you have
a hash of the values), or from_kvform
(if you have an
application/x-www-form-urlencoded
encoded string of the values).
On 400+ status code responses, an OAuth2::Error
will be raised. If it is a
standard OAuth2 error response, the body will be parsed and #code
and #description
will contain the values provided from the error and
error_description
parameters. The #response
property of OAuth2::Error
will
always contain the OAuth2::Response
instance.
If you do not want an error to be raised, you may use :raise_errors => false
option on initialization of the client. In this case the OAuth2::Response
instance will be returned as usual and on 400+ status code responses, the
Response instance will contain the OAuth2::Error
instance.
Currently the Authorization Code, Implicit, Resource Owner Password Credentials, Client Credentials, and Assertion
authentication grant types have helper strategy classes that simplify client
use. They are available via the #auth_code
, #implicit
, #password
, #client_credentials
, and #assertion
methods respectively.
auth_url = client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')
auth_url = client.implicit.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
# get the token params in the callback and
token = OAuth2::AccessToken.from_kvform(client, query_string)
token = client.password.get_token('username', 'password')
token = client.client_credentials.get_token
token = client.assertion.get_token(assertion_params)
If you want to specify additional headers to be sent out with the request, add a 'headers' hash under 'params':
token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback', :headers => {'Some' => 'Header'})
You can always use the #request
method on the OAuth2::Client
instance to make
requests for tokens for any Authentication grant type.
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.
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 'oauth2', '~> 1.4'
- Copyright (c) 2011-2013 Michael Bleigh and Intridea, Inc.
- Copyright (c) 2017-2022 oauth-xx organization
- See LICENSE for details.
After checking out the repo, run bundle install
to install dependencies. Then, run bundle excec rake spec
to run the tests.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/oauth-xx/oauth2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Everyone interacting in the OAuth2 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.