google-api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## v0.0.1.rc2 (TBD)
1
+ ## v0.2.0 (2012-11-24)
2
+
3
+ * Rewrite token encryption to use OpenSSL's Cipher. (NOTE: This is a non-backward compatible change!)
4
+
5
+ ## v0.1.0 (2012-11-14)
2
6
 
3
7
  * Generalize #update_oauth! method for an oauthable ActiveRecord object. See active_record_inclusions.rb for more.
4
8
 
data/google-api.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "google-api"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = "0.1.0"
14
+ gem.version = "0.2.0"
15
15
 
16
16
  gem.add_runtime_dependency 'mime-types', '~> 1.0'
17
17
  gem.add_runtime_dependency 'oauth2', '~> 0.8.0'
@@ -14,8 +14,8 @@ module GoogleAPI
14
14
  def oauthable
15
15
  define_method :oauth_hash do
16
16
  {
17
- access_token: GoogleAPI.decrypt!(oauth_access_token),
18
- refresh_token: GoogleAPI.decrypt!(oauth_refresh_token),
17
+ access_token: GoogleAPI::Encrypter.decrypt!(oauth_access_token),
18
+ refresh_token: GoogleAPI::Encrypter.decrypt!(oauth_refresh_token),
19
19
  expires_at: oauth_access_token_expires_at
20
20
  }
21
21
  end
@@ -25,10 +25,10 @@ module GoogleAPI
25
25
  # that you want to update on the object can be passed as a final parameter.
26
26
  define_method :update_oauth! do |access_token, refresh_token = nil, additional_attrs = {}|
27
27
  attrs = {
28
- oauth_access_token: GoogleAPI.encrypt!(access_token),
28
+ oauth_access_token: GoogleAPI::Encrypter.encrypt!(access_token),
29
29
  oauth_access_token_expires_at: 59.minutes.from_now # it's actually an hour from now, but just to make sure we don't overlap at all, let's set it to 59 minutes
30
30
  }.merge(additional_attrs)
31
- attrs[:oauth_refresh_token] = GoogleAPI.encrypt!(refresh_token) if refresh_token
31
+ attrs[:oauth_refresh_token] = GoogleAPI::Encrypter.encrypt!(refresh_token) if refresh_token
32
32
  update_attributes(attrs)
33
33
  end
34
34
 
@@ -0,0 +1,26 @@
1
+ module GoogleAPI
2
+ class Encrypter
3
+
4
+ class << self
5
+
6
+ def encrypt!(value)
7
+ crypt(:encrypt, value)
8
+ end
9
+
10
+ def decrypt!(value)
11
+ crypt(:decrypt, value)
12
+ end
13
+
14
+ protected
15
+ def crypt(method, value)
16
+ cipher = OpenSSL::Cipher::AES256.new(:CBC)
17
+ cipher.send(method)
18
+ cipher.key = GoogleAPI.encryption_key
19
+ result = cipher.update(value)
20
+ result << cipher.final
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
data/lib/google-api.rb CHANGED
@@ -3,6 +3,7 @@ require 'oauth2'
3
3
  require 'google-api/oauth2'
4
4
  require 'google-api/api'
5
5
  require 'google-api/client'
6
+ require 'google-api/encrypter'
6
7
  if defined?(::Rails)
7
8
  require 'google-api/railtie'
8
9
  else
@@ -56,14 +57,6 @@ module GoogleAPI
56
57
  @discovered_apis = {}
57
58
  end
58
59
 
59
- def encrypt!(string)
60
- Base64.encode64("#{string}#{encryption_key}")
61
- end
62
-
63
- def decrypt!(string)
64
- Base64.decode64(string).sub(/#{Regexp.escape(encryption_key)}$/, '')
65
- end
66
-
67
60
  end
68
61
 
69
62
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleAPI::Encrypter do
4
+
5
+ before(:each) do
6
+ GoogleAPI.configure do |config|
7
+ config.client_id = 'test id'
8
+ config.client_secret = 'test secret'
9
+ config.encryption_key = 'a really really really really really really long encryption key'
10
+ end
11
+ end
12
+
13
+ describe "#encrypt!" do
14
+
15
+ it "should return an encrypted version of the string" do
16
+ GoogleAPI::Encrypter.encrypt!('test').should == "0\xC2\xF3y\b\xA00\xB1\x9C\xBD;\xC7s61\x8E"
17
+ end
18
+
19
+ end
20
+
21
+ describe "#decrypt!" do
22
+
23
+ it "should return an decrypted version of the string" do
24
+ GoogleAPI::Encrypter.decrypt!("0\xC2\xF3y\b\xA00\xB1\x9C\xBD;\xC7s61\x8E").should == 'test'
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -116,32 +116,4 @@ describe GoogleAPI do
116
116
 
117
117
  end
118
118
 
119
- describe "#encrypt!" do
120
-
121
- it "should return an encrypted version of the string" do
122
- GoogleAPI.configure do |config|
123
- config.client_id = 'test id'
124
- config.client_secret = 'test secret'
125
- config.encryption_key = 'encryption key'
126
- end
127
-
128
- GoogleAPI.encrypt!('test').should == "dGVzdGVuY3J5cHRpb24ga2V5\n"
129
- end
130
-
131
- end
132
-
133
- describe "#decrypt!" do
134
-
135
- it "should return an decrypted version of the string" do
136
- GoogleAPI.configure do |config|
137
- config.client_id = 'test id'
138
- config.client_secret = 'test secret'
139
- config.encryption_key = 'encryption key'
140
- end
141
-
142
- GoogleAPI.decrypt!("dGVzdGVuY3J5cHRpb24ga2V5\n").should == 'test'
143
- end
144
-
145
- end
146
-
147
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mime-types
@@ -127,6 +127,7 @@ files:
127
127
  - lib/google-api/active_record_inclusions.rb
128
128
  - lib/google-api/api.rb
129
129
  - lib/google-api/client.rb
130
+ - lib/google-api/encrypter.rb
130
131
  - lib/google-api/oauth2.rb
131
132
  - lib/google-api/railtie.rb
132
133
  - spec/fixtures/discovery_drive.json
@@ -136,6 +137,7 @@ files:
136
137
  - spec/fixtures/refresh_access_token.json
137
138
  - spec/google-api/api_spec.rb
138
139
  - spec/google-api/client_spec.rb
140
+ - spec/google-api/encrypter_spec.rb
139
141
  - spec/google-api_spec.rb
140
142
  - spec/spec_helper.rb
141
143
  - spec/support/faraday_stubs.rb
@@ -156,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
158
  version: '0'
157
159
  segments:
158
160
  - 0
159
- hash: -3705329295137939947
161
+ hash: 1675511776149438759
160
162
  required_rubygems_version: !ruby/object:Gem::Requirement
161
163
  none: false
162
164
  requirements:
@@ -165,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
167
  version: '0'
166
168
  segments:
167
169
  - 0
168
- hash: -3705329295137939947
170
+ hash: 1675511776149438759
169
171
  requirements: []
170
172
  rubyforge_project:
171
173
  rubygems_version: 1.8.24
@@ -180,6 +182,7 @@ test_files:
180
182
  - spec/fixtures/refresh_access_token.json
181
183
  - spec/google-api/api_spec.rb
182
184
  - spec/google-api/client_spec.rb
185
+ - spec/google-api/encrypter_spec.rb
183
186
  - spec/google-api_spec.rb
184
187
  - spec/spec_helper.rb
185
188
  - spec/support/faraday_stubs.rb