Skip to content

Commit

Permalink
Merge 97714aa into 8a6b7a6
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyMcWho committed Jan 11, 2021
2 parents 8a6b7a6 + 97714aa commit f06840a
Show file tree
Hide file tree
Showing 20 changed files with 484 additions and 110 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/main.yml
Expand Up @@ -9,9 +9,9 @@ name: Ruby

on:
push:
branches: [ master ]
branches: [ master, 2_0-indev ]
pull_request:
branches: [ master ]
branches: [ master, 2_0-indev ]

jobs:
test:
Expand Down Expand Up @@ -54,14 +54,29 @@ jobs:
env:
JRUBY_OPTS: --debug
run: bundle exec rake
frozen-string-compat:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run tests
env:
RUBYOPT: "--enable-frozen-string-literal"
run: bundle exec rake
coveralls:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.5
ruby-version: 2.6
bundler-cache: true
- name: Install dependencies
run: bundle install
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -19,7 +19,8 @@ group :test do
gem 'rack', '>= 2.0.6', :platforms => %i[jruby_18 jruby_19 ruby_19 ruby_20 ruby_21]
gem 'rack-test'
gem 'rest-client', '~> 2.0.0', :platforms => [:jruby_18]
gem 'rspec', '~> 3.5.0'
gem 'rspec', '~> 3.5'
gem 'rack-freeze'
gem 'rubocop', '>= 0.58.2', '< 0.69.0', :platforms => %i[ruby_20 ruby_21 ruby_22 ruby_23 ruby_24]
gem 'simplecov-lcov'
gem 'tins', '~> 1.13', :platforms => %i[jruby_18 jruby_19 ruby_19]
Expand Down
19 changes: 9 additions & 10 deletions README.md
Expand Up @@ -2,9 +2,8 @@

[![Gem Version](http://img.shields.io/gem/v/omniauth.svg)][gem]
[![Build Status](http://img.shields.io/travis/omniauth/omniauth.svg)][travis]
[![Code Climate](http://img.shields.io/codeclimate/github/omniauth/omniauth.svg)][codeclimate]
[![Code Climate](https://api.codeclimate.com/v1/badges/ffd33970723587806744/maintainability)][codeclimate]
[![Coverage Status](http://img.shields.io/coveralls/omniauth/omniauth.svg)][coveralls]
[![Security](https://hakiri.io/github/omniauth/omniauth/master.svg)](https://hakiri.io/github/omniauth/omniauth/master)

[gem]: https://rubygems.org/gems/omniauth
[travis]: http://travis-ci.org/omniauth/omniauth
Expand Down Expand Up @@ -33,8 +32,8 @@ development and easily swap in other strategies later.
## Getting Started
Each OmniAuth strategy is a Rack Middleware. That means that you can use
it the same way that you use any other Rack middleware. For example, to
use the built-in Developer strategy in a Sinatra application I might do
this:
use the built-in Developer strategy in a Sinatra application you might
do this:

```ruby
require 'sinatra'
Expand All @@ -46,7 +45,7 @@ class MyApplication < Sinatra::Base
end
```

Because OmniAuth is built for *multi-provider* authentication, I may
Because OmniAuth is built for *multi-provider* authentication, you may
want to leave room to run multiple strategies. For this, the built-in
`OmniAuth::Builder` class gives you an easy way to specify multiple
strategies. Note that there is **no difference** between the following
Expand Down Expand Up @@ -83,14 +82,14 @@ environment of a request to `/auth/:provider/callback`. This hash
contains as much information about the user as OmniAuth was able to
glean from the utilized strategy. You should set up an endpoint in your
application that matches to the callback URL and then performs whatever
steps are necessary for your application. For example, in a Rails app I
would add a line in my `routes.rb` file like this:
steps are necessary for your application. For example, in a Rails app
you would add a line in your `routes.rb` file like this:

```ruby
post '/auth/:provider/callback', to: 'sessions#create'
```

And I might then have a `SessionsController` with code that looks
And you might then have a `SessionsController` with code that looks
something like this:

```ruby
Expand All @@ -112,7 +111,7 @@ class SessionsController < ApplicationController
end
```

The `omniauth.auth` key in the environment hash gives me my
The `omniauth.auth` key in the environment hash provides an
Authentication Hash which will contain information about the just
authenticated user including a unique id, the strategy they just used
for authentication, and personal details such as name and email address
Expand Down Expand Up @@ -167,7 +166,7 @@ a `session_store.rb` initializer, add `use ActionDispatch::Session::CookieStore`
and have sessions functioning as normal.

To be clear: sessions may work, but your session options will be ignored
(i.e the session key will default to `_session_id`). Instead of the
(i.e. the session key will default to `_session_id`). Instead of the
initializer, you'll have to set the relevant options somewhere
before your middleware is built (like `application.rb`) and pass them to your
preferred middleware, like this:
Expand Down
24 changes: 18 additions & 6 deletions lib/omniauth.rb
Expand Up @@ -15,6 +15,7 @@ module Strategies
autoload :Form, 'omniauth/form'
autoload :AuthHash, 'omniauth/auth_hash'
autoload :FailureEndpoint, 'omniauth/failure_endpoint'
autoload :AuthenticityTokenProtection, 'omniauth/authenticity_token_protection'

def self.strategies
@strategies ||= []
Expand All @@ -29,20 +30,22 @@ def self.default_logger
logger
end

def self.defaults
def self.defaults # rubocop:disable MethodLength
@defaults ||= {
:camelizations => {},
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:failure_raise_out_environments => ['development'],
:request_validation_phase => OmniAuth::AuthenticityTokenProtection,
:before_request_phase => nil,
:before_callback_phase => nil,
:before_options_phase => nil,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
:logger => default_logger,
:allowed_request_methods => %i[get post],
:mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Example User'})}
:allowed_request_methods => %i[post],
:mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Example User'})},
:silence_get_warning => false
}
end

Expand Down Expand Up @@ -74,6 +77,14 @@ def before_options_phase(&block)
end
end

def request_validation_phase(&block)
if block_given?
@request_validation_phase = block
else
@request_validation_phase
end
end

def before_request_phase(&block)
if block_given?
@before_request_phase = block
Expand Down Expand Up @@ -111,8 +122,9 @@ def add_camelization(name, camelized)
camelizations[name.to_s] = camelized.to_s
end

attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase, :request_validation_phase
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css,
:test_mode, :mock_auth, :full_host, :camelizations, :logger, :silence_get_warning
end

def self.config
Expand Down Expand Up @@ -159,7 +171,7 @@ def camelize(word, first_letter_in_uppercase = true)
if first_letter_in_uppercase
word.to_s.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
else
word.first + camelize(word)[1..-1]
camelize(word).tap { |w| w[0] = w[0].downcase }
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/omniauth/authenticity_token_protection.rb
@@ -0,0 +1,30 @@
require 'rack-protection'

module OmniAuth
class AuthenticityError < StandardError; end
class AuthenticityTokenProtection < Rack::Protection::AuthenticityToken
def initialize(options = {})
@options = default_options.merge(options)
end

def self.call(env)
new.call!(env)
end

def call!(env)
return if accepts?(env)

instrument env
react env
end

private

def deny(_env)
OmniAuth.logger.send(:warn, "Attack prevented by #{self.class}")
raise AuthenticityError.new(options[:message])
end

alias default_reaction deny
end
end
2 changes: 1 addition & 1 deletion lib/omniauth/builder.rb
Expand Up @@ -31,7 +31,7 @@ def provider(klass, *args, &block)
middleware = klass
else
begin
middleware = OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize(klass.to_s).to_s)
middleware = OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize(klass.to_s).to_s, false)
rescue NameError
raise(LoadError.new("Could not find matching strategy for #{klass.inspect}. You may need to install an additional gem (such as omniauth-#{klass})."))
end
Expand Down
11 changes: 10 additions & 1 deletion lib/omniauth/failure_endpoint.rb
Expand Up @@ -27,10 +27,19 @@ def raise_out!

def redirect_to_failure
message_key = env['omniauth.error.type']
new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}"

new_path = "#{env['SCRIPT_NAME']}#{strategy_path_prefix}/failure?message=#{Rack::Utils.escape(message_key)}#{origin_query_param}#{strategy_name_query_param}"
Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
end

def strategy_path_prefix
if env['omniauth.error.strategy']
env['omniauth.error.strategy'].path_prefix
else
OmniAuth.config.path_prefix
end
end

def strategy_name_query_param
return '' unless env['omniauth.error.strategy']

Expand Down
2 changes: 1 addition & 1 deletion lib/omniauth/form.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(options = {})
options[:header_info] ||= ''
self.options = options

@html = ''
@html = +'' # unary + string allows it to be mutable if strings are frozen
@with_custom_button = false
@footer = nil
header(options[:title], options[:header_info])
Expand Down

0 comments on commit f06840a

Please sign in to comment.