Class: SimpleGoogleAuth::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_google_auth/receiver.rb

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_google_auth/receiver.rb', line 5

def call(env)
  request = Rack::Request.new(env)
  config = SimpleGoogleAuth.config
  ensure_params_are_correct(request, config)

  api = SimpleGoogleAuth::OAuth.new(config)
  auth_data = api.exchange_code_for_auth_token!(request.params["code"])

  raise Error, "Authentication failed" unless config.authenticate.call(auth_data)

  request.session[config.data_session_key_name] = auth_data

  path = request.session[config.state_session_key_name][32..-1]
  path = "/" if path.blank?
  [302, {"Location" => path}, [" "]]

rescue Error => e
  uri = URI(config.)
  uri.query = uri.query ? "#{uri.query}&" : ""
  uri.query += "message=#{CGI.escape e.message}"
  [302, {"Location" => uri.to_s}, [" "]]
end