diff --git a/lib/omniauth.rb b/lib/omniauth.rb index af622f2..f5bf66e 100644 --- a/lib/omniauth.rb +++ b/lib/omniauth.rb @@ -17,7 +17,7 @@ module OmniAuth autoload :FailureEndpoint, 'omniauth/failure_endpoint' def self.strategies - @@strategies ||= [] + @strategies ||= [] end class Configuration @@ -25,39 +25,29 @@ module OmniAuth def self.default_logger logger = Logger.new(STDOUT) - logger.progname = "omniauth" + logger.progname = 'omniauth' logger end - @@defaults = { - :camelizations => {}, - :path_prefix => '/auth', - :on_failure => OmniAuth::FailureEndpoint, - :failure_raise_out_environments => ['development'], - :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 => [:get, :post], - :mock_auth => { - :default => AuthHash.new( - 'provider' => 'default', - 'uid' => '1234', - 'info' => { - 'name' => 'Bob Example' - } - ) - } - } - def self.defaults - @@defaults + @defaults ||= { + :camelizations => {}, + :path_prefix => '/auth', + :on_failure => OmniAuth::FailureEndpoint, + :failure_raise_out_environments => ['development'], + :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 => [:get, :post], + :mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Bob Example'})} + } end def initialize - @@defaults.each_pair{|k,v| self.send("#{k}=",v)} + self.class.defaults.each_pair { |k, v| send("#{k}=", v) } end def on_failure(&block) @@ -92,7 +82,7 @@ module OmniAuth end end - def add_mock(provider, mock={}) + def add_mock(provider, mock = {}) # Stringify keys recursively one level. mock.keys.each do |key| mock[key.to_s] = mock.delete(key) @@ -106,11 +96,11 @@ module OmniAuth end # Merge with the default mock and ensure provider is correct. - mock = self.mock_auth[:default].dup.merge(mock) - mock["provider"] = provider.to_s + mock = mock_auth[:default].dup.merge(mock) + mock['provider'] = provider.to_s # Add it to the mocks. - self.mock_auth[provider.to_sym] = mock + mock_auth[provider.to_sym] = mock end # This is a convenience method to be used by strategy authors @@ -120,7 +110,7 @@ module OmniAuth # @param name [String] The underscored name, e.g. `oauth` # @param camelized [String] The properly camelized name, e.g. 'OAuth' def add_camelization(name, camelized) - self.camelizations[name.to_s] = camelized.to_s + camelizations[name.to_s] = camelized.to_s end attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase @@ -154,8 +144,8 @@ module OmniAuth target = hash.dup other_hash.keys.each do |key| - if other_hash[key].is_a? ::Hash and hash[key].is_a? ::Hash - target[key] = deep_merge(target[key],other_hash[key]) + if other_hash[key].is_a?(::Hash) && hash[key].is_a?(::Hash) + target[key] = deep_merge(target[key], other_hash[key]) next end @@ -169,7 +159,7 @@ module OmniAuth return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s] if first_letter_in_uppercase - word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } + word.to_s.gsub(/\/(.?)/) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase } else word.first + camelize(word)[1..-1] end diff --git a/lib/omniauth/auth_hash.rb b/lib/omniauth/auth_hash.rb index b28cb84..b2b05b3 100644 --- a/lib/omniauth/auth_hash.rb +++ b/lib/omniauth/auth_hash.rb @@ -6,7 +6,9 @@ module OmniAuth # is able to provide into the InfoHash (stored as the `'info'` # key). class AuthHash < Hashie::Mash - def self.subkey_class; Hashie::Mash end + def self.subkey_class + Hashie::Mash + end # Tells you if this is considered to be a valid # OmniAuth AuthHash. The requirements for that @@ -25,7 +27,9 @@ module OmniAuth end class InfoHash < Hashie::Mash - def self.subkey_class; Hashie::Mash end + def self.subkey_class + Hashie::Mash + end def name return self[:name] if self[:name] @@ -35,11 +39,10 @@ module OmniAuth nil end - def name?; !!name end - - def valid? - name? + def name? + !!name end + alias_method :valid?, :name? def to_hash hash = super diff --git a/lib/omniauth/failure_endpoint.rb b/lib/omniauth/failure_endpoint.rb index d9edbcc..66e9aee 100644 --- a/lib/omniauth/failure_endpoint.rb +++ b/lib/omniauth/failure_endpoint.rb @@ -22,22 +22,22 @@ module OmniAuth end def raise_out! - raise env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']) + fail(env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type'])) end 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}" - Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish + Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish end def strategy_name_query_param - return "" unless env['omniauth.error.strategy'] + return '' unless env['omniauth.error.strategy'] "&strategy=#{env['omniauth.error.strategy'].name}" end def origin_query_param - return "" unless env['omniauth.origin'] + return '' unless env['omniauth.origin'] "&origin=#{Rack::Utils.escape(env['omniauth.origin'])}" end end diff --git a/lib/omniauth/form.css b/lib/omniauth/form.css new file mode 100644 index 0000000..6c1f49e --- /dev/null +++ b/lib/omniauth/form.css @@ -0,0 +1,81 @@ +body { + background: #ccc; + font-family: "Lucida Grande", "Lucida Sans", Helvetica, Arial, sans-serif; +} + +h1 { + text-align: center; + margin: 30px auto 0px; + font-size: 18px; + padding: 10px 10px 15px; + background: #555; + color: white; + width: 320px; + border: 10px solid #444; + border-bottom: 0; + -moz-border-radius-topleft: 10px; + -moz-border-radius-topright: 10px; + -webkit-border-top-left-radius: 10px; + -webkit-border-top-right-radius: 10px; + border-top-left-radius: 10px; + border-top-right-radius: 10px; +} + +h1, form { + -moz-box-shadow: 2px 2px 7px rgba(0,0,0,0.3); + -webkit-box-shadow: 2px 2px 7px rgba(0,0,0,0.3); +} + +form { + background: white; + border: 10px solid #eee; + border-top: 0; + padding: 20px; + margin: 0px auto 40px; + width: 300px; + -moz-border-radius-bottomleft: 10px; + -moz-border-radius-bottomright: 10px; + -webkit-border-bottom-left-radius: 10px; + -webkit-border-bottom-right-radius: 10px; + border-bottom-left-radius: 10px; + border-bottom-right-radius: 10px; +} + +label { + display: block; + font-weight: bold; + margin-bottom: 5px; +} + +input { + font-size: 18px; + padding: 4px 8px; + display: block; + margin-bottom: 10px; + width: 280px; +} + +input#identifier, input#openid_url { + background: url(http://openid.net/login-bg.gif) no-repeat; + background-position: 0 50%; + padding-left: 18px; +} + +button { + font-size: 22px; + padding: 4px 8px; + display: block; + margin: 20px auto 0; +} + +fieldset { + border: 1px solid #ccc; + border-left: 0; + border-right: 0; + padding: 10px 0; +} + +fieldset input { + width: 260px; + font-size: 16px; +} diff --git a/lib/omniauth/form.rb b/lib/omniauth/form.rb index 4462e4b..dd1fda3 100644 --- a/lib/omniauth/form.rb +++ b/lib/omniauth/form.rb @@ -1,103 +1,21 @@ module OmniAuth - class Form - DEFAULT_CSS = <<-CSS - body { - background: #ccc; - font-family: "Lucida Grande", "Lucida Sans", Helvetica, Arial, sans-serif; - } - - h1 { - text-align: center; - margin: 30px auto 0px; - font-size: 18px; - padding: 10px 10px 15px; - background: #555; - color: white; - width: 320px; - border: 10px solid #444; - border-bottom: 0; - -moz-border-radius-topleft: 10px; - -moz-border-radius-topright: 10px; - -webkit-border-top-left-radius: 10px; - -webkit-border-top-right-radius: 10px; - border-top-left-radius: 10px; - border-top-right-radius: 10px; - } - - h1, form { - -moz-box-shadow: 2px 2px 7px rgba(0,0,0,0.3); - -webkit-box-shadow: 2px 2px 7px rgba(0,0,0,0.3); - } - - form { - background: white; - border: 10px solid #eee; - border-top: 0; - padding: 20px; - margin: 0px auto 40px; - width: 300px; - -moz-border-radius-bottomleft: 10px; - -moz-border-radius-bottomright: 10px; - -webkit-border-bottom-left-radius: 10px; - -webkit-border-bottom-right-radius: 10px; - border-bottom-left-radius: 10px; - border-bottom-right-radius: 10px; - } - - label { - display: block; - font-weight: bold; - margin-bottom: 5px; - } - - input { - font-size: 18px; - padding: 4px 8px; - display: block; - margin-bottom: 10px; - width: 280px; - } - - input#identifier, input#openid_url { - background: url(http://openid.net/login-bg.gif) no-repeat; - background-position: 0 50%; - padding-left: 18px; - } - - button { - font-size: 22px; - padding: 4px 8px; - display: block; - margin: 20px auto 0; - } - - fieldset { - border: 1px solid #ccc; - border-left: 0; - border-right: 0; - padding: 10px 0; - } - - fieldset input { - width: 260px; - font-size: 16px; - } - CSS + class Form # rubocop:disable ClassLength + DEFAULT_CSS = File.read(File.expand_path('../form.css', __FILE__)) attr_accessor :options def initialize(options = {}) - options[:title] ||= "Authentication Info Required" - options[:header_info] ||= "" + options[:title] ||= 'Authentication Info Required' + options[:header_info] ||= '' self.options = options - @html = "" + @html = '' @with_custom_button = false @footer = nil - header(options[:title],options[:header_info]) + header(options[:title], options[:header_info]) end - def self.build(options = {},&block) + def self.build(options = {}, &block) form = OmniAuth::Form.new(options) if block.arity > 0 yield form @@ -140,12 +58,12 @@ module OmniAuth def fieldset(legend, options = {}, &block) @html << "\n
" self end - def header(title,header_info) + def header(title, header_info) @html << <<-HTML @@ -181,10 +99,10 @@ module OmniAuth def to_response footer - Rack::Response.new(@html, 200, {"content-type" => "text/html"}).finish + Rack::Response.new(@html, 200, 'content-type' => 'text/html').finish end - protected + protected def css "\n" diff --git a/lib/omniauth/strategies/developer.rb b/lib/omniauth/strategies/developer.rb index bca3e45..908505b 100644 --- a/lib/omniauth/strategies/developer.rb +++ b/lib/omniauth/strategies/developer.rb @@ -35,11 +35,11 @@ module OmniAuth option :uid_field, :email def request_phase - form = OmniAuth::Form.new(:title => "User Info", :url => callback_path) + form = OmniAuth::Form.new(:title => 'User Info', :url => callback_path) options.fields.each do |field| - form.text_field field.to_s.capitalize.gsub("_", " "), field.to_s + form.text_field field.to_s.capitalize.gsub('_', ' '), field.to_s end - form.button "Sign In" + form.button 'Sign In' form.to_response end diff --git a/lib/omniauth/strategy.rb b/lib/omniauth/strategy.rb index cabdee6..9a847e8 100644 --- a/lib/omniauth/strategy.rb +++ b/lib/omniauth/strategy.rb @@ -84,7 +84,7 @@ module OmniAuth return end existing = superclass.respond_to?(:args) ? superclass.args : [] - return (instance_variable_defined?(:@args) && @args) || existing + (instance_variable_defined?(:@args) && @args) || existing end %w(uid info extra credentials).each do |fetcher| @@ -140,7 +140,7 @@ module OmniAuth end # Make sure that all of the args have been dealt with, otherwise error out. - raise ArgumentError, "Received wrong number of arguments. #{args.inspect}" unless args.empty? + fail(ArgumentError, "Received wrong number of arguments. #{args.inspect}") unless args.empty? yield options if block_given? end @@ -169,14 +169,16 @@ module OmniAuth # the request path is recognized. # # @param env [Hash] The Rack environment. - def call!(env) - raise OmniAuth::NoSessionError.new("You must provide a session to use OmniAuth.") unless env['rack.session'] + def call!(env) # rubocop:disable CyclomaticComplexity + unless env['rack.session'] + error = OmniAuth::NoSessionError.new('You must provide a session to use OmniAuth.') + fail(error) + end @env = env @env['omniauth.strategy'] = self if on_auth_path? return mock_call!(env) if OmniAuth.config.test_mode - return options_call if on_auth_path? && options_request? return request_call if on_request_path? && OmniAuth.config.allowed_request_methods.include?(request.request_method.downcase.to_sym) return callback_call if on_callback_path? @@ -186,27 +188,23 @@ module OmniAuth # Responds to an OPTIONS request. def options_call - OmniAuth.config.before_options_phase.call(self.env) if OmniAuth.config.before_options_phase - verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ') - return [ 200, { 'Allow' => verbs }, [] ] + OmniAuth.config.before_options_phase.call(env) if OmniAuth.config.before_options_phase + verbs = OmniAuth.config.allowed_request_methods.collect(&:to_s).collect(&:upcase).join(', ') + [200, {'Allow' => verbs}, []] end # Performs the steps necessary to run the request phase of a strategy. - def request_call + def request_call # rubocop:disable CyclomaticComplexity, MethodLength setup_phase - - log :info, "Request phase initiated." - - #store query params from the request url, extracted in the callback_phase + log :info, 'Request phase initiated.' + # store query params from the request url, extracted in the callback_phase session['omniauth.params'] = request.params - - OmniAuth.config.before_request_phase.call(self.env) if OmniAuth.config.before_request_phase - + OmniAuth.config.before_request_phase.call(env) if OmniAuth.config.before_request_phase if options.form.respond_to?(:call) - log :info, "Rendering form from supplied Rack endpoint." + log :info, 'Rendering form from supplied Rack endpoint.' options.form.call(env) elsif options.form - log :info, "Rendering form from underlying application." + log :info, 'Rendering form from underlying application.' call_app! else if request.params['origin'] @@ -221,7 +219,7 @@ module OmniAuth # Performs the steps necessary to run the callback phase of a strategy. def callback_call setup_phase - log :info, "Callback phase initiated." + log :info, 'Callback phase initiated.' @env['omniauth.origin'] = session.delete('omniauth.origin') @env['omniauth.origin'] = nil if env['omniauth.origin'] == '' @env['omniauth.params'] = session.delete('omniauth.params') || {} @@ -268,7 +266,7 @@ module OmniAuth setup_phase session['omniauth.params'] = request.params - OmniAuth.config.before_request_phase.call(self.env) if OmniAuth.config.before_request_phase + OmniAuth.config.before_request_phase.call(env) if OmniAuth.config.before_request_phase if request.params['origin'] @env['rack.session']['omniauth.origin'] = request.params['origin'] elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/) @@ -299,10 +297,10 @@ module OmniAuth # underlying application. This will default to `/auth/:provider/setup`. def setup_phase if options[:setup].respond_to?(:call) - log :info, "Setup endpoint detected, running now." + log :info, 'Setup endpoint detected, running now.' options[:setup].call(env) elsif options.setup? - log :info, "Calling through to underlying application for setup." + log :info, 'Calling through to underlying application for setup.' setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET') call_app!(setup_env) end @@ -312,7 +310,7 @@ module OmniAuth # perform any information gathering you need to be able to authenticate # the user in this phase. def request_phase - raise NotImplementedError + fail(NotImplementedError) end def uid @@ -360,7 +358,7 @@ module OmniAuth end def callback_phase - self.env['omniauth.auth'] = auth_hash + env['omniauth.auth'] = auth_hash call_app! end @@ -387,6 +385,7 @@ module OmniAuth path ||= current_path if options[:callback_path].respond_to?(:call) && options[:callback_path].call(env) path ||= custom_path(:request_path) path ||= "#{path_prefix}/#{name}/callback" + path end def setup_path @@ -394,11 +393,11 @@ module OmniAuth end def current_path - request.path_info.downcase.sub(/\/$/,'') + request.path_info.downcase.sub(/\/$/, '') end def query_string - request.query_string.empty? ? "" : "?#{request.query_string}" + request.query_string.empty? ? '' : "?#{request.query_string}" end def call_app!(env = @env) @@ -414,12 +413,13 @@ module OmniAuth else # in Rack 1.3.x, request.url explodes if scheme is nil if request.scheme && request.url.match(URI::ABS_URI) - uri = URI.parse(request.url.gsub(/\?.*$/,'')) + uri = URI.parse(request.url.gsub(/\?.*$/, '')) uri.path = '' - #sometimes the url is actually showing http inside rails because the other layers (like nginx) have handled the ssl termination. - uri.scheme = 'https' if ssl? + # sometimes the url is actually showing http inside rails because the + # other layers (like nginx) have handled the ssl termination. + uri.scheme = 'https' if ssl? # rubocop:disable BlockNesting uri.to_s - else "" + else '' end end end @@ -457,12 +457,14 @@ module OmniAuth r.finish end - def user_info; {} end + def user_info + {} + end def fail!(message_key, exception = nil) - self.env['omniauth.error'] = exception - self.env['omniauth.error.type'] = message_key.to_sym - self.env['omniauth.error.strategy'] = self + env['omniauth.error'] = exception + env['omniauth.error.type'] = message_key.to_sym + env['omniauth.error.strategy'] = self if exception log :error, "Authentication failure! #{message_key}: #{exception.class.to_s}, #{exception.message}" @@ -470,16 +472,20 @@ module OmniAuth log :error, "Authentication failure! #{message_key} encountered." end - OmniAuth.config.on_failure.call(self.env) + OmniAuth.config.on_failure.call(env) end class Options < Hashie::Mash; end - protected + protected def merge_stack(stack) - stack.inject({}){|c,h| c.merge!(h); c} + stack.inject({}) do |a, e| + a.merge!(e) + a + end end + def ssl? request.env['HTTPS'] == 'on' || request.env['HTTP_X_FORWARDED_SSL'] == 'on' || diff --git a/lib/omniauth/test.rb b/lib/omniauth/test.rb index bce2561..84c8148 100644 --- a/lib/omniauth/test.rb +++ b/lib/omniauth/test.rb @@ -1,12 +1,8 @@ module OmniAuth - # Support for testing OmniAuth strategies. module Test - autoload :PhonySession, 'omniauth/test/phony_session' autoload :StrategyMacros, 'omniauth/test/strategy_macros' autoload :StrategyTestCase, 'omniauth/test/strategy_test_case' - end - end diff --git a/lib/omniauth/test/phony_session.rb b/lib/omniauth/test/phony_session.rb index 768fa74..dce23a5 100644 --- a/lib/omniauth/test/phony_session.rb +++ b/lib/omniauth/test/phony_session.rb @@ -1,5 +1,8 @@ class OmniAuth::Test::PhonySession - def initialize(app); @app = app end + def initialize(app) + @app = app + end + def call(env) @session ||= (env['rack.session'] || {}) env['rack.session'] = @session diff --git a/lib/omniauth/test/strategy_macros.rb b/lib/omniauth/test/strategy_macros.rb index cd84883..5fa46b9 100644 --- a/lib/omniauth/test/strategy_macros.rb +++ b/lib/omniauth/test/strategy_macros.rb @@ -1,11 +1,8 @@ module OmniAuth - module Test - module StrategyMacros - def sets_an_auth_hash - it "sets an auth hash" do + it 'sets an auth hash' do expect(last_request.env['omniauth.auth']).to be_kind_of(Hash) end end @@ -28,7 +25,5 @@ module OmniAuth end end end - end - end diff --git a/lib/omniauth/test/strategy_test_case.rb b/lib/omniauth/test/strategy_test_case.rb index 9dc67e8..59933a8 100644 --- a/lib/omniauth/test/strategy_test_case.rb +++ b/lib/omniauth/test/strategy_test_case.rb @@ -2,9 +2,7 @@ require 'rack' require 'omniauth/test' module OmniAuth - module Test - # Support for testing OmniAuth strategies. # # @example Usage @@ -19,15 +17,14 @@ module OmniAuth # end # end module StrategyTestCase - def app - strat = self.strategy - resp = self.app_response - Rack::Builder.new { - use OmniAuth::Test::PhonySession - use *strat - run lambda {|env| [404, {'Content-Type' => 'text/plain'}, [resp || env.key?('omniauth.auth').to_s]] } - }.to_app + strat = strategy + resp = app_response + Rack::Builder.new do + use(OmniAuth::Test::PhonySession) + use(*strat) + run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [resp || env.key?('omniauth.auth').to_s]] } + end.to_app end def app_response @@ -39,11 +36,9 @@ module OmniAuth end def strategy - raise NotImplementedError.new('Including specs must define #strategy') + error = NotImplementedError.new('Including specs must define #strategy') + fail(error) end - end - end - end diff --git a/lib/omniauth/version.rb b/lib/omniauth/version.rb index f0f569b..35f08f7 100644 --- a/lib/omniauth/version.rb +++ b/lib/omniauth/version.rb @@ -1,3 +1,3 @@ module OmniAuth - VERSION = "1.1.4" unless defined?(OmniAuth::VERSION) + VERSION = '1.1.4' unless defined?(OmniAuth::VERSION) end diff --git a/omniauth.gemspec b/omniauth.gemspec index e83261e..24c2afb 100644 --- a/omniauth.gemspec +++ b/omniauth.gemspec @@ -12,15 +12,15 @@ Gem::Specification.new do |spec| spec.description = %q{A generalized Rack framework for multiple-provider authentication.} spec.email = ['michael@intridea.com', 'sferik@gmail.com'] spec.files = %w(.yardopts LICENSE.md README.md Rakefile omniauth.gemspec) - spec.files += Dir.glob("lib/**/*.rb") - spec.files += Dir.glob("spec/**/*") + spec.files += Dir.glob('lib/**/*.rb') + spec.files += Dir.glob('spec/**/*') spec.homepage = 'http://github.com/intridea/omniauth' spec.licenses = ['MIT'] spec.name = 'omniauth' spec.require_paths = ['lib'] spec.required_rubygems_version = '>= 1.3.5' - spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/ + spec.signing_key = File.expand_path('~/.gem/private_key.pem') if $PROGRAM_NAME =~ /gem\z/ spec.summary = spec.description - spec.test_files = Dir.glob("spec/**/*") + spec.test_files = Dir.glob('spec/**/*') spec.version = OmniAuth::VERSION end diff --git a/spec/helper.rb b/spec/helper.rb index 7b02da4..621512c 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -12,7 +12,7 @@ require 'rack/test' require 'omniauth' require 'omniauth/test' -OmniAuth.config.logger = Logger.new("/dev/null") +OmniAuth.config.logger = Logger.new('/dev/null') RSpec.configure do |config| config.include Rack::Test::Methods @@ -24,23 +24,29 @@ end class ExampleStrategy include OmniAuth::Strategy - option :name, 'test' - def call(env); self.call!(env) end attr_reader :last_env + option :name, 'test' + + def call(env) + self.call!(env) + end + def initialize(*args, &block) super @fail = nil end + def request_phase @fail = fail!(options[:failure]) if options[:failure] @last_env = env return @fail if @fail - raise "Request Phase" + fail('Request Phase') end + def callback_phase @fail = fail!(options[:failure]) if options[:failure] @last_env = env return @fail if @fail - raise "Callback Phase" + fail('Callback Phase') end end diff --git a/spec/omniauth/auth_hash_spec.rb b/spec/omniauth/auth_hash_spec.rb index b62ad0a..33f7da3 100644 --- a/spec/omniauth/auth_hash_spec.rb +++ b/spec/omniauth/auth_hash_spec.rb @@ -1,106 +1,109 @@ require 'helper' describe OmniAuth::AuthHash do - subject{ OmniAuth::AuthHash.new } - it "converts a supplied info key into an InfoHash object" do + subject { OmniAuth::AuthHash.new } + it 'converts a supplied info key into an InfoHash object' do subject.info = {:first_name => 'Awesome'} expect(subject.info).to be_kind_of(OmniAuth::AuthHash::InfoHash) expect(subject.info.first_name).to eq('Awesome') end - describe "#valid?" do - subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :info => {:name => 'Steven'}) } + describe '#valid?' do + subject { OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :info => {:name => 'Steven'}) } - it "is valid with the right parameters" do + it 'is valid with the right parameters' do expect(subject).to be_valid end - it "requires a uid" do + it 'requires a uid' do subject.uid = nil expect(subject).not_to be_valid end - it "requires a provider" do + it 'requires a provider' do subject.provider = nil expect(subject).not_to be_valid end - it "requires a name in the user info hash" do + it 'requires a name in the user info hash' do subject.info.name = nil expect(subject).not_to be_valid? end end - describe "#name" do - subject{ OmniAuth::AuthHash.new( - :info => { - :name => 'Phillip J. Fry', - :first_name => 'Phillip', - :last_name => 'Fry', - :nickname => 'meatbag', - :email => 'fry@planetexpress.com' - })} + describe '#name' do + subject do + OmniAuth::AuthHash.new( + :info => { + :name => 'Phillip J. Fry', + :first_name => 'Phillip', + :last_name => 'Fry', + :nickname => 'meatbag', + :email => 'fry@planetexpress.com', + } + ) + end - it "defaults to the name key" do + it 'defaults to the name key' do expect(subject.info.name).to eq('Phillip J. Fry') end - it "falls back to go to first_name last_name concatenation" do + it 'falls back to go to first_name last_name concatenation' do subject.info.name = nil expect(subject.info.name).to eq('Phillip Fry') end - it "displays only a first or last name if only that is available" do + it 'displays only a first or last name if only that is available' do subject.info.name = nil subject.info.first_name = nil expect(subject.info.name).to eq('Fry') end - it "displays the nickname if no name, first, or last is available" do + it 'displays the nickname if no name, first, or last is available' do subject.info.name = nil - %w(first_name last_name).each{|k| subject.info[k] = nil} + %w(first_name last_name).each { |k| subject.info[k] = nil } expect(subject.info.name).to eq('meatbag') end - it "displays the email if no name, first, last, or nick is available" do + it 'displays the email if no name, first, last, or nick is available' do subject.info.name = nil - %w(first_name last_name nickname).each{|k| subject.info[k] = nil} + %w(first_name last_name nickname).each { |k| subject.info[k] = nil } expect(subject.info.name).to eq('fry@planetexpress.com') end end - describe "#to_hash" do - subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'test', :name => 'Bob Example')} - let(:hash){ subject.to_hash } + describe '#to_hash' do + subject { OmniAuth::AuthHash.new(:uid => '123', :provider => 'test', :name => 'Bob Example') } + let(:hash) { subject.to_hash } - it "is a plain old hash" do + it 'is a plain old hash' do expect(hash.class).to eq(::Hash) end - it "has string keys" do + it 'has string keys' do expect(hash.keys).to be_include('uid') end - it "converts an info hash as well" do + it 'converts an info hash as well' do subject.info = {:first_name => 'Bob', :last_name => 'Example'} expect(subject.info.class).to eq(OmniAuth::AuthHash::InfoHash) expect(subject.to_hash['info'].class).to eq(::Hash) end - it "supplies the calculated name in the converted hash" do + it 'supplies the calculated name in the converted hash' do subject.info = {:first_name => 'Bob', :last_name => 'Examplar'} expect(hash['info']['name']).to eq('Bob Examplar') end it "does not pollute the URL hash with 'name' etc" do - subject.info = {'urls' => {'Homepage' => "http://homepage.com"}} - expect(subject.to_hash['info']['urls']).to eq({'Homepage' => "http://homepage.com"}) + subject.info = {'urls' => {'Homepage' => 'http://homepage.com'}} + expect(subject.to_hash['info']['urls']).to eq('Homepage' => 'http://homepage.com') end end describe OmniAuth::AuthHash::InfoHash do - describe "#valid?" do - it "is valid if there is a name" do + describe '#valid?' do + it 'is valid if there is a name' do expect(OmniAuth::AuthHash::InfoHash.new(:name => 'Awesome')).to be_valid end end diff --git a/spec/omniauth/builder_spec.rb b/spec/omniauth/builder_spec.rb index 0b28445..1477f6e 100644 --- a/spec/omniauth/builder_spec.rb +++ b/spec/omniauth/builder_spec.rb @@ -1,35 +1,35 @@ require 'helper' describe OmniAuth::Builder do - describe "#provider" do - it "translates a symbol to a constant" do + describe '#provider' do + it 'translates a symbol to a constant' do OmniAuth::Strategies.should_receive(:const_get).with('MyStrategy').and_return(Class.new) OmniAuth::Builder.new(nil) do provider :my_strategy end end - it "accepts a class" do + it 'accepts a class' do class ::ExampleClass; end - expect{ + expect do OmniAuth::Builder.new(nil) do provider ::ExampleClass end - }.not_to raise_error + end.not_to raise_error end it "raises a helpful LoadError message if it can't find the class" do - expect { + expect do OmniAuth::Builder.new(nil) do provider :lorax end - }.to raise_error(LoadError, "Could not find matching strategy for :lorax. You may need to install an additional gem (such as omniauth-lorax).") + end.to raise_error(LoadError, 'Could not find matching strategy for :lorax. You may need to install an additional gem (such as omniauth-lorax).') end end - describe "#options" do - it "merges provided options in" do + describe '#options' do + it 'merges provided options in' do k = Class.new b = OmniAuth::Builder.new(nil) b.should_receive(:use).with(k, :foo => 'bar', :baz => 'tik') @@ -38,7 +38,7 @@ describe OmniAuth::Builder do b.provider k, :baz => 'tik' end - it "adds an argument if no options are provided" do + it 'adds an argument if no options are provided' do k = Class.new b = OmniAuth::Builder.new(nil) b.should_receive(:use).with(k, :foo => 'bar') diff --git a/spec/omniauth/failure_endpoint_spec.rb b/spec/omniauth/failure_endpoint_spec.rb index e4f5cf4..1e7e032 100644 --- a/spec/omniauth/failure_endpoint_spec.rb +++ b/spec/omniauth/failure_endpoint_spec.rb @@ -1,9 +1,9 @@ require 'helper' describe OmniAuth::FailureEndpoint do - subject{ OmniAuth::FailureEndpoint } + subject { OmniAuth::FailureEndpoint } - context "raise-out environment" do + context 'raise-out environment' do before do @rack_env = ENV['RACK_ENV'] ENV['RACK_ENV'] = 'test' @@ -12,14 +12,14 @@ describe OmniAuth::FailureEndpoint do OmniAuth.config.failure_raise_out_environments = ['test'] end - it "raises out the error" do + it 'raises out the error' do expect do - subject.call('omniauth.error' => StandardError.new("Blah")) - end.to raise_error(StandardError, "Blah") + subject.call('omniauth.error' => StandardError.new('Blah')) + end.to raise_error(StandardError, 'Blah') end - it "raises out an OmniAuth::Error if no omniauth.error is set" do - expect{ subject.call('omniauth.error.type' => 'example') }.to raise_error(OmniAuth::Error, "example") + it 'raises out an OmniAuth::Error if no omniauth.error is set' do + expect { subject.call('omniauth.error.type' => 'example') }.to raise_error(OmniAuth::Error, 'example') end after do @@ -28,27 +28,28 @@ describe OmniAuth::FailureEndpoint do end end - context "non-raise-out environment" do - let(:env){ {'omniauth.error.type' => 'invalid_request', - 'omniauth.error.strategy' => ExampleStrategy.new({}) } } + context 'non-raise-out environment' do + let(:env) do + {'omniauth.error.type' => 'invalid_request', 'omniauth.error.strategy' => ExampleStrategy.new({})} + end - it "is a redirect" do + it 'is a redirect' do status, _, _ = *subject.call(env) expect(status).to eq(302) end - it "includes the SCRIPT_NAME" do + it 'includes the SCRIPT_NAME' do _, head, _ = *subject.call(env.merge('SCRIPT_NAME' => '/random')) expect(head['Location']).to eq('/random/auth/failure?message=invalid_request&strategy=test') end - it "respects the configured path prefix" do + it 'respects the configured path prefix' do allow(OmniAuth.config).to receive(:path_prefix).and_return('/boo') _, head, _ = *subject.call(env) - expect(head["Location"]).to eq('/boo/failure?message=invalid_request&strategy=test') + expect(head['Location']).to eq('/boo/failure?message=invalid_request&strategy=test') end - it "includes the origin (escaped) if one is provided" do + it 'includes the origin (escaped) if one is provided' do env.merge! 'omniauth.origin' => '/origin-example' _, head, _ = *subject.call(env) expect(head['Location']).to be_include('&origin=%2Forigin-example') diff --git a/spec/omniauth/form_spec.rb b/spec/omniauth/form_spec.rb index e4c7776..69b91d9 100644 --- a/spec/omniauth/form_spec.rb +++ b/spec/omniauth/form_spec.rb @@ -1,22 +1,22 @@ require 'helper' describe OmniAuth::Form do - describe ".build" do - it "yields the instance when called with a block and argument" do - OmniAuth::Form.build{|f| expect(f).to be_kind_of(OmniAuth::Form)} + describe '.build' do + it 'yields the instance when called with a block and argument' do + OmniAuth::Form.build { |f| expect(f).to be_kind_of(OmniAuth::Form) } end - it "evaluates in the instance when called with a block and no argument" do - OmniAuth::Form.build{|f| expect(f.class).to eq(OmniAuth::Form)} + it 'evaluates in the instance when called with a block and no argument' do + OmniAuth::Form.build { |f| expect(f.class).to eq(OmniAuth::Form) } end end - describe "#initialize" do - it "sets the form action to the passed :url option" do + describe '#initialize' do + it 'sets the form action to the passed :url option' do expect(OmniAuth::Form.new(:url => '/awesome').to_html).to be_include("action='/awesome'") end - it "sets an H1 tag from the passed :title option" do + it 'sets an H1 tag from the passed :title option' do expect(OmniAuth::Form.new(:title => 'Something Cool').to_html).to be_include('