Skip to content

Commit 8510735

Browse files
committed
Resolve merge conflict, update rspec syntax
2 parents 262ebe3 + fd145d2 commit 8510735

File tree

10 files changed

+482
-44
lines changed

10 files changed

+482
-44
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ pkg
1111
specdoc
1212
wiki
1313
.google-api.yaml
14+
*.log
15+
16+
#IntelliJ
17+
.idea
18+
*.iml
19+
atlassian*
20+

lib/google/api_client/auth/file_storage.rb

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,39 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require 'json'
1615
require 'signet/oauth_2/client'
16+
require_relative 'storage'
17+
require_relative 'storages/file_store'
1718

1819
module Google
1920
class APIClient
21+
2022
##
2123
# Represents cached OAuth 2 tokens stored on local disk in a
2224
# JSON serialized file. Meant to resemble the serialized format
2325
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
2426
#
27+
# @deprecated
28+
# Use {Google::APIClient::Storage} and {Google::APIClient::FileStore} instead
29+
#
2530
class FileStorage
26-
# @return [String] Path to the credentials file.
27-
attr_accessor :path
2831

29-
# @return [Signet::OAuth2::Client] Path to the credentials file.
30-
attr_reader :authorization
32+
attr_accessor :storage,
33+
:path
3134

32-
##
33-
# Initializes the FileStorage object.
34-
#
35-
# @param [String] path
36-
# Path to the credentials file.
3735
def initialize(path)
3836
@path = path
39-
self.load_credentials
37+
store = Google::APIClient::FileStore.new(@path)
38+
@storage = Google::APIClient::Storage.new(store)
39+
@storage.authorize
4040
end
4141

42-
##
43-
# Attempt to read in credentials from the specified file.
4442
def load_credentials
45-
if File.exists? self.path
46-
File.open(self.path, 'r') do |file|
47-
cached_credentials = JSON.load(file)
48-
@authorization = Signet::OAuth2::Client.new(cached_credentials)
49-
@authorization.issued_at = Time.at(cached_credentials['issued_at'])
50-
if @authorization.expired?
51-
@authorization.fetch_access_token!
52-
self.write_credentials
53-
end
54-
end
55-
end
43+
storage.authorize
44+
end
45+
46+
def authorization
47+
storage.authorization
5648
end
5749

5850
##
@@ -61,26 +53,9 @@ def load_credentials
6153
# @param [Signet::OAuth2::Client] authorization
6254
# Optional authorization instance. If not provided, the authorization
6355
# already associated with this instance will be written.
64-
def write_credentials(authorization=nil)
65-
@authorization = authorization unless authorization.nil?
66-
67-
unless @authorization.refresh_token.nil?
68-
hash = {}
69-
%w'access_token
70-
authorization_uri
71-
client_id
72-
client_secret
73-
expires_in
74-
refresh_token
75-
token_credential_uri'.each do |var|
76-
hash[var] = @authorization.instance_variable_get("@#{var}")
77-
end
78-
hash['issued_at'] = @authorization.issued_at.to_i
79-
80-
File.open(self.path, 'w', 0600) do |file|
81-
file.write(hash.to_json)
82-
end
83-
end
56+
def write_credentials(auth=nil)
57+
self.authorization = auth unless auth.nil?
58+
storage.write_credentials(self.authorization)
8459
end
8560
end
8661
end

lib/google/api_client/auth/storage.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2013 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'signet/oauth_2/client'
16+
17+
module Google
18+
class APIClient
19+
##
20+
# Represents cached OAuth 2 tokens stored on local disk in a
21+
# JSON serialized file. Meant to resemble the serialized format
22+
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
23+
#
24+
class Storage
25+
26+
AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth'
27+
TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'
28+
29+
# @return [Object] Storage object.
30+
attr_accessor :store
31+
32+
# @return [Signet::OAuth2::Client]
33+
attr_reader :authorization
34+
35+
##
36+
# Initializes the Storage object.
37+
#
38+
# @params [Object] Storage object
39+
def initialize(store)
40+
@store= store
41+
end
42+
43+
##
44+
# Write the credentials to the specified store.
45+
#
46+
# @params [Signet::OAuth2::Client] authorization
47+
# Optional authorization instance. If not provided, the authorization
48+
# already associated with this instance will be written.
49+
def write_credentials(authorization=nil)
50+
@authorization = authorization if authorization
51+
if @authorization.respond_to?(:refresh_token) && @authorization.refresh_token
52+
store.write_credentials(credentials_hash)
53+
end
54+
end
55+
56+
##
57+
# Loads credentials and authorizes an client.
58+
# @return [Object] Signet::OAuth2::Client or NIL
59+
def authorize
60+
@authorization = nil
61+
cached_credentials = load_credentials
62+
if cached_credentials && cached_credentials.size > 0
63+
@authorization = Signet::OAuth2::Client.new(cached_credentials)
64+
@authorization.issued_at = Time.at(cached_credentials['issued_at'].to_i)
65+
self.refresh_authorization if @authorization.expired?
66+
end
67+
return @authorization
68+
end
69+
70+
##
71+
# refresh credentials and save them to store
72+
def refresh_authorization
73+
authorization.refresh!
74+
self.write_credentials
75+
end
76+
77+
private
78+
79+
##
80+
# Attempt to read in credentials from the specified store.
81+
def load_credentials
82+
store.load_credentials
83+
end
84+
85+
##
86+
# @return [Hash] with credentials
87+
def credentials_hash
88+
{
89+
:access_token => authorization.access_token,
90+
:authorization_uri => AUTHORIZATION_URI,
91+
:client_id => authorization.client_id,
92+
:client_secret => authorization.client_secret,
93+
:expires_in => authorization.expires_in,
94+
:refresh_token => authorization.refresh_token,
95+
:token_credential_uri => TOKEN_CREDENTIAL_URI,
96+
:issued_at => authorization.issued_at.to_i
97+
}
98+
end
99+
end
100+
end
101+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2013 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'json'
16+
17+
module Google
18+
class APIClient
19+
##
20+
# Represents cached OAuth 2 tokens stored on local disk in a
21+
# JSON serialized file. Meant to resemble the serialized format
22+
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
23+
#
24+
class FileStore
25+
26+
attr_accessor :path
27+
28+
##
29+
# Initializes the FileStorage object.
30+
#
31+
# @param [String] path
32+
# Path to the credentials file.
33+
def initialize(path)
34+
@path= path
35+
end
36+
37+
##
38+
# Attempt to read in credentials from the specified file.
39+
def load_credentials
40+
open(path, 'r') { |f| JSON.parse(f.read) }
41+
rescue
42+
nil
43+
end
44+
45+
##
46+
# Write the credentials to the specified file.
47+
#
48+
# @param [Signet::OAuth2::Client] authorization
49+
# Optional authorization instance. If not provided, the authorization
50+
# already associated with this instance will be written.
51+
def write_credentials(credentials_hash)
52+
open(self.path, 'w+') do |f|
53+
f.write(credentials_hash.to_json)
54+
end
55+
end
56+
end
57+
end
58+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2013 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'json'
16+
17+
module Google
18+
class APIClient
19+
class RedisStore
20+
21+
DEFAULT_REDIS_CREDENTIALS_KEY = "google_api_credentials"
22+
23+
attr_accessor :redis
24+
25+
##
26+
# Initializes the RedisStore object.
27+
#
28+
# @params [Object] Redis instance
29+
def initialize(redis, key = nil)
30+
@redis= redis
31+
@redis_credentials_key = key
32+
end
33+
34+
##
35+
# Attempt to read in credentials from redis.
36+
def load_credentials
37+
credentials = redis.get redis_credentials_key
38+
JSON.parse(credentials) if credentials
39+
end
40+
41+
def redis_credentials_key
42+
@redis_credentials_key || DEFAULT_REDIS_CREDENTIALS_KEY
43+
end
44+
45+
##
46+
# Write the credentials to redis.
47+
#
48+
# @params [Hash] credentials
49+
def write_credentials(credentials_hash)
50+
redis.set(redis_credentials_key, credentials_hash.to_json)
51+
end
52+
end
53+
end
54+
end

lib/google/api_client/service_account.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
require 'google/api_client/auth/jwt_asserter'
1717
require 'google/api_client/auth/key_utils'
1818
require 'google/api_client/auth/compute_service_account'
19+
require 'google/api_client/auth/storage'
20+
require 'google/api_client/auth/storages/redis_store'
21+
require 'google/api_client/auth/storages/file_store'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{ "access_token":"access_token_123456789",
2+
"authorization_uri":"https://accounts.google.com/o/oauth2/auth",
3+
"client_id":"123456789p.apps.googleusercontent.com",
4+
"client_secret":"very_secret",
5+
"expires_in":3600,
6+
"refresh_token":"refresh_token_12345679",
7+
"token_credential_uri":"https://accounts.google.com/o/oauth2/token",
8+
"issued_at":1386053761}

0 commit comments

Comments
 (0)