omniauth-oauth2-1.5.0/0000755000004100000410000000000013270053541014571 5ustar www-datawww-dataomniauth-oauth2-1.5.0/.travis.yml0000644000004100000410000000046313270053541016705 0ustar www-datawww-databefore_install: gem install bundler env: global: - JRUBY_OPTS="$JRUBY_OPTS --debug" language: ruby rvm: - jruby-9000 - 2.1.10 # EOL Soon - 2.2.6 - 2.3.3 - 2.4.0 - jruby-head - ruby-head matrix: allow_failures: - rvm: jruby-head - rvm: ruby-head fast_finish: true sudo: false omniauth-oauth2-1.5.0/.rspec0000644000004100000410000000003313270053541015702 0ustar www-datawww-data--colour --format=progress omniauth-oauth2-1.5.0/README.md0000644000004100000410000000436013270053541016053 0ustar www-datawww-data# OmniAuth OAuth2 [![Gem Version](http://img.shields.io/gem/v/omniauth-oauth2.svg)][gem] [![Build Status](http://img.shields.io/travis/omniauth/omniauth-oauth2.svg)][travis] [![Dependency Status](http://img.shields.io/gemnasium/omniauth/omniauth-oauth2.svg)][gemnasium] [![Code Climate](http://img.shields.io/codeclimate/github/intridea/omniauth-oauth2.svg)][codeclimate] [![Coverage Status](http://img.shields.io/coveralls/intridea/omniauth-oauth2.svg)][coveralls] [gem]: https://rubygems.org/gems/omniauth-oauth2 [travis]: http://travis-ci.org/intridea/omniauth-oauth2 [gemnasium]: https://gemnasium.com/intridea/omniauth-oauth2 [codeclimate]: https://codeclimate.com/github/intridea/omniauth-oauth2 [coveralls]: https://coveralls.io/r/intridea/omniauth-oauth2 This gem contains a generic OAuth2 strategy for OmniAuth. It is meant to serve as a building block strategy for other strategies and not to be used independently (since it has no inherent way to gather uid and user info). ## Creating an OAuth2 Strategy To create an OmniAuth OAuth2 strategy using this gem, you can simply subclass it and add a few extra methods like so: ```ruby require 'omniauth-oauth2' module OmniAuth module Strategies class SomeSite < OmniAuth::Strategies::OAuth2 # Give your strategy a name. option :name, "some_site" # This is where you pass the options you would pass when # initializing your consumer from the OAuth gem. option :client_options, {:site => "https://api.somesite.com"} # These are called after authentication has succeeded. If # possible, you should try to set the UID without making # additional calls (if the user id is returned with the token # or as a URI parameter). This may not be possible with all # providers. uid{ raw_info['id'] } info do { :name => raw_info['name'], :email => raw_info['email'] } end extra do { 'raw_info' => raw_info } end def raw_info @raw_info ||= access_token.get('/me').parsed end end end end ``` That's pretty much it! [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/intridea/omniauth-oauth2/trend.png)](https://bitdeli.com/free "Bitdeli Badge") omniauth-oauth2-1.5.0/LICENSE.md0000644000004100000410000000211113270053541016170 0ustar www-datawww-dataCopyright (C) 2014 Michael Bleigh, Erik Michaels-Ober and Intridea, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. omniauth-oauth2-1.5.0/spec/0000755000004100000410000000000013270053541015523 5ustar www-datawww-dataomniauth-oauth2-1.5.0/spec/helper.rb0000644000004100000410000000124313270053541017327 0ustar www-datawww-data$LOAD_PATH.unshift File.expand_path("..", __FILE__) $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) if RUBY_VERSION >= "1.9" require "simplecov" require "coveralls" SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter] SimpleCov.start do minimum_coverage(78.48) end end require "rspec" require "rack/test" require "webmock/rspec" require "omniauth" require "omniauth-oauth2" RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :expect end config.extend OmniAuth::Test::StrategyMacros, :type => :strategy config.include Rack::Test::Methods config.include WebMock::API end omniauth-oauth2-1.5.0/spec/omniauth/0000755000004100000410000000000013270053541017347 5ustar www-datawww-dataomniauth-oauth2-1.5.0/spec/omniauth/strategies/0000755000004100000410000000000013270053541021521 5ustar www-datawww-dataomniauth-oauth2-1.5.0/spec/omniauth/strategies/oauth2_spec.rb0000644000004100000410000000721513270053541024267 0ustar www-datawww-datarequire "helper" describe OmniAuth::Strategies::OAuth2 do # rubocop:disable Metrics/BlockLength def app lambda do |_env| [200, {}, ["Hello."]] end end let(:fresh_strategy) { Class.new(OmniAuth::Strategies::OAuth2) } before do OmniAuth.config.test_mode = true end after do OmniAuth.config.test_mode = false end describe "Subclassing Behavior" do subject { fresh_strategy } it "performs the OmniAuth::Strategy included hook" do expect(OmniAuth.strategies).to include(OmniAuth::Strategies::OAuth2) expect(OmniAuth.strategies).to include(subject) end end describe "#client" do subject { fresh_strategy } it "is initialized with symbolized client_options" do instance = subject.new(app, :client_options => {"authorize_url" => "https://example.com"}) expect(instance.client.options[:authorize_url]).to eq("https://example.com") end it "sets ssl options as connection options" do instance = subject.new(app, :client_options => {"ssl" => {"ca_path" => "foo"}}) expect(instance.client.options[:connection_opts][:ssl]).to eq(:ca_path => "foo") end end describe "#authorize_params" do subject { fresh_strategy } it "includes any authorize params passed in the :authorize_params option" do instance = subject.new("abc", "def", :authorize_params => {:foo => "bar", :baz => "zip"}) expect(instance.authorize_params["foo"]).to eq("bar") expect(instance.authorize_params["baz"]).to eq("zip") end it "includes top-level options that are marked as :authorize_options" do instance = subject.new("abc", "def", :authorize_options => %i[scope foo state], :scope => "bar", :foo => "baz") expect(instance.authorize_params["scope"]).to eq("bar") expect(instance.authorize_params["foo"]).to eq("baz") end it "includes random state in the authorize params" do instance = subject.new("abc", "def") expect(instance.authorize_params.keys).to eq(["state"]) expect(instance.session["omniauth.state"]).not_to be_empty end end describe "#token_params" do subject { fresh_strategy } it "includes any authorize params passed in the :authorize_params option" do instance = subject.new("abc", "def", :token_params => {:foo => "bar", :baz => "zip"}) expect(instance.token_params).to eq("foo" => "bar", "baz" => "zip") end it "includes top-level options that are marked as :authorize_options" do instance = subject.new("abc", "def", :token_options => %i[scope foo], :scope => "bar", :foo => "baz") expect(instance.token_params).to eq("scope" => "bar", "foo" => "baz") end end describe "#callback_phase" do subject { fresh_strategy } it "calls fail with the client error received" do instance = subject.new("abc", "def") allow(instance).to receive(:request) do double("Request", :params => {"error_reason" => "user_denied", "error" => "access_denied"}) end expect(instance).to receive(:fail!).with("user_denied", anything) instance.callback_phase end end end describe OmniAuth::Strategies::OAuth2::CallbackError do let(:error) { Class.new(OmniAuth::Strategies::OAuth2::CallbackError) } describe "#message" do subject { error } it "includes all of the attributes" do instance = subject.new("error", "description", "uri") expect(instance.message).to match(/error/) expect(instance.message).to match(/description/) expect(instance.message).to match(/uri/) end it "includes all of the attributes" do instance = subject.new(nil, :symbol) expect(instance.message).to eq("symbol") end end end omniauth-oauth2-1.5.0/.rubocop.yml0000644000004100000410000000146013270053541017044 0ustar www-datawww-dataLayout/AccessModifierIndentation: EnforcedStyle: outdent Layout/SpaceInsideHashLiteralBraces: EnforcedStyle: no_space Metrics/BlockNesting: Max: 2 Metrics/LineLength: AllowURI: true Enabled: false Metrics/MethodLength: CountComments: false Max: 10 Metrics/ParameterLists: Max: 4 CountKeywordArgs: true Style/CollectionMethods: PreferredMethods: map: 'collect' reduce: 'inject' find: 'detect' find_all: 'select' Style/Documentation: Enabled: false Style/DoubleNegation: Enabled: false Style/HashSyntax: EnforcedStyle: hash_rockets Style/StderrPuts: Enabled: false Style/StringLiterals: EnforcedStyle: double_quotes Style/TrailingCommaInArguments: EnforcedStyleForMultiline: comma Style/TrailingCommaInLiteral: EnforcedStyleForMultiline: comma omniauth-oauth2-1.5.0/.gitignore0000644000004100000410000000024013270053541016555 0ustar www-datawww-data*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp *.swp omniauth-oauth2-1.5.0/omniauth-oauth2.gemspec0000644000004100000410000000177213270053541021171 0ustar www-datawww-datalib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "omniauth-oauth2/version" Gem::Specification.new do |gem| gem.add_dependency "oauth2", "~> 1.1" gem.add_dependency "omniauth", "~> 1.2" gem.add_development_dependency "bundler", "~> 1.0" gem.authors = ["Michael Bleigh", "Erik Michaels-Ober", "Tom Milewski"] gem.email = ["michael@intridea.com", "sferik@gmail.com", "tmilewski@gmail.com"] gem.description = "An abstract OAuth2 strategy for OmniAuth." gem.summary = gem.description gem.homepage = "https://github.com/omniauth/omniauth-oauth2" gem.licenses = %w[MIT] gem.executables = `git ls-files -- bin/*`.split("\n").collect { |f| File.basename(f) } gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.name = "omniauth-oauth2" gem.require_paths = %w[lib] gem.version = OmniAuth::OAuth2::VERSION end omniauth-oauth2-1.5.0/Rakefile0000755000004100000410000000046413270053541016245 0ustar www-datawww-data#!/usr/bin/env rake require "bundler/gem_tasks" require "rspec/core/rake_task" RSpec::Core::RakeTask.new task :test => :spec begin require "rubocop/rake_task" RuboCop::RakeTask.new rescue LoadError task :rubocop do $stderr.puts "RuboCop is disabled" end end task :default => %i[spec rubocop] omniauth-oauth2-1.5.0/lib/0000755000004100000410000000000013270053541015337 5ustar www-datawww-dataomniauth-oauth2-1.5.0/lib/omniauth-oauth2/0000755000004100000410000000000013270053541020363 5ustar www-datawww-dataomniauth-oauth2-1.5.0/lib/omniauth-oauth2/version.rb0000644000004100000410000000010713270053541022373 0ustar www-datawww-datamodule OmniAuth module OAuth2 VERSION = "1.5.0".freeze end end omniauth-oauth2-1.5.0/lib/omniauth/0000755000004100000410000000000013270053541017163 5ustar www-datawww-dataomniauth-oauth2-1.5.0/lib/omniauth/strategies/0000755000004100000410000000000013270053541021335 5ustar www-datawww-dataomniauth-oauth2-1.5.0/lib/omniauth/strategies/oauth2.rb0000644000004100000410000001043513270053541023067 0ustar www-datawww-datarequire "oauth2" require "omniauth" require "securerandom" require "socket" # for SocketError require "timeout" # for Timeout::Error module OmniAuth module Strategies # Authentication strategy for connecting with APIs constructed using # the [OAuth 2.0 Specification](http://tools.ietf.org/html/draft-ietf-oauth-v2-10). # You must generally register your application with the provider and # utilize an application id and secret in order to authenticate using # OAuth 2.0. class OAuth2 include OmniAuth::Strategy def self.inherited(subclass) OmniAuth::Strategy.included(subclass) end args %i[client_id client_secret] option :client_id, nil option :client_secret, nil option :client_options, {} option :authorize_params, {} option :authorize_options, [:scope] option :token_params, {} option :token_options, [] option :auth_token_params, {} option :provider_ignores_state, false attr_accessor :access_token def client ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options)) end credentials do hash = {"token" => access_token.token} hash["refresh_token"] = access_token.refresh_token if access_token.expires? && access_token.refresh_token hash["expires_at"] = access_token.expires_at if access_token.expires? hash["expires"] = access_token.expires? hash end def request_phase redirect client.auth_code.authorize_url({:redirect_uri => callback_url}.merge(authorize_params)) end def authorize_params options.authorize_params[:state] = SecureRandom.hex(24) params = options.authorize_params.merge(options_for("authorize")) if OmniAuth.config.test_mode @env ||= {} @env["rack.session"] ||= {} end session["omniauth.state"] = params[:state] params end def token_params options.token_params.merge(options_for("token")) end def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity error = request.params["error_reason"] || request.params["error"] if error fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"])) elsif !options.provider_ignores_state && (request.params["state"].to_s.empty? || request.params["state"] != session.delete("omniauth.state")) fail!(:csrf_detected, CallbackError.new(:csrf_detected, "CSRF detected")) else self.access_token = build_access_token self.access_token = access_token.refresh! if access_token.expired? super end rescue ::OAuth2::Error, CallbackError => e fail!(:invalid_credentials, e) rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e fail!(:timeout, e) rescue ::SocketError => e fail!(:failed_to_connect, e) end protected def build_access_token verifier = request.params["code"] client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)), deep_symbolize(options.auth_token_params)) end def deep_symbolize(options) hash = {} options.each do |key, value| hash[key.to_sym] = value.is_a?(Hash) ? deep_symbolize(value) : value end hash end def options_for(option) hash = {} options.send(:"#{option}_options").select { |key| options[key] }.each do |key| hash[key.to_sym] = options[key] end hash end # An error that is indicated in the OAuth 2.0 callback. # This could be a `redirect_uri_mismatch` or other class CallbackError < StandardError attr_accessor :error, :error_reason, :error_uri def initialize(error, error_reason = nil, error_uri = nil) self.error = error self.error_reason = error_reason self.error_uri = error_uri end def message [error, error_reason, error_uri].compact.join(" | ") end end end end end OmniAuth.config.add_camelization "oauth2", "OAuth2" omniauth-oauth2-1.5.0/lib/omniauth-oauth2.rb0000644000004100000410000000014213270053541020705 0ustar www-datawww-datarequire "omniauth-oauth2/version" # rubocop:disable FileName require "omniauth/strategies/oauth2" omniauth-oauth2-1.5.0/Gemfile0000644000004100000410000000112513270053541016063 0ustar www-datawww-datasource "http://rubygems.org" gem "rake", "~> 10.5" group :test do gem "addressable", "~> 2.3.8", :platforms => %i[jruby ruby_18] gem "coveralls" gem "json", :platforms => %i[jruby ruby_18 ruby_19] gem "mime-types", "~> 1.25", :platforms => %i[jruby ruby_18] gem "rack-test" gem "rest-client", "~> 1.7.3", :platforms => %i[jruby ruby_18] gem "rspec", "~> 3.2" gem "rubocop", ">= 0.51", :platforms => %i[ruby_19 ruby_20 ruby_21 ruby_22 ruby_23 ruby_24] gem "simplecov", ">= 0.9" gem "webmock", "~> 1.0" end # Specify your gem's dependencies in omniauth-oauth2.gemspec gemspec