Class: SimpleGoogleAuth::Receiver

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



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

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"])

  data = AuthDataPresenter.new(auth_data)
  raise Error, "Authentication failed" unless config.authenticate.call(data)

  request.session[config.data_session_key_name] = auth_data

  path = config.authentication_uri_state_path_extractor.call(request.session[config.state_session_key_name])
  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