Module: OmniAuth::Strategy

Included in:
OmniAuth::Strategies::Password
Defined in:
lib/omniauth/strategy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/omniauth/strategy.rb', line 6

def self.included(base)
  OmniAuth.strategies << base
  base.class_eval do
    attr_reader :app, :name, :env, :options, :response
  end
end

Instance Method Details

#auth_hashObject



142
143
144
145
146
147
# File 'lib/omniauth/strategy.rb', line 142

def auth_hash
  {
    'provider' => name.to_s,
    'uid' => nil
  }
end

#call(env) ⇒ Object



21
22
23
# File 'lib/omniauth/strategy.rb', line 21

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



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
56
57
58
# File 'lib/omniauth/strategy.rb', line 25

def call!(env)
  raise OmniAuth::NoSessionError.new("You must provide a session to use OmniAuth.") unless env['rack.session']

  @env = env
  @env['omniauth.strategy'] = self      
  
  return mock_call!(env) if OmniAuth.config.test_mode
  
  if current_path == request_path && OmniAuth.config.allowed_request_methods.include?(request.request_method.downcase.to_sym)
    setup_phase                  
    if response = call_through_to_app
      response
    else
      if request.params['origin']
        @env['rack.session']['omniauth.origin'] = request.params['origin']            
      elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
        @env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
      end
      request_phase
    end
  elsif current_path == callback_path
    setup_phase                  
    @env['omniauth.origin'] = session.delete('omniauth.origin')
    @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''

    callback_phase
  else
    if respond_to?(:other_phase)
      other_phase
    else
      @app.call(env)
    end
  end
end

#call_app!(env = @env) ⇒ Object



138
139
140
# File 'lib/omniauth/strategy.rb', line 138

def call_app!(env = @env)
  @app.call(env)
end

#call_through_to_appObject



131
132
133
134
135
136
# File 'lib/omniauth/strategy.rb', line 131

def call_through_to_app
  status, headers, body = *call_app!
  @response = Rack::Response.new(body, status, headers)
  
  status == 404 ? nil : @response.finish
end

#callback_pathObject



115
116
117
# File 'lib/omniauth/strategy.rb', line 115

def callback_path
  options[:callback_path] || "#{path_prefix}/#{name}/callback"
end

#callback_phaseObject



102
103
104
105
# File 'lib/omniauth/strategy.rb', line 102

def callback_phase
  @env['omniauth.auth'] = auth_hash
  call_app! 
end

#callback_urlObject



163
164
165
# File 'lib/omniauth/strategy.rb', line 163

def callback_url
  full_host + script_name + callback_path + query_string
end

#current_pathObject



123
124
125
# File 'lib/omniauth/strategy.rb', line 123

def current_path
  request.path_info.downcase.sub(/\/$/,'')
end

#fail!(message_key, exception = nil) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/omniauth/strategy.rb', line 194

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

  OmniAuth.config.on_failure.call(self.env)
end

#full_hostObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/omniauth/strategy.rb', line 149

def full_host
  case OmniAuth.config.full_host
    when String
      OmniAuth.config.full_host
    when Proc
      OmniAuth.config.full_host.call(env)
    else
      uri = URI.parse(request.url.gsub(/\?.*$/,''))
      uri.path = ''
      uri.query = nil
      uri.to_s
  end
end

#initialize(app, name, *args) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
# File 'lib/omniauth/strategy.rb', line 13

def initialize(app, name, *args, &block)
  @app = app
  @name = name.to_sym
  @options = args.last.is_a?(Hash) ? args.pop : {}
  
  yield self if block_given?
end

#mock_call!(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/omniauth/strategy.rb', line 60

def mock_call!(env)
  if current_path == request_path 
    setup_phase
    if response = call_through_to_app
      response
    else
      if request.params['origin']
        @env['rack.session']['omniauth.origin'] = request.params['origin']
      elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
        @env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
      end
      redirect(callback_path)
    end
  elsif current_path == callback_path
    setup_phase
    mocked_auth = OmniAuth.mock_auth_for(name.to_sym)
    if mocked_auth.is_a?(Symbol)
      fail!(mocked_auth)
    else
      @env['omniauth.auth'] = mocked_auth
      @env['omniauth.origin'] = session.delete('omniauth.origin')
      @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
      call_app!
    end
  else
    call_app!
  end
end

#path_prefixObject



107
108
109
# File 'lib/omniauth/strategy.rb', line 107

def path_prefix
  options[:path_prefix] || OmniAuth.config.path_prefix
end

#query_stringObject



127
128
129
# File 'lib/omniauth/strategy.rb', line 127

def query_string
  request.query_string.empty? ? "" : "?#{request.query_string}"
end

#redirect(uri) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/omniauth/strategy.rb', line 179

def redirect(uri)
  r = Rack::Response.new

  if options[:iframe]
    r.write("<script type='text/javascript' charset='utf-8'>top.location.href = '#{uri}';</script>")
  else
    r.write("Redirecting to #{uri}...")
    r.redirect(uri)
  end
  
  r.finish
end

#requestObject



175
176
177
# File 'lib/omniauth/strategy.rb', line 175

def request
  @request ||= Rack::Request.new(@env)
end

#request_pathObject



111
112
113
# File 'lib/omniauth/strategy.rb', line 111

def request_path
  options[:request_path] || "#{path_prefix}/#{name}"
end

#request_phaseObject

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/omniauth/strategy.rb', line 98

def request_phase
  raise NotImplementedError
end

#script_nameObject



167
168
169
# File 'lib/omniauth/strategy.rb', line 167

def script_name
  @env['SCRIPT_NAME'] || ''
end

#sessionObject



171
172
173
# File 'lib/omniauth/strategy.rb', line 171

def session
  @env['rack.session']
end

#setup_pathObject



119
120
121
# File 'lib/omniauth/strategy.rb', line 119

def setup_path
  options[:setup_path] || "#{path_prefix}/#{name}/setup"
end

#setup_phaseObject



89
90
91
92
93
94
95
96
# File 'lib/omniauth/strategy.rb', line 89

def setup_phase
  if options[:setup].respond_to?(:call)
    options[:setup].call(env) 
  elsif options[:setup]
    setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
    call_app!(setup_env)
  end
end

#user_infoObject



192
# File 'lib/omniauth/strategy.rb', line 192

def ; {} end