Class: GoogleApi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/google_api/session/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_name, scope, name_api, version_api) ⇒ Session

Returns a new instance of Session.



8
9
10
11
12
13
14
15
16
# File 'lib/google_api/session/session.rb', line 8

def initialize(config_name, scope, name_api, version_api)
  @config_name = config_name
  @scope       = scope
  @name_api    = name_api
  @version_api = version_api

  @client = nil
  @api    = nil
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/google_api/session/session.rb', line 6

def scope
  @scope
end

Instance Method Details

#apiObject



177
178
179
180
181
# File 'lib/google_api/session/session.rb', line 177

def api
  check_session!

  @api
end

#check_sessionObject

Check session



152
153
154
155
156
157
158
# File 'lib/google_api/session/session.rb', line 152

def check_session
  if @api.nil? || @client.nil?
    return false
  end

  return true
end

#check_session!Object

Check session with error



161
162
163
164
165
# File 'lib/google_api/session/session.rb', line 161

def check_session!
  if @api.nil? || @client.nil?
    raise GoogleApi::SessionError, "You are not log in."
  end   
end

#clientObject



167
168
169
170
171
172
173
174
175
# File 'lib/google_api/session/session.rb', line 167

def client
  check_session!

  if @client.authorization.refresh_token && @client.authorization.expired?
    @client.authorization.fetch_access_token!
  end

  @client
end

#login(code = nil) ⇒ Object

Classic oauth 2 login

login() -> return autorization url
login(code) -> try login, return true false


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/google_api/session/session.rb', line 63

def (code = nil)
  @client = Google::APIClient.new
  @client.authorization.client_id     = c('client_id')
  @client.authorization.client_secret = c('client_secret')
  @client.authorization.scope         = @scope
  @client.authorization.redirect_uri  = c('redirect_uri')

  @api = @client.discovered_api(@name_api, @version_api)

  unless code
    return @client.authorization.authorization_uri.to_s
  end

  begin
    @client.authorization.code = code
    @client.authorization.fetch_access_token!
  rescue
    return false
  end

  return true
end

#login_by_certObject

Login using cert file (Service account)

Required: client_cert_file, client_developer_email, key_secret

Success: return true
Failure: return false


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/google_api/session/session.rb', line 25

def 
  @client = Google::APIClient.new

  key      = Google::APIClient::PKCS12.load_key(c('client_cert_file'), c('key_secret'))
  asserter = Google::APIClient::JWTAsserter.new(c('client_developer_email'), @scope, key)

  begin
    @client.authorization = asserter.authorize()
    @api = @client.discovered_api(@name_api, @version_api)
  rescue
    return false
  end

  return true
end

#login_by_cert!Object

In loop call login_by_cert

return true after successful login loop can be stopped with ctr+c - return false



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/google_api/session/session.rb', line 46

def 
  trap("INT"){ return false }

  i = 0
  until 
    progress_log(i+=1)
    sleep(1)
  end

  return true
end

#login_by_line(server = 'http://localhost/oauth2callback', port = 0) ⇒ Object

Automaticaly open autorization url a waiting for callback. Launchy gem is required

Parameters:

server:: server will be on this addres, its alson address for oatuh 2 callback
port:: listening port for server
port=0:: server will be on first free port

Steps: 1) create server 2) launch browser and redirect to google api 3) confirm and google api redirect to localhost 4) server get code and start session 5) close server - you are login



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/google_api/session/session.rb', line 101

def (server = 'http://localhost/oauth2callback', port = 0)
  begin
    require "launchy" # open browser
  rescue
    raise GoogleApi::RequireError, "You don't have launchy gem. Firt install it: gem install launchy."
  end
  
  require "socket"  # make tcp server 
  require "uri"     # parse uri

  uri = URI(server)

  # Start webserver.
  webserver = TCPServer.new(uri.host, port)

  # By default port is 0. It means that TCPServer will get first free port.
  # Port is required for redirect_uri.
  uri.port = webserver.addr[1]

  # Add redirect_uri for google oauth 2 callback.
  _config.send(@config_name).redirect_uri = uri.to_s

  # Open browser.
  Launchy.open()

  # Wait for new session.
  session = webserver.accept

  # Parse header for query.
  request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
  request = Hash[URI.decode_www_form(URI(request).query)]

  # Failure login
  to_return = false
  message   = "You have not been logged. Please try again."

  if (request['code'])
    message = "You have been successfully logged. Now you can close the browser."

    to_return = true
  end

  session.write(message)

  # Close session and webserver.
  session.close

  return to_return
end