Sha256: 93b98251f19ff0b03ebb1b12246a901c73ab0e94e55c52d84f8c83660f42ac4e

Contents?: true

Size: 1.33 KB

Versions: 16

Compression:

Stored size: 1.33 KB

Contents

module OmniAuth
  # This simple Rack endpoint that serves as the default
  # 'failure' mechanism for OmniAuth. If a strategy fails for
  # any reason this endpoint will be invoked. The default behavior
  # is to redirect to `/auth/failure` except in the case of
  # a development `RACK_ENV`, in which case an exception will
  # be raised.
  class FailureEndpoint
    attr_reader :env

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

    def initialize(env)
      @env = env
    end

    def call
      raise_out! if OmniAuth.config.failure_raise_out_environments.include?(ENV['RACK_ENV'].to_s)
      redirect_to_failure
    end

    def raise_out!
      raise(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
    end

    def strategy_name_query_param
      return '' unless env['omniauth.error.strategy']
      "&strategy=#{env['omniauth.error.strategy'].name}"
    end

    def origin_query_param
      return '' unless env['omniauth.origin']
      "&origin=#{Rack::Utils.escape(env['omniauth.origin'])}"
    end
  end
end

Version data entries

16 entries across 15 versions & 2 rubygems

Version Path
tdiary-5.0.8 vendor/bundle/gems/omniauth-1.8.1/lib/omniauth/failure_endpoint.rb
omniauth-1.8.1 lib/omniauth/failure_endpoint.rb
omniauth-1.8.0 lib/omniauth/failure_endpoint.rb
omniauth-1.4.3 lib/omniauth/failure_endpoint.rb
omniauth-1.7.1 lib/omniauth/failure_endpoint.rb
omniauth-1.7.0 lib/omniauth/failure_endpoint.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/omniauth-1.6.1/lib/omniauth/failure_endpoint.rb
tdiary-5.0.5 vendor/bundle/gems/omniauth-1.6.1/lib/omniauth/failure_endpoint.rb
tdiary-5.0.4 vendor/bundle/gems/omniauth-1.6.1/lib/omniauth/failure_endpoint.rb
omniauth-1.6.1 lib/omniauth/failure_endpoint.rb
omniauth-1.6.0 lib/omniauth/failure_endpoint.rb
omniauth-1.5.0 lib/omniauth/failure_endpoint.rb
omniauth-1.4.2 lib/omniauth/failure_endpoint.rb
omniauth-1.4.1 lib/omniauth/failure_endpoint.rb
omniauth-1.4.0 lib/omniauth/failure_endpoint.rb
omniauth-1.3.2 lib/omniauth/failure_endpoint.rb