File: failure_endpoint.rb

package info (click to toggle)
ruby-omniauth 2.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 364 kB
  • sloc: ruby: 2,447; makefile: 7
file content (55 lines) | stat: -rw-r--r-- 1,567 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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']}#{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']

      "&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