Skip to content

Commit da8fba2

Browse files
authored
Update image sizing code to handle already sized images coming from Google (#394)
1 parent 109155a commit da8fba2

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Metrics/ClassLength:
33
Metrics/AbcSize:
44
Enabled: false
55
Metrics/BlockLength:
6-
ExcludedMethods: ['describe', 'context']
6+
ExcludedMethods: ['describe', 'context', 'shared_examples']
77
Metrics/CyclomaticComplexity:
88
Enabled: false
99
Metrics/LineLength:

lib/omniauth/strategies/google_oauth2.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class GoogleOauth2 < OmniAuth::Strategies::OAuth2
1414
BASE_SCOPES = %w[profile email openid].freeze
1515
DEFAULT_SCOPE = 'email,profile'
1616
USER_INFO_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'
17+
IMAGE_SIZE_REGEXP = /(s\d+(-c)?)|(w\d+-h\d+(-c)?)|(w\d+(-c)?)|(h\d+(-c)?)|c/
1718

1819
option :name, 'google_oauth2'
1920
option :skip_friends, true
@@ -171,6 +172,10 @@ def image_url
171172
if path_index && image_size_opts_passed?
172173
u.path.insert(path_index, image_params)
173174
u.path = u.path.gsub('//', '/')
175+
176+
# Check if the image is already sized!
177+
split_path = u.path.split('/')
178+
u.path = u.path.sub("/#{split_path[-3]}", '') if split_path[-3] =~ IMAGE_SIZE_REGEXP
174179
end
175180

176181
u.query = strip_unnecessary_query_parameters(u.query)

spec/omniauth/strategies/google_oauth2_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
before { allow(subject).to receive(:access_token).and_return(access_token) }
350350

351351
describe 'id_token' do
352-
shared_examples 'id_token issued by valid issuer' do |issuer| # rubocop:disable Metrics/BlockLength
352+
shared_examples 'id_token issued by valid issuer' do |issuer|
353353
context 'when the id_token is passed into the access token' do
354354
let(:token_info) do
355355
{
@@ -462,6 +462,12 @@
462462
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg')
463463
end
464464

465+
it 'should return the image with size specified in the `image_size` option when sizing is in the picture' do
466+
@options = { image_size: 50 }
467+
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh4.googleusercontent.com/url/s96-c/photo.jpg' } }
468+
expect(subject.info[:image]).to eq('https://lh4.googleusercontent.com/url/s50/photo.jpg')
469+
end
470+
465471
it 'should handle a picture with too many slashes correctly' do
466472
@options = { image_size: 50 }
467473
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url//photo.jpg' } }
@@ -492,24 +498,48 @@
492498
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40/photo.jpg')
493499
end
494500

501+
it 'should return the image with width and height specified in the `image_size` option when sizing is in the picture' do
502+
@options = { image_size: { width: 50, height: 40 } }
503+
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/w100-h80-c/photo.jpg' } }
504+
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40/photo.jpg')
505+
end
506+
495507
it 'should return square image when `image_aspect_ratio` is specified' do
496508
@options = { image_aspect_ratio: 'square' }
497509
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
498510
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/c/photo.jpg')
499511
end
500512

513+
it 'should return square image when `image_aspect_ratio` is specified and sizing is in the picture' do
514+
@options = { image_aspect_ratio: 'square' }
515+
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/c/photo.jpg' } }
516+
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/c/photo.jpg')
517+
end
518+
501519
it 'should return square sized image when `image_aspect_ratio` and `image_size` is set' do
502520
@options = { image_aspect_ratio: 'square', image_size: 50 }
503521
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
504522
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50-c/photo.jpg')
505523
end
506524

525+
it 'should return square sized image when `image_aspect_ratio` and `image_size` is set and sizing is in the picture' do
526+
@options = { image_aspect_ratio: 'square', image_size: 50 }
527+
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/s90/photo.jpg' } }
528+
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50-c/photo.jpg')
529+
end
530+
507531
it 'should return square sized image when `image_aspect_ratio` and `image_size` has height and width' do
508532
@options = { image_aspect_ratio: 'square', image_size: { width: 50, height: 40 } }
509533
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } }
510534
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40-c/photo.jpg')
511535
end
512536

537+
it 'should return square sized image when `image_aspect_ratio` and `image_size` has height and width and sizing is in the picture' do
538+
@options = { image_aspect_ratio: 'square', image_size: { width: 50, height: 40 } }
539+
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/w100-h80/photo.jpg' } }
540+
expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40-c/photo.jpg')
541+
end
542+
513543
it 'should return original image if image url does not end in `photo.jpg`' do
514544
@options = { image_size: 50 }
515545
allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photograph.jpg' } }

0 commit comments

Comments
 (0)