Class: GoogleClientController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/google_client_controller.rb

Constant Summary collapse

DEFAULT_PARAMS =

parameters required in both OAuth steps

{
  :client_id => Rails.application.config.google_client.client_id
}

Instance Method Summary collapse

Instance Method Details

#indexObject

OAuth step1: redirect to Google endpoint with the application idenfitifer and the redirect uri



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/google_client_controller.rb', line 10

def index
  _params = {
    :redirect_uri    => Rails.application.config.google_client.redirect_uri,
    :response_type   => "code",
    :scope           => Rails.application.config.google_client.scope,
    :access_type     => Rails.application.config.google_client.access_type.nil? ? "online" : Rails.application.config.google_client.access_type,
    #:approval_prompt => Rails.application.config.google_client.approval_prompt.nil? ? "force" : Rails.application.config.google_client.approval_prompt
  }
  _params.merge!(DEFAULT_PARAMS)
  
  redirect_to connection.create_uri("/o/oauth2/auth", _params).to_s
end

#oauth2callbackObject

OAuth step2: retrieve the code from Google, ask for a valid access token and forward to the user defined uri



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
59
60
61
62
63
64
65
# File 'app/controllers/google_client_controller.rb', line 25

def oauth2callback
  # This code is retrieved from Google

  _params = {
    :redirect_uri  => Rails.application.config.google_client.redirect_uri,
    :client_secret => Rails.application.config.google_client.client_secret,
    :grant_type    => "authorization_code",
    :code          => params[:code]
  }

  _params.merge!(DEFAULT_PARAMS)

  response = connection.post "/o/oauth2/token", _params
  
  if response.code.to_s.eql?("200")
    response = ActiveSupport::JSON.decode(response.body)

    if Rails.application.config.google_client.forward_action.nil? or !Rails.application.config.google_client.forward_action.is_a?(String)
      raise GoogleClient::Error.new("Invalid forward_action value")
    end

    url = Rails.application.config.google_client.forward_action.split("#")

    url.length == 2 or raise GoogleClient::Error.new("Invalid forward_action value")

    @data = response

    redirect_to ({
      :controller => url.first,
      :action => url.last,
      :access_token => response["access_token"],
      :token_type => response["token_type"],
      :expires_in => response["expires_in"],
      :refresh_token => response["refresh_token"]
    })

  else
    logger.error "Error #{response.code} while accessing to Google #{response.body}"
    raise RuntimeError, "Unable to access to Google"
  end
end

#showObject



67
68
69
70
71
72
# File 'app/controllers/google_client_controller.rb', line 67

def show
  @data = {:access_token => params[:access_token], 
            :token_type => params[:token_type],
            :expires_in => params[:expires_in],
            :refresh_token => params[:refresh_token]}
end