2013-05-29 19:31:44 +00:00
|
|
|
# Copyright 2013 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'signet/oauth_2/client'
|
|
|
|
|
|
|
|
module Google
|
|
|
|
class APIClient
|
|
|
|
##
|
|
|
|
# Represents cached OAuth 2 tokens stored on local disk in a
|
|
|
|
# JSON serialized file. Meant to resemble the serialized format
|
|
|
|
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
|
|
|
|
#
|
2013-11-14 14:50:06 +00:00
|
|
|
class Storage
|
2013-11-14 13:45:36 +00:00
|
|
|
|
|
|
|
AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth'
|
|
|
|
TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
2013-11-14 14:50:06 +00:00
|
|
|
# @return [Object] Storage object.
|
|
|
|
attr_accessor :store
|
2013-05-29 19:31:44 +00:00
|
|
|
|
2013-11-14 14:50:06 +00:00
|
|
|
# @return [Signet::OAuth2::Client]
|
2013-05-29 19:31:44 +00:00
|
|
|
attr_reader :authorization
|
|
|
|
|
|
|
|
##
|
2013-11-14 14:50:06 +00:00
|
|
|
# Initializes the Storage object.
|
2013-05-29 19:31:44 +00:00
|
|
|
#
|
2013-11-14 14:50:06 +00:00
|
|
|
# @params [Object] Storage object
|
|
|
|
def initialize(store)
|
|
|
|
@store= store
|
2015-03-25 23:10:00 +00:00
|
|
|
@authorization = nil
|
2013-05-29 19:31:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2013-11-14 16:40:54 +00:00
|
|
|
# Write the credentials to the specified store.
|
|
|
|
#
|
|
|
|
# @params [Signet::OAuth2::Client] authorization
|
|
|
|
# Optional authorization instance. If not provided, the authorization
|
|
|
|
# already associated with this instance will be written.
|
|
|
|
def write_credentials(authorization=nil)
|
|
|
|
@authorization = authorization if authorization
|
2013-11-28 07:14:41 +00:00
|
|
|
if @authorization.respond_to?(:refresh_token) && @authorization.refresh_token
|
2013-11-14 16:40:54 +00:00
|
|
|
store.write_credentials(credentials_hash)
|
|
|
|
end
|
2013-11-14 07:17:08 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 08:20:37 +00:00
|
|
|
##
|
|
|
|
# Loads credentials and authorizes an client.
|
|
|
|
# @return [Object] Signet::OAuth2::Client or NIL
|
2013-11-14 13:45:36 +00:00
|
|
|
def authorize
|
2013-12-03 07:30:28 +00:00
|
|
|
@authorization = nil
|
2013-11-18 10:39:02 +00:00
|
|
|
cached_credentials = load_credentials
|
|
|
|
if cached_credentials && cached_credentials.size > 0
|
2013-11-14 13:45:36 +00:00
|
|
|
@authorization = Signet::OAuth2::Client.new(cached_credentials)
|
|
|
|
@authorization.issued_at = Time.at(cached_credentials['issued_at'].to_i)
|
|
|
|
self.refresh_authorization if @authorization.expired?
|
|
|
|
end
|
2013-11-18 10:39:02 +00:00
|
|
|
return @authorization
|
2013-11-14 07:31:21 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 07:17:08 +00:00
|
|
|
##
|
2013-11-14 14:50:06 +00:00
|
|
|
# refresh credentials and save them to store
|
2013-11-14 07:17:08 +00:00
|
|
|
def refresh_authorization
|
2013-11-18 10:39:02 +00:00
|
|
|
authorization.refresh!
|
2013-11-14 14:50:06 +00:00
|
|
|
self.write_credentials
|
2013-05-29 19:31:44 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 16:40:54 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
##
|
|
|
|
# Attempt to read in credentials from the specified store.
|
|
|
|
def load_credentials
|
|
|
|
store.load_credentials
|
|
|
|
end
|
|
|
|
|
2013-11-14 14:50:06 +00:00
|
|
|
##
|
|
|
|
# @return [Hash] with credentials
|
2013-11-14 13:45:36 +00:00
|
|
|
def credentials_hash
|
|
|
|
{
|
2013-11-18 10:39:02 +00:00
|
|
|
:access_token => authorization.access_token,
|
2013-11-14 13:45:36 +00:00
|
|
|
:authorization_uri => AUTHORIZATION_URI,
|
2013-11-18 10:39:02 +00:00
|
|
|
:client_id => authorization.client_id,
|
|
|
|
:client_secret => authorization.client_secret,
|
|
|
|
:expires_in => authorization.expires_in,
|
|
|
|
:refresh_token => authorization.refresh_token,
|
2013-11-14 13:45:36 +00:00
|
|
|
:token_credential_uri => TOKEN_CREDENTIAL_URI,
|
2013-11-18 10:39:02 +00:00
|
|
|
:issued_at => authorization.issued_at.to_i
|
2013-11-14 13:45:36 +00:00
|
|
|
}
|
|
|
|
end
|
2013-05-29 19:31:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|