From 2d326459b2c05621a4c51ea50c59b209955f457a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 07:59:08 +0100 Subject: [PATCH 01/33] ignore IntelliJ files ignore logfiles --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 67599524b..fb4875a9a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,10 @@ pkg specdoc wiki .google-api.yaml +*.log + +#IntelliJ +.idea +*.iml +atlassian* + From 5538bded1fe7c577df6ecc0dd05cd0424b8dc56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 08:17:08 +0100 Subject: [PATCH 02/33] refactoring load_credentials adding refresh_authorization write_credentials now using the authorizations methods not the instance variables, Using the instance variables can cause problems. --- lib/google/api_client/auth/file_storage.rb | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index 049ef965a..24cdeed9a 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -41,18 +41,22 @@ module Google ## # Attempt to read in credentials from the specified file. + # TODO: add methods to: + # check if file exists + # check if file is writeable + # check json structure is valid def load_credentials - if File.exist? self.path - File.open(self.path, 'r') do |file| - cached_credentials = JSON.load(file) - @authorization = Signet::OAuth2::Client.new(cached_credentials) - @authorization.issued_at = Time.at(cached_credentials['issued_at']) - if @authorization.expired? - @authorization.fetch_access_token! - self.write_credentials - end - end - end + cached_credentials = JSON.load(self.path) + @authorization = Signet::OAuth2::Client.new(cached_credentials) + @authorization.issued_at = Time.at(cached_credentials['issued_at']) + self.refresh_authorization if @authorization.expired? + end + + ## + # refresh credentials and save them to file + def refresh_authorization + @authorization.refresh! + self.write_credentials(@authorization) end ## @@ -62,23 +66,21 @@ module Google # Optional authorization instance. If not provided, the authorization # already associated with this instance will be written. def write_credentials(authorization=nil) - @authorization = authorization unless authorization.nil? + @authorization = authorization if authorization + if @authorization.refresh_token - unless @authorization.refresh_token.nil? - hash = {} - %w'access_token - authorization_uri - client_id - client_secret - expires_in - refresh_token - token_credential_uri'.each do |var| - hash[var] = @authorization.instance_variable_get("@#{var}") - end - hash['issued_at'] = @authorization.issued_at.to_i + credentials_hash = { + 'access_token' => @authorization.access_token, + 'authorization_uri' => @authorization.authorization_uri, + 'client_id' => @authorization.client_id, + 'client_secret' => @authorization.client_secret, + 'expires_in' => @authorization.expires_in, + 'refresh_token' => @authorization.refresh_token, + 'token_credential_uri' => @authorization.token_credential_uri + } File.open(self.path, 'w', 0600) do |file| - file.write(hash.to_json) + file.write(credentials_hash.to_json) end end end From 146ccad83a93fa103f30a8e8c3afc89b08869b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 08:19:21 +0100 Subject: [PATCH 03/33] using symbols as hash keys --- lib/google/api_client/auth/file_storage.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index 24cdeed9a..716f109d3 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -70,13 +70,13 @@ module Google if @authorization.refresh_token credentials_hash = { - 'access_token' => @authorization.access_token, - 'authorization_uri' => @authorization.authorization_uri, - 'client_id' => @authorization.client_id, - 'client_secret' => @authorization.client_secret, - 'expires_in' => @authorization.expires_in, - 'refresh_token' => @authorization.refresh_token, - 'token_credential_uri' => @authorization.token_credential_uri + :access_token => @authorization.access_token, + :authorization_uri => @authorization.authorization_uri, + :client_id => @authorization.client_id, + :client_secret => @authorization.client_secret, + :expires_in => @authorization.expires_in, + :refresh_token => @authorization.refresh_token, + :token_credential_uri => @authorization.token_credential_uri } File.open(self.path, 'w', 0600) do |file| From 10a337b807953521ce3c1b389ebc21be7eeef47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 08:31:21 +0100 Subject: [PATCH 04/33] adds method path= to proof if file can be accessed --- lib/google/api_client/auth/file_storage.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index 716f109d3..d19927ab6 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -24,7 +24,7 @@ module Google # class FileStorage # @return [String] Path to the credentials file. - attr_accessor :path + attr_reader :path # @return [Signet::OAuth2::Client] Path to the credentials file. attr_reader :authorization @@ -41,17 +41,18 @@ module Google ## # Attempt to read in credentials from the specified file. - # TODO: add methods to: - # check if file exists - # check if file is writeable - # check json structure is valid def load_credentials - cached_credentials = JSON.load(self.path) + cached_credentials = JSON.load(path) @authorization = Signet::OAuth2::Client.new(cached_credentials) @authorization.issued_at = Time.at(cached_credentials['issued_at']) self.refresh_authorization if @authorization.expired? end + def path=(value) + @path=value + raise "Credentials on path #{path} not accessible" unless File.exists?(@path) && File.readable?(@path) && File.writable?(@path) + end + ## # refresh credentials and save them to file def refresh_authorization From bbb24a5d2eabafe22f8ff138762fb89dff480962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 08:32:47 +0100 Subject: [PATCH 05/33] using the path= method on initialize --- lib/google/api_client/auth/file_storage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index d19927ab6..b08615b18 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -35,7 +35,7 @@ module Google # @param [String] path # Path to the credentials file. def initialize(path) - @path = path + self.path= path self.load_credentials end From bd7cf179f34f2dad2e537369cc4e97d24f145ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 14:45:36 +0100 Subject: [PATCH 06/33] adds Constants for Authorization_uri, Token_credentials_uri extracts authorize extracts credentials_hash --- lib/google/api_client/auth/file_storage.rb | 54 +++++++++++++--------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index b08615b18..880259dd1 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -23,8 +23,12 @@ module Google # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html # class FileStorage + + AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth' + TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token' + # @return [String] Path to the credentials file. - attr_reader :path + attr_accessor :path # @return [Signet::OAuth2::Client] Path to the credentials file. attr_reader :authorization @@ -35,22 +39,26 @@ module Google # @param [String] path # Path to the credentials file. def initialize(path) - self.path= path - self.load_credentials + @path= path + self.authorize end ## # Attempt to read in credentials from the specified file. def load_credentials - cached_credentials = JSON.load(path) - @authorization = Signet::OAuth2::Client.new(cached_credentials) - @authorization.issued_at = Time.at(cached_credentials['issued_at']) - self.refresh_authorization if @authorization.expired? + if File.exists?(@path) && File.readable?(@path) && File.writable?(@path) + credentials = File.open(path, 'r') { |f| JSON.parse(f.read) } + end + credentials end - def path=(value) - @path=value - raise "Credentials on path #{path} not accessible" unless File.exists?(@path) && File.readable?(@path) && File.writable?(@path) + def authorize + if load_credentials + cached_credentials = load_credentials + @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 end ## @@ -60,6 +68,19 @@ module Google self.write_credentials(@authorization) end + def credentials_hash + { + :access_token => @authorization.access_token, + :authorization_uri => AUTHORIZATION_URI, + :client_id => @authorization.client_id, + :client_secret => @authorization.client_secret, + :expires_in => @authorization.expires_in, + :refresh_token => @authorization.refresh_token, + :token_credential_uri => TOKEN_CREDENTIAL_URI, + :issued_at => @authorization.issued_at.to_i + } + end + ## # Write the credentials to the specified file. # @@ -69,18 +90,7 @@ module Google def write_credentials(authorization=nil) @authorization = authorization if authorization if @authorization.refresh_token - - credentials_hash = { - :access_token => @authorization.access_token, - :authorization_uri => @authorization.authorization_uri, - :client_id => @authorization.client_id, - :client_secret => @authorization.client_secret, - :expires_in => @authorization.expires_in, - :refresh_token => @authorization.refresh_token, - :token_credential_uri => @authorization.token_credential_uri - } - - File.open(self.path, 'w', 0600) do |file| + File.open(self.path, 'w') do |file| file.write(credentials_hash.to_json) end end From c2135d9dcf160367e3e4c878cf14ec0d2cdc2695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 15:50:06 +0100 Subject: [PATCH 07/33] split storages in own adapter classes, added redis adapter --- google-api-client.gemspec | 2 +- .../auth/{file_storage.rb => storage.rb} | 40 ++++++------- .../api_client/auth/storages/file_store.rb | 59 +++++++++++++++++++ .../api_client/auth/storages/redis_store.rb | 51 ++++++++++++++++ 4 files changed, 129 insertions(+), 23 deletions(-) rename lib/google/api_client/auth/{file_storage.rb => storage.rb} (73%) create mode 100644 lib/google/api_client/auth/storages/file_store.rb create mode 100644 lib/google/api_client/auth/storages/redis_store.rb diff --git a/google-api-client.gemspec b/google-api-client.gemspec index 6b91979a9..00489b92a 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.email = "sbazyl@google.com" s.executables = ["google-api"] s.extra_rdoc_files = ["README.md"] - s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/file_storage.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] + s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/auth/storages/*.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] s.homepage = "http://code.google.com/p/google-api-ruby-client/" s.licenses = ["Apache 2.0"] s.rdoc_options = ["--main", "README.md"] diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/storage.rb similarity index 73% rename from lib/google/api_client/auth/file_storage.rb rename to lib/google/api_client/auth/storage.rb index 880259dd1..2fa18eab8 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require 'json' require 'signet/oauth_2/client' module Google @@ -22,34 +21,31 @@ module Google # JSON serialized file. Meant to resemble the serialized format # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html # - class FileStorage + class Storage AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth' TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token' - # @return [String] Path to the credentials file. - attr_accessor :path + # @return [Object] Storage object. + attr_accessor :store - # @return [Signet::OAuth2::Client] Path to the credentials file. + + # @return [Signet::OAuth2::Client] attr_reader :authorization ## - # Initializes the FileStorage object. + # Initializes the Storage object. # - # @param [String] path - # Path to the credentials file. - def initialize(path) - @path= path + # @params [Object] Storage object + def initialize(store) + @store= store self.authorize end ## - # Attempt to read in credentials from the specified file. + # Attempt to read in credentials from the specified store. def load_credentials - if File.exists?(@path) && File.readable?(@path) && File.writable?(@path) - credentials = File.open(path, 'r') { |f| JSON.parse(f.read) } - end - credentials + store.load_credentials end def authorize @@ -62,12 +58,14 @@ module Google end ## - # refresh credentials and save them to file + # refresh credentials and save them to store def refresh_authorization @authorization.refresh! - self.write_credentials(@authorization) + self.write_credentials end + ## + # @return [Hash] with credentials def credentials_hash { :access_token => @authorization.access_token, @@ -82,17 +80,15 @@ module Google end ## - # Write the credentials to the specified file. + # Write the credentials to the specified store. # - # @param [Signet::OAuth2::Client] authorization + # @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 if @authorization.refresh_token - File.open(self.path, 'w') do |file| - file.write(credentials_hash.to_json) - end + store.write_credentials(credentials_hash) end end end diff --git a/lib/google/api_client/auth/storages/file_store.rb b/lib/google/api_client/auth/storages/file_store.rb new file mode 100644 index 000000000..1b8ad4a36 --- /dev/null +++ b/lib/google/api_client/auth/storages/file_store.rb @@ -0,0 +1,59 @@ +# 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 'json' + +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 + # + class FileStore + + attr_accessor :path + + ## + # Initializes the FileStorage object. + # + # @param [String] path + # Path to the credentials file. + def initialize(path) + @path= path + end + + ## + # Attempt to read in credentials from the specified file. + def load_credentials + if File.exists?(path) && File.readable?(path) && File.writable?(path) + credentials = File.open(path, 'r') { |f| JSON.parse(f.read) } + end + credentials + end + + ## + # Write the credentials to the specified file. + # + # @param [Signet::OAuth2::Client] authorization + # Optional authorization instance. If not provided, the authorization + # already associated with this instance will be written. + def write_credentials(credentials_hash) + File.open(self.path, 'w') do |file| + file.write(credentials_hash.to_json) + end + end + end + end +end diff --git a/lib/google/api_client/auth/storages/redis_store.rb b/lib/google/api_client/auth/storages/redis_store.rb new file mode 100644 index 000000000..715ab5dbd --- /dev/null +++ b/lib/google/api_client/auth/storages/redis_store.rb @@ -0,0 +1,51 @@ +# 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 'json' + +module Google + class APIClient + class RedisStore + + attr_accessor :redis + + ## + # Initializes the RedisStore object. + # + # @params [Object] Redis instance + def initialize(redis) + @redis= redis + end + + ## + # Attempt to read in credentials from redis. + def load_credentials + credentials = redis.get redis_credentials_key + JSON.parse(credentials) if credentials + end + + def redis_credentials_key + "google_api_credentials" + end + + ## + # Write the credentials to redis. + # + # @params [Hash] credentials + def write_credentials(credentials_hash) + redis.set(redis_credentials_key, credentials_hash.to_json) + end + end + end +end From 2a87a93d489d01f182c1be1e59fe7fe44740d9f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 15:58:46 +0100 Subject: [PATCH 08/33] repair gemspec --- .../api_client/auth/file_storage_spec.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spec/google/api_client/auth/file_storage_spec.rb diff --git a/spec/google/api_client/auth/file_storage_spec.rb b/spec/google/api_client/auth/file_storage_spec.rb new file mode 100644 index 000000000..d3bcc3b70 --- /dev/null +++ b/spec/google/api_client/auth/file_storage_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' +require_relative '../../../../lib/google/api_client' +require_relative '../../../../lib/google/api_client/auth/storage' + +describe Google::APIClient::FileStorage do + let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } + let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } + let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'credentials.json')) } + + #file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) + #if file_storage.authorization.nil? + # client_secrets = Google::APIClient::ClientSecrets.load + it 'should initialize' do + Google::APIClient::FileStorage.any_instance.should_receive(:load_credentials) + file_storage = Google::APIClient::FileStorage.new(json_file) + file_storage.path.to_s.should == json_file + end + + it 'should load_credentials' do + Google::APIClient::FileStorage.any_instance.should_receive(:load_credentials) + + file_storage = Google::APIClient::FileStorage.new(json_file) + file_storage.path.to_s.should == json_file + end + +end From 60ce5bb082ea3561d8dec975ac1fc1b9cc55891f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 16:04:56 +0100 Subject: [PATCH 09/33] repair gemspec --- google-api-client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-api-client.gemspec b/google-api-client.gemspec index 00489b92a..01572d0b6 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.email = "sbazyl@google.com" s.executables = ["google-api"] s.extra_rdoc_files = ["README.md"] - s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/auth/storages/*.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] + s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/auth/storages/file_store.rb", "lib/google/api_client/auth/storages/redis_store.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] s.homepage = "http://code.google.com/p/google-api-ruby-client/" s.licenses = ["Apache 2.0"] s.rdoc_options = ["--main", "README.md"] From 9ddc8b85dc89738af7d6b6eda0794d107d2d8d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 17:40:54 +0100 Subject: [PATCH 10/33] make methods private --- lib/google/api_client/auth/storage.rb | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/google/api_client/auth/storage.rb b/lib/google/api_client/auth/storage.rb index 2fa18eab8..1fc080eb5 100644 --- a/lib/google/api_client/auth/storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -29,7 +29,6 @@ module Google # @return [Object] Storage object. attr_accessor :store - # @return [Signet::OAuth2::Client] attr_reader :authorization @@ -43,9 +42,16 @@ module Google end ## - # Attempt to read in credentials from the specified store. - def load_credentials - store.load_credentials + # 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 + if @authorization.refresh_token + store.write_credentials(credentials_hash) + end end def authorize @@ -64,6 +70,14 @@ module Google self.write_credentials end + private + + ## + # Attempt to read in credentials from the specified store. + def load_credentials + store.load_credentials + end + ## # @return [Hash] with credentials def credentials_hash @@ -78,19 +92,6 @@ module Google :issued_at => @authorization.issued_at.to_i } end - - ## - # 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 - if @authorization.refresh_token - store.write_credentials(credentials_hash) - end - end end end end From 7de9612071ad5bd745d4a43147db4c2c3c252549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 14 Nov 2013 18:09:48 +0100 Subject: [PATCH 11/33] adds old FileStorage again to be backward compatible --- lib/google/api_client/auth/file_store.rb | 58 ++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/google/api_client/auth/file_store.rb diff --git a/lib/google/api_client/auth/file_store.rb b/lib/google/api_client/auth/file_store.rb new file mode 100644 index 000000000..3c94735b5 --- /dev/null +++ b/lib/google/api_client/auth/file_store.rb @@ -0,0 +1,58 @@ +# 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' +require_relative 'storage' +require_relative 'storages/file_store' + +module Google + class APIClient + + ## + # DEPRECATED WARNING + # This class is deprecated use Storage and FileStore as + # mentioned in the samples + # + # 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 + # + class FileStorage + + attr_accessor :storage, + :path + + def initialize(path) + @path = path + store = Google::APIClient::FileStore.new(@path) + @storage = Google::APIClient::Storage.new(store) + end + + def authorization + storage.authorization + end + + ## + # Write the credentials to the specified file. + # + # @param [Signet::OAuth2::Client] authorization + # Optional authorization instance. If not provided, the authorization + # already associated with this instance will be written. + def write_credentials(auth=nil) + self.authorization = auth unless auth.nil? + storage.write_credentials(self.authorization) + end + end + end +end From a27a122825711e4ca4a5d7e24ed2f6ac8f525d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Fri, 15 Nov 2013 09:20:37 +0100 Subject: [PATCH 12/33] adds comment --- lib/google/api_client/auth/storage.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/google/api_client/auth/storage.rb b/lib/google/api_client/auth/storage.rb index 1fc080eb5..a324c3815 100644 --- a/lib/google/api_client/auth/storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -54,6 +54,9 @@ module Google end end + ## + # Loads credentials and authorizes an client. + # @return [Object] Signet::OAuth2::Client or NIL def authorize if load_credentials cached_credentials = load_credentials From 8418d5abdc385601eb1f1ced465d5f9f66ac32f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Fri, 15 Nov 2013 09:35:52 +0100 Subject: [PATCH 13/33] add file_storage to gemspec --- google-api-client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-api-client.gemspec b/google-api-client.gemspec index 01572d0b6..c89e23727 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.email = "sbazyl@google.com" s.executables = ["google-api"] s.extra_rdoc_files = ["README.md"] - s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/auth/storages/file_store.rb", "lib/google/api_client/auth/storages/redis_store.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] + s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/file_storage.rb" "lib/google/api_client/auth/storages/file_store.rb", "lib/google/api_client/auth/storages/redis_store.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] s.homepage = "http://code.google.com/p/google-api-ruby-client/" s.licenses = ["Apache 2.0"] s.rdoc_options = ["--main", "README.md"] From 4a3827c44b90b29ef57e1972504418912a2093d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Fri, 15 Nov 2013 09:36:30 +0100 Subject: [PATCH 14/33] adds spec construct for file_store and storage --- .../api_client/auth/file_storage_spec.rb | 26 ------------- spec/google/api_client/auth/storage_spec.rb | 38 +++++++++++++++++++ .../auth/storages/file_store_spec.rb | 13 +++++++ 3 files changed, 51 insertions(+), 26 deletions(-) delete mode 100644 spec/google/api_client/auth/file_storage_spec.rb create mode 100644 spec/google/api_client/auth/storage_spec.rb create mode 100644 spec/google/api_client/auth/storages/file_store_spec.rb diff --git a/spec/google/api_client/auth/file_storage_spec.rb b/spec/google/api_client/auth/file_storage_spec.rb deleted file mode 100644 index d3bcc3b70..000000000 --- a/spec/google/api_client/auth/file_storage_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'spec_helper' -require_relative '../../../../lib/google/api_client' -require_relative '../../../../lib/google/api_client/auth/storage' - -describe Google::APIClient::FileStorage do - let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } - let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } - let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'credentials.json')) } - - #file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) - #if file_storage.authorization.nil? - # client_secrets = Google::APIClient::ClientSecrets.load - it 'should initialize' do - Google::APIClient::FileStorage.any_instance.should_receive(:load_credentials) - file_storage = Google::APIClient::FileStorage.new(json_file) - file_storage.path.to_s.should == json_file - end - - it 'should load_credentials' do - Google::APIClient::FileStorage.any_instance.should_receive(:load_credentials) - - file_storage = Google::APIClient::FileStorage.new(json_file) - file_storage.path.to_s.should == json_file - end - -end diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb new file mode 100644 index 000000000..c6d3c3ef3 --- /dev/null +++ b/spec/google/api_client/auth/storage_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' +require_relative '../../../../lib/google/api_client' +require_relative '../../../../lib/google/api_client/auth/storage' + +describe Google::APIClient::Storage do + let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } + let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } + let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } + + it 'should initialize' + + describe 'authorize' do + it 'should authorize' + end + + + describe 'write_credentials' do + + it 'should store credentials to var' + + it 'should call store to write credentials' + + it 'should not call store to write credentials' + + end + + describe 'refresh_authorization' do + + it 'should call refresh and write credentials' + + end + + describe 'load_credentials' do + it 'should call store to load credentials' + + end + +end diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb new file mode 100644 index 000000000..cd7dbc94d --- /dev/null +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +require_relative '../../../../../lib/google/api_client/auth/storages/file_store' + +describe Google::APIClient::FileStore do + let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } + let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } + let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } + + it 'should initialize' + + it 'should load_credentials' + +end From 862a844c638555c62f3da069474a6354ae6dd548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Fri, 15 Nov 2013 09:37:01 +0100 Subject: [PATCH 15/33] adds json fixture to test storage --- spec/fixtures/files/auth_stored_credentials.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 spec/fixtures/files/auth_stored_credentials.json diff --git a/spec/fixtures/files/auth_stored_credentials.json b/spec/fixtures/files/auth_stored_credentials.json new file mode 100644 index 000000000..56ff4ec34 --- /dev/null +++ b/spec/fixtures/files/auth_stored_credentials.json @@ -0,0 +1,10 @@ +{ + "access_token":"my_access_token", + "authorization_uri":"https://accounts.google.com/o/oauth2/auth", + "client_id":"123456_test_client_id@.apps.googleusercontent.com", + "client_secret":"123456_client_secret", + "expires_in":3600, + "refresh_token":"my_refresh_token", + "token_credential_uri":"https://accounts.google.com/o/oauth2/token", + "issued_at":1384440275 +} \ No newline at end of file From 0e9637c77c0643bf594f03d24f6a615f7e3b38d2 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Fri, 15 Nov 2013 10:26:18 +0100 Subject: [PATCH 16/33] rename file_store --- lib/google/api_client/auth/{file_store.rb => file_storage.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/google/api_client/auth/{file_store.rb => file_storage.rb} (100%) diff --git a/lib/google/api_client/auth/file_store.rb b/lib/google/api_client/auth/file_storage.rb similarity index 100% rename from lib/google/api_client/auth/file_store.rb rename to lib/google/api_client/auth/file_storage.rb From 34d3c18fed988d330e36cffb527808d2ba471929 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Fri, 15 Nov 2013 10:27:46 +0100 Subject: [PATCH 17/33] added deprecation warnings to file_storage --- lib/google/api_client/auth/file_storage.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index 3c94735b5..b87bb2e09 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -34,11 +34,17 @@ module Google :path def initialize(path) + Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.") @path = path store = Google::APIClient::FileStore.new(@path) @storage = Google::APIClient::Storage.new(store) end + def load_credentials + Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.") + storage.authorize + end + def authorization storage.authorization end From 05baf20957c475a153eee853b0fdcc8dc0207078 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Fri, 15 Nov 2013 10:42:51 +0100 Subject: [PATCH 18/33] include all git added files to prevent inclusion errors --- google-api-client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-api-client.gemspec b/google-api-client.gemspec index c89e23727..aae908612 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.email = "sbazyl@google.com" s.executables = ["google-api"] s.extra_rdoc_files = ["README.md"] - s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/file_storage.rb" "lib/google/api_client/auth/storages/file_store.rb", "lib/google/api_client/auth/storages/redis_store.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] + s.files = `git ls-files`.split($/) s.homepage = "http://code.google.com/p/google-api-ruby-client/" s.licenses = ["Apache 2.0"] s.rdoc_options = ["--main", "README.md"] From 5c61e39ba476fed01b41d07ff186b9bbc9ae7f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Mon, 18 Nov 2013 11:39:02 +0100 Subject: [PATCH 19/33] use authorization method instead of variable remove authorize from initialize --- lib/google/api_client/auth/storage.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/google/api_client/auth/storage.rb b/lib/google/api_client/auth/storage.rb index a324c3815..db308e5e6 100644 --- a/lib/google/api_client/auth/storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -38,7 +38,6 @@ module Google # @params [Object] Storage object def initialize(store) @store= store - self.authorize end ## @@ -58,18 +57,20 @@ module Google # Loads credentials and authorizes an client. # @return [Object] Signet::OAuth2::Client or NIL def authorize - if load_credentials - cached_credentials = load_credentials + @authorization = false + cached_credentials = load_credentials + if cached_credentials && cached_credentials.size > 0 @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 + return @authorization end ## # refresh credentials and save them to store def refresh_authorization - @authorization.refresh! + authorization.refresh! self.write_credentials end @@ -85,14 +86,14 @@ module Google # @return [Hash] with credentials def credentials_hash { - :access_token => @authorization.access_token, + :access_token => authorization.access_token, :authorization_uri => AUTHORIZATION_URI, - :client_id => @authorization.client_id, - :client_secret => @authorization.client_secret, - :expires_in => @authorization.expires_in, - :refresh_token => @authorization.refresh_token, + :client_id => authorization.client_id, + :client_secret => authorization.client_secret, + :expires_in => authorization.expires_in, + :refresh_token => authorization.refresh_token, :token_credential_uri => TOKEN_CREDENTIAL_URI, - :issued_at => @authorization.issued_at.to_i + :issued_at => authorization.issued_at.to_i } end end From 6404615a5722d0788433a678003725363fa2a450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Mon, 18 Nov 2013 11:39:40 +0100 Subject: [PATCH 20/33] uses new initialize and authorize --- lib/google/api_client/auth/file_storage.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index b87bb2e09..771ebac7c 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -38,6 +38,7 @@ module Google @path = path store = Google::APIClient::FileStore.new(@path) @storage = Google::APIClient::Storage.new(store) + @storage.authorize end def load_credentials From e6858bd2924245745027468af2b4d6a6d80d9e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Mon, 18 Nov 2013 11:39:58 +0100 Subject: [PATCH 21/33] adds some specs for storage class --- spec/google/api_client/auth/storage_spec.rb | 49 +++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb index c6d3c3ef3..b61057df3 100644 --- a/spec/google/api_client/auth/storage_spec.rb +++ b/spec/google/api_client/auth/storage_spec.rb @@ -7,10 +7,48 @@ describe Google::APIClient::Storage do let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } + let(:store) { double } + let(:client_stub) { double } + subject { Google::APIClient::Storage.new(store) } + it 'should initialize' describe 'authorize' do - it 'should authorize' + it 'should authorize' do + subject.should respond_to(:authorization) + subject.store.should == store + end + end + + describe 'authorize' do + describe 'with credentials' do + + it 'should initialize a new OAuth Client' do + subject.should_receive(:load_credentials).and_return({:first => 'a dummy'}) + client_stub.stub(:issued_at=) + client_stub.stub(:expired?).and_return(false) + Signet::OAuth2::Client.should_receive(:new).and_return(client_stub) + subject.should_not_receive(:refresh_authorization) + subject.authorize + end + + it 'should refresh authorization' do + subject.should_receive(:load_credentials).and_return({:first => 'a dummy'}) + client_stub.stub(:issued_at=) + client_stub.stub(:expired?).and_return(true) + Signet::OAuth2::Client.should_receive(:new).and_return(client_stub) + subject.should_receive(:refresh_authorization) + subject.authorize + end + + end + describe 'without credentials' do + + it 'should return false' do + subject.should_receive(:load_credentials).and_return({}) + subject.authorize.should be_false + end + end end @@ -26,8 +64,13 @@ describe Google::APIClient::Storage do describe 'refresh_authorization' do - it 'should call refresh and write credentials' - + it 'should call refresh and write credentials' do + subject.should_receive(:write_credentials) + authorization_stub = double + subject.stub(:authorization).and_return(authorization_stub) + authorization_stub.should_receive(:refresh!).and_return(true) + subject.refresh_authorization + end end describe 'load_credentials' do From 31fac0a6f55225d36a942e98642966c65f1487b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 28 Nov 2013 08:14:41 +0100 Subject: [PATCH 22/33] adds specs for storage changes expectation in write_credentials --- lib/google/api_client/auth/storage.rb | 2 +- spec/google/api_client/auth/storage_spec.rb | 58 +++++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/google/api_client/auth/storage.rb b/lib/google/api_client/auth/storage.rb index db308e5e6..5197cf2d7 100644 --- a/lib/google/api_client/auth/storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -48,7 +48,7 @@ module Google # already associated with this instance will be written. def write_credentials(authorization=nil) @authorization = authorization if authorization - if @authorization.refresh_token + if @authorization.respond_to?(:refresh_token) && @authorization.refresh_token store.write_credentials(credentials_hash) end end diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb index b61057df3..1c319f4b7 100644 --- a/spec/google/api_client/auth/storage_spec.rb +++ b/spec/google/api_client/auth/storage_spec.rb @@ -40,8 +40,8 @@ describe Google::APIClient::Storage do subject.should_receive(:refresh_authorization) subject.authorize end - end + describe 'without credentials' do it 'should return false' do @@ -51,19 +51,34 @@ describe Google::APIClient::Storage do end end - describe 'write_credentials' do + it 'should call store to write credentials' do + authorization_stub = double + authorization_stub.should_receive(:refresh_token).and_return(true) + subject.should_receive(:credentials_hash) + subject.store.should_receive(:write_credentials) + subject.write_credentials(authorization_stub) + subject.authorization.should == authorization_stub + end - it 'should store credentials to var' - - it 'should call store to write credentials' - - it 'should not call store to write credentials' + it 'should not call store to write credentials' do + subject.should_not_receive(:credentials_hash) + subject.store.should_not_receive(:write_credentials) + expect { + subject.write_credentials() + }.not_to raise_error + end + it 'should not call store to write credentials' do + subject.should_not_receive(:credentials_hash) + subject.store.should_not_receive(:write_credentials) + expect { + subject.write_credentials('something') + }.not_to raise_error + end end describe 'refresh_authorization' do - it 'should call refresh and write credentials' do subject.should_receive(:write_credentials) authorization_stub = double @@ -74,8 +89,31 @@ describe Google::APIClient::Storage do end describe 'load_credentials' do - it 'should call store to load credentials' - + it 'should call store to load credentials' do + subject.store.should_receive(:load_credentials) + subject.send(:load_credentials) + end end + describe 'credentials_hash' do + it 'should return an hash' do + authorization_stub = double + authorization_stub.should_receive(:access_token) + authorization_stub.should_receive(:client_id) + authorization_stub.should_receive(:client_secret) + authorization_stub.should_receive(:expires_in) + authorization_stub.should_receive(:refresh_token) + authorization_stub.should_receive(:issued_at).and_return('100') + subject.stub(:authorization).and_return(authorization_stub) + credentials = subject.send(:credentials_hash) + credentials.should include(:access_token) + credentials.should include(:authorization_uri) + credentials.should include(:client_id) + credentials.should include(:client_secret) + credentials.should include(:expires_in) + credentials.should include(:refresh_token) + credentials.should include(:token_credential_uri) + credentials.should include(:issued_at) + end + end end From 4666fedaed1f0b368bd98273f83c29a3fad5615e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 28 Nov 2013 10:24:47 +0100 Subject: [PATCH 23/33] load_credentials just returns nil on errors --- lib/google/api_client/auth/storages/file_store.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/google/api_client/auth/storages/file_store.rb b/lib/google/api_client/auth/storages/file_store.rb index 1b8ad4a36..850dee2d0 100644 --- a/lib/google/api_client/auth/storages/file_store.rb +++ b/lib/google/api_client/auth/storages/file_store.rb @@ -37,10 +37,9 @@ module Google ## # Attempt to read in credentials from the specified file. def load_credentials - if File.exists?(path) && File.readable?(path) && File.writable?(path) - credentials = File.open(path, 'r') { |f| JSON.parse(f.read) } - end - credentials + File.open(path, 'r') { |f| JSON.parse(f.read) } + rescue + nil end ## From 53be124a6f0d69b8735024e2460084c4e2e8f097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 28 Nov 2013 10:26:23 +0100 Subject: [PATCH 24/33] pending test for file_store and redis_store --- .../api_client/auth/storages/file_store_spec.rb | 5 +++-- .../api_client/auth/storages/redis_store_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 spec/google/api_client/auth/storages/redis_store_spec.rb diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb index cd7dbc94d..b2cee7c8d 100644 --- a/spec/google/api_client/auth/storages/file_store_spec.rb +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -2,12 +2,13 @@ require 'spec_helper' require_relative '../../../../../lib/google/api_client/auth/storages/file_store' describe Google::APIClient::FileStore do - let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } it 'should initialize' - it 'should load_credentials' + it 'should load credentials' + + it 'should write credentials' end diff --git a/spec/google/api_client/auth/storages/redis_store_spec.rb b/spec/google/api_client/auth/storages/redis_store_spec.rb new file mode 100644 index 000000000..ba5b88923 --- /dev/null +++ b/spec/google/api_client/auth/storages/redis_store_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +require_relative '../../../../../lib/google/api_client/auth/storages/redis_store' + +describe Google::APIClient::RedisStore do + let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } + + it 'should initialize' + + it 'should load credentials' + + it 'should write credentials' + +end From 80d25c219b8a0b39f9365faf2257f26ed78d230c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 28 Nov 2013 12:26:25 +0100 Subject: [PATCH 25/33] bugfix file_storage --- lib/google/api_client/auth/file_storage.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index 771ebac7c..f6c41289b 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -34,7 +34,7 @@ module Google :path def initialize(path) - Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.") + Google::APIClient.logger("DEPRECATED: Please use Storage Class instead.") @path = path store = Google::APIClient::FileStore.new(@path) @storage = Google::APIClient::Storage.new(store) @@ -42,7 +42,7 @@ module Google end def load_credentials - Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.") + Google::APIClient.logger("DEPRECATED: Please use Storage Class instead.") storage.authorize end From 18c4bab286ab5208532e4051f3b4da6664338ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Thu, 28 Nov 2013 12:29:55 +0100 Subject: [PATCH 26/33] remove deprecation warning --- lib/google/api_client/auth/file_storage.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/google/api_client/auth/file_storage.rb b/lib/google/api_client/auth/file_storage.rb index f6c41289b..83786f20f 100644 --- a/lib/google/api_client/auth/file_storage.rb +++ b/lib/google/api_client/auth/file_storage.rb @@ -34,7 +34,6 @@ module Google :path def initialize(path) - Google::APIClient.logger("DEPRECATED: Please use Storage Class instead.") @path = path store = Google::APIClient::FileStore.new(@path) @storage = Google::APIClient::Storage.new(store) @@ -42,7 +41,6 @@ module Google end def load_credentials - Google::APIClient.logger("DEPRECATED: Please use Storage Class instead.") storage.authorize end From b1f8ac3c0d83a5d9756537357bc64cf5df79fd19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 08:30:28 +0100 Subject: [PATCH 27/33] change default for authorization to nil --- lib/google/api_client/auth/storage.rb | 2 +- spec/google/api_client/auth/storage_spec.rb | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/google/api_client/auth/storage.rb b/lib/google/api_client/auth/storage.rb index 5197cf2d7..e7cc5bc4e 100644 --- a/lib/google/api_client/auth/storage.rb +++ b/lib/google/api_client/auth/storage.rb @@ -57,7 +57,7 @@ module Google # Loads credentials and authorizes an client. # @return [Object] Signet::OAuth2::Client or NIL def authorize - @authorization = false + @authorization = nil cached_credentials = load_credentials if cached_credentials && cached_credentials.size > 0 @authorization = Signet::OAuth2::Client.new(cached_credentials) diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb index 1c319f4b7..b9564644d 100644 --- a/spec/google/api_client/auth/storage_spec.rb +++ b/spec/google/api_client/auth/storage_spec.rb @@ -38,15 +38,19 @@ describe Google::APIClient::Storage do client_stub.stub(:expired?).and_return(true) Signet::OAuth2::Client.should_receive(:new).and_return(client_stub) subject.should_receive(:refresh_authorization) - subject.authorize + auth = subject.authorize + auth.should == subject.authorization + auth.should_not be_nil end end describe 'without credentials' do - it 'should return false' do + it 'should return nil' do + subject.authorization.should be_nil subject.should_receive(:load_credentials).and_return({}) - subject.authorize.should be_false + subject.authorize.should be_nil + subject.authorization.should be_nil end end end From 71eeabe55c1c2d8ed2c92fbf6f07fcf99832281f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 08:30:55 +0100 Subject: [PATCH 28/33] adds specs for file_store --- .../api_client/auth/storages/file_store.rb | 6 ++-- .../files/auth_stored_credentials.json | 18 +++++------ .../auth/storages/file_store_spec.rb | 32 ++++++++++++++++--- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/google/api_client/auth/storages/file_store.rb b/lib/google/api_client/auth/storages/file_store.rb index 850dee2d0..cd3eae710 100644 --- a/lib/google/api_client/auth/storages/file_store.rb +++ b/lib/google/api_client/auth/storages/file_store.rb @@ -37,7 +37,7 @@ module Google ## # Attempt to read in credentials from the specified file. def load_credentials - File.open(path, 'r') { |f| JSON.parse(f.read) } + open(path, 'r') { |f| JSON.parse(f.read) } rescue nil end @@ -49,8 +49,8 @@ module Google # Optional authorization instance. If not provided, the authorization # already associated with this instance will be written. def write_credentials(credentials_hash) - File.open(self.path, 'w') do |file| - file.write(credentials_hash.to_json) + open(self.path, 'w+') do |f| + f.write(credentials_hash.to_json) end end end diff --git a/spec/fixtures/files/auth_stored_credentials.json b/spec/fixtures/files/auth_stored_credentials.json index 56ff4ec34..4cd786e4a 100644 --- a/spec/fixtures/files/auth_stored_credentials.json +++ b/spec/fixtures/files/auth_stored_credentials.json @@ -1,10 +1,8 @@ -{ - "access_token":"my_access_token", - "authorization_uri":"https://accounts.google.com/o/oauth2/auth", - "client_id":"123456_test_client_id@.apps.googleusercontent.com", - "client_secret":"123456_client_secret", - "expires_in":3600, - "refresh_token":"my_refresh_token", - "token_credential_uri":"https://accounts.google.com/o/oauth2/token", - "issued_at":1384440275 -} \ No newline at end of file +{ "access_token":"access_token_123456789", + "authorization_uri":"https://accounts.google.com/o/oauth2/auth", + "client_id":"123456789p.apps.googleusercontent.com", + "client_secret":"very_secret", + "expires_in":3600, + "refresh_token":"refresh_token_12345679", + "token_credential_uri":"https://accounts.google.com/o/oauth2/token", + "issued_at":1386053761} \ No newline at end of file diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb index b2cee7c8d..e2b165f57 100644 --- a/spec/google/api_client/auth/storages/file_store_spec.rb +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -2,13 +2,37 @@ require 'spec_helper' require_relative '../../../../../lib/google/api_client/auth/storages/file_store' describe Google::APIClient::FileStore do - let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } + let(:root_path) { File.expand_path(File.join(__FILE__, '..','..','..', '..','..')) } let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } - it 'should initialize' + let(:credentials_hash) {{ + "access_token"=>"my_access_token", + "authorization_uri"=>"https://accounts.google.com/o/oauth2/auth", + "client_id"=>"123456_test_client_id@.apps.googleusercontent.com", + "client_secret"=>"123456_client_secret", + "expires_in"=>3600, + "refresh_token"=>"my_refresh_token", + "token_credential_uri"=>"https://accounts.google.com/o/oauth2/token", + "issued_at"=>1384440275 + }} - it 'should load credentials' + subject{Google::APIClient::FileStore.new('a file path')} - it 'should write credentials' + it 'should have a path' do + subject.path.should == 'a file path' + subject.path = 'an other file path' + subject.path.should == 'an other file path' + end + it 'should load credentials' do + subject.path = json_file + credentials = subject.load_credentials + credentials.should include('access_token', 'authorization_uri', 'refresh_token') + end + + it 'should write credentials' do + io_stub = StringIO.new + subject.should_receive(:open).and_return(io_stub) + subject.write_credentials(credentials_hash) + end end From 3d1568d67ff618c548ee057daf3878e4608e4073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 08:42:01 +0100 Subject: [PATCH 29/33] adds specs for redis_store --- .../auth/storages/redis_store_spec.rb | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/spec/google/api_client/auth/storages/redis_store_spec.rb b/spec/google/api_client/auth/storages/redis_store_spec.rb index ba5b88923..9f653bf64 100644 --- a/spec/google/api_client/auth/storages/redis_store_spec.rb +++ b/spec/google/api_client/auth/storages/redis_store_spec.rb @@ -2,12 +2,51 @@ require 'spec_helper' require_relative '../../../../../lib/google/api_client/auth/storages/redis_store' describe Google::APIClient::RedisStore do + let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..')) } let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } + let(:redis) {double} - it 'should initialize' + let(:credentials_hash) { { + "access_token" => "my_access_token", + "authorization_uri" => "https://accounts.google.com/o/oauth2/auth", + "client_id" => "123456_test_client_id@.apps.googleusercontent.com", + "client_secret" => "123456_client_secret", + "expires_in" => 3600, + "refresh_token" => "my_refresh_token", + "token_credential_uri" => "https://accounts.google.com/o/oauth2/token", + "issued_at" => 1384440275 + } } - it 'should load credentials' + subject { Google::APIClient::RedisStore.new('a redis instance') } - it 'should write credentials' + it 'should have a redis instance' do + subject.redis.should == 'a redis instance' + subject.redis = 'an other redis instance' + subject.redis.should == 'an other redis instance' + end + + describe 'load_credentials' do + + it 'should load credentials' do + subject.redis= redis + redis.should_receive(:get).and_return(credentials_hash.to_json) + subject.load_credentials.should == credentials_hash + end + + it 'should return nil' do + subject.redis= redis + redis.should_receive(:get).and_return(nil) + subject.load_credentials.should == nil + end + end + + describe 'write credentials' do + + it 'should write credentials' do + subject.redis= redis + redis.should_receive(:set).and_return('ok') + subject.write_credentials(credentials_hash).should be_true + end + end end From 0cb0075c7b2c0b27ee6f0f2366d445b5b36546b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 10:01:06 +0100 Subject: [PATCH 30/33] remove useless test --- spec/google/api_client/auth/storage_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb index b9564644d..32624b73b 100644 --- a/spec/google/api_client/auth/storage_spec.rb +++ b/spec/google/api_client/auth/storage_spec.rb @@ -11,8 +11,6 @@ describe Google::APIClient::Storage do let(:client_stub) { double } subject { Google::APIClient::Storage.new(store) } - it 'should initialize' - describe 'authorize' do it 'should authorize' do subject.should respond_to(:authorization) From 3ef99c7ebb23b7840ff11d045c4f1494de751ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 11:11:51 +0100 Subject: [PATCH 31/33] changes for ruby 1.8.7 --- lib/google/api_client/service_account.rb | 3 +++ spec/google/api_client/auth/storage_spec.rb | 5 +++-- spec/google/api_client/auth/storages/file_store_spec.rb | 4 +++- spec/google/api_client/auth/storages/redis_store_spec.rb | 5 ++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/google/api_client/service_account.rb b/lib/google/api_client/service_account.rb index b6a0b3cb0..3d941ae07 100644 --- a/lib/google/api_client/service_account.rb +++ b/lib/google/api_client/service_account.rb @@ -16,3 +16,6 @@ require 'google/api_client/auth/pkcs12' require 'google/api_client/auth/jwt_asserter' require 'google/api_client/auth/key_utils' require 'google/api_client/auth/compute_service_account' +require 'google/api_client/auth/storage' +require 'google/api_client/auth/storages/redis_store' +require 'google/api_client/auth/storages/file_store' diff --git a/spec/google/api_client/auth/storage_spec.rb b/spec/google/api_client/auth/storage_spec.rb index 32624b73b..ac6fea651 100644 --- a/spec/google/api_client/auth/storage_spec.rb +++ b/spec/google/api_client/auth/storage_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' -require_relative '../../../../lib/google/api_client' -require_relative '../../../../lib/google/api_client/auth/storage' + +require 'google/api_client' +require 'google/api_client/version' describe Google::APIClient::Storage do let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') } diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb index e2b165f57..fd4d7b1fa 100644 --- a/spec/google/api_client/auth/storages/file_store_spec.rb +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -1,5 +1,7 @@ require 'spec_helper' -require_relative '../../../../../lib/google/api_client/auth/storages/file_store' + +require 'google/api_client' +require 'google/api_client/version' describe Google::APIClient::FileStore do let(:root_path) { File.expand_path(File.join(__FILE__, '..','..','..', '..','..')) } diff --git a/spec/google/api_client/auth/storages/redis_store_spec.rb b/spec/google/api_client/auth/storages/redis_store_spec.rb index 9f653bf64..ab40f9faa 100644 --- a/spec/google/api_client/auth/storages/redis_store_spec.rb +++ b/spec/google/api_client/auth/storages/redis_store_spec.rb @@ -1,5 +1,8 @@ require 'spec_helper' -require_relative '../../../../../lib/google/api_client/auth/storages/redis_store' + +require 'google/api_client' +require 'google/api_client/version' + describe Google::APIClient::RedisStore do let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..')) } From adf6d1facd14f16f4e40ea35d1cb3c6c3e81c6b9 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Fri, 10 Jan 2014 15:50:04 +0100 Subject: [PATCH 32/33] added optional redis credential key to be set --- .../api_client/auth/storages/redis_store.rb | 5 +++-- .../api_client/auth/storages/redis_store_spec.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client/auth/storages/redis_store.rb b/lib/google/api_client/auth/storages/redis_store.rb index 715ab5dbd..af9ddb765 100644 --- a/lib/google/api_client/auth/storages/redis_store.rb +++ b/lib/google/api_client/auth/storages/redis_store.rb @@ -24,8 +24,9 @@ module Google # Initializes the RedisStore object. # # @params [Object] Redis instance - def initialize(redis) + def initialize(redis, key = nil) @redis= redis + @redis_credentials_key = key end ## @@ -36,7 +37,7 @@ module Google end def redis_credentials_key - "google_api_credentials" + @redis_credentials_key || "google_api_credentials" end ## diff --git a/spec/google/api_client/auth/storages/redis_store_spec.rb b/spec/google/api_client/auth/storages/redis_store_spec.rb index ab40f9faa..a275e52c0 100644 --- a/spec/google/api_client/auth/storages/redis_store_spec.rb +++ b/spec/google/api_client/auth/storages/redis_store_spec.rb @@ -43,6 +43,21 @@ describe Google::APIClient::RedisStore do end end + describe 'redis_credentials_key' do + context 'without given key' do + it 'should return default key' do + subject.redis_credentials_key.should == "google_api_credentials" + end + end + context 'with given key' do + let(:redis_store) { Google::APIClient::RedisStore.new('a redis instance', 'another_google_api_credentials') } + it 'should use given key' do + redis_store.redis_credentials_key.should == "another_google_api_credentials" + end + end + + end + describe 'write credentials' do it 'should write credentials' do From fd145d2cd11a839fa70c3b22c05ef9fc3cca5a86 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Fri, 10 Jan 2014 16:05:50 +0100 Subject: [PATCH 33/33] moved default key into constant --- lib/google/api_client/auth/storages/redis_store.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/google/api_client/auth/storages/redis_store.rb b/lib/google/api_client/auth/storages/redis_store.rb index af9ddb765..3f76f7ca8 100644 --- a/lib/google/api_client/auth/storages/redis_store.rb +++ b/lib/google/api_client/auth/storages/redis_store.rb @@ -18,6 +18,8 @@ module Google class APIClient class RedisStore + DEFAULT_REDIS_CREDENTIALS_KEY = "google_api_credentials" + attr_accessor :redis ## @@ -37,7 +39,7 @@ module Google end def redis_credentials_key - @redis_credentials_key || "google_api_credentials" + @redis_credentials_key || DEFAULT_REDIS_CREDENTIALS_KEY end ##