omniauth-google-oauth2 0.1.18 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,17 +42,27 @@ You can configure several options, which you pass in to the `provider` method vi
42
42
 
43
43
  If no value is specified, the user only sees the authentication page if he is not logged in and only sees the consent page the first time he authorizes a given set of scopes.
44
44
 
45
+ * `image_aspect_ratio`: The shape of the user's profile picture. Possible values are:
46
+ * `original`: Picture maintains its original aspect ratio.
47
+ * `square`: Picture presents equal width and height.
48
+
49
+ Defaults to `original`.
50
+
51
+ * `image_size`: The size of the user's profile picture. The image returned will have width equal to the given value and variable height, according to the `image_aspect_ratio` chosen. Additionally, a picture with specific width and height can be request by setting this option to a hash with `:width` and `:height` as keys. If only `:width` or `:height` is specified, a picture whose width or height is closest to the requested size and requested aspect ratio will be returned. Defaults to the original width and height of the picture.
52
+
45
53
  * `access_type`: Defaults to `offline`, so a refresh token is sent to be used when the user is not present at the browser. Can be set to `online`.
46
54
 
47
- Here's an example of a possible configuration where the user is asked for extra permissions and is always prompted to select his account when logging in:
55
+ Here's an example of a possible configuration where the user is asked for extra permissions, the user is always prompted to select his account when logging in and the user's profile picture is returned as a thumbnail:
48
56
 
49
57
  ```ruby
50
58
  Rails.application.config.middleware.use OmniAuth::Builder do
51
59
  provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"],
52
- {
53
- :scope => "userinfo.email, userinfo.profile, plus.me, http://gdata.youtube.com",
54
- :prompt => "consent select_account"
55
- }
60
+ {
61
+ :scope => "userinfo.email, userinfo.profile, plus.me, http://gdata.youtube.com",
62
+ :prompt => "select_account",
63
+ :image_aspect_ratio => "square",
64
+ :image_size => 50
65
+ }
56
66
  end
57
67
  ```
58
68
 
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module GoogleOauth2
3
- VERSION = "0.1.18"
3
+ VERSION = "0.1.19"
4
4
  end
5
5
  end
@@ -43,7 +43,7 @@ module OmniAuth
43
43
  :email => verified_email,
44
44
  :first_name => raw_info['given_name'],
45
45
  :last_name => raw_info['family_name'],
46
- :image => raw_info['picture'],
46
+ :image => image_url(options),
47
47
  :urls => {
48
48
  'Google' => raw_info['link']
49
49
  }
@@ -85,6 +85,23 @@ module OmniAuth
85
85
  raw_info['verified_email'] ? raw_info['email'] : nil
86
86
  end
87
87
 
88
+ def image_url(options)
89
+ original_url = raw_info['picture']
90
+ return original_url if original_url.nil? || (!options[:image_size] && !options[:image_aspect_ratio])
91
+
92
+ image_params = []
93
+ if options[:image_size].is_a?(Integer)
94
+ image_params << "s#{options[:image_size]}"
95
+ elsif options[:image_size].is_a?(Hash)
96
+ image_params << "w#{options[:image_size][:width]}" if options[:image_size][:width]
97
+ image_params << "h#{options[:image_size][:height]}" if options[:image_size][:height]
98
+ end
99
+ image_params << 'c' if options[:image_aspect_ratio] == 'square'
100
+
101
+ params_index = original_url.index('/photo.jpg')
102
+ original_url.insert(params_index, '/'+image_params.join('-'))
103
+ end
104
+
88
105
  def verify_token(id_token, access_token)
89
106
  # Verify id_token as well
90
107
  # request fails and raises error when id_token or access_token is invalid
@@ -159,6 +159,48 @@ describe OmniAuth::Strategies::GoogleOauth2 do
159
159
  end
160
160
  end
161
161
 
162
+ describe 'image options' do
163
+ it 'should return the image with size specified in the `image_size` option' do
164
+ @options = { :image_size => 50 }
165
+ subject.stub(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
166
+ main_url, image_params = subject.info[:image].match(/^(.*)\/(.*)\/photo.jpg/).captures
167
+ main_url.should eq('https://lh3.googleusercontent.com/url')
168
+ image_params.should eq('s50')
169
+ end
170
+
171
+ it 'should return the image with width and height specified in the `image_size` option' do
172
+ @options = { :image_size => { :width => 50, :height => 50 } }
173
+ subject.stub(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
174
+ main_url, image_params = subject.info[:image].match(/^(.*)\/(.*)\/photo.jpg/).captures
175
+ image_params = image_params.split('-').inject({}) do |result, element|
176
+ result[element.slice!(0)] = element
177
+ result
178
+ end
179
+ main_url.should eq('https://lh3.googleusercontent.com/url')
180
+ image_params['w'].should eq('50')
181
+ image_params['h'].should eq('50')
182
+ end
183
+
184
+ it 'should return square image when `image_aspect_ratio` is specified' do
185
+ @options = { :image_aspect_ratio => 'square' }
186
+ subject.stub(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
187
+ main_url, image_params = subject.info[:image].match(/^(.*)\/(.*)\/photo.jpg/).captures
188
+ main_url.should eq('https://lh3.googleusercontent.com/url')
189
+ image_params.should eq('c')
190
+ end
191
+
192
+ it 'should not break if no picture present in raw_info' do
193
+ @options = { :image_aspect_ratio => 'square' }
194
+ subject.stub(:raw_info) { { 'name' => 'User Without Pic' } }
195
+ subject.info[:image].should be_nil
196
+ end
197
+
198
+ it 'should return original image if no options are provided' do
199
+ subject.stub(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
200
+ subject.info[:image].should eq('https://lh3.googleusercontent.com/url/photo.jpg')
201
+ end
202
+ end
203
+
162
204
  describe 'build_access_token' do
163
205
  it 'should read access_token from hash' do
164
206
  @request.stub(:params).and_return('id_token' => 'valid_id_token', 'access_token' => 'valid_access_token')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-google-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-17 00:00:00.000000000 Z
13
+ date: 2013-06-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: omniauth
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  segments:
114
114
  - 0
115
- hash: 465443751449155842
115
+ hash: 3969644160829709281
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  segments:
123
123
  - 0
124
- hash: 465443751449155842
124
+ hash: 3969644160829709281
125
125
  requirements: []
126
126
  rubyforge_project:
127
127
  rubygems_version: 1.8.25