diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-01 03:09:48 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-01 03:09:48 +0300 |
commit | 0dc67904c1b8b01e7406fc510cf432c5bfc919fb (patch) | |
tree | 93fffcf04b7d5ef50291c9d6386e6dd5f199dcef | |
parent | 915ce2e09259d71275f3e0a6e5214b793c2eb52e (diff) |
Add latest changes from gitlab-org/gitlab@master
39 files changed, 1982 insertions, 146 deletions
diff --git a/.gitlab/ci/rules.gitlab-ci.yml b/.gitlab/ci/rules.gitlab-ci.yml index 6cc958e192d..6fc381fc7c2 100644 --- a/.gitlab/ci/rules.gitlab-ci.yml +++ b/.gitlab/ci/rules.gitlab-ci.yml @@ -1517,6 +1517,12 @@ changes: ["vendor/gems/omniauth-gitlab/**/*"] - <<: *if-merge-request-labels-run-all-rspec +.vendor:rules:omniauth-google-oauth2: + rules: + - <<: *if-merge-request + changes: ["vendor/gems/omniauth-google-oauth2/**/*"] + - <<: *if-merge-request-labels-run-all-rspec + .vendor:rules:omniauth-salesforce: rules: - <<: *if-merge-request diff --git a/.gitlab/ci/vendored-gems.gitlab-ci.yml b/.gitlab/ci/vendored-gems.gitlab-ci.yml index 03a6d1bcc0e..468c0e72ede 100644 --- a/.gitlab/ci/vendored-gems.gitlab-ci.yml +++ b/.gitlab/ci/vendored-gems.gitlab-ci.yml @@ -46,6 +46,14 @@ vendor omniauth-gitlab: include: vendor/gems/omniauth-gitlab/.gitlab-ci.yml strategy: depend +vendor omniauth-google-oauth2: + extends: + - .vendor:rules:omniauth-google-oauth2 + needs: [] + trigger: + include: vendor/gems/omniauth-google-oauth2/.gitlab-ci.yml + strategy: depend + vendor omniauth-salesforce: extends: - .vendor:rules:omniauth-salesforce diff --git a/.rubocop.yml b/.rubocop.yml index 914bc89dfda..7fe3fc35b66 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -566,9 +566,6 @@ Graphql/Descriptions: RSpec/ImplicitSubject: Enabled: false -RSpec/ReceiveNever: - Enabled: false - # Already covered by `RSpec::Configuration#on_potential_false_positives = :raise`. # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/86429 RSpec/UnspecifiedException: @@ -46,7 +46,7 @@ gem 'omniauth-alicloud', '~> 1.0.1' gem 'omniauth-facebook', '~> 4.0.0' gem 'omniauth-github', '~> 1.4' gem 'omniauth-gitlab', '~> 4.0.0', path: 'vendor/gems/omniauth-gitlab' # See vendor/gems/omniauth-gitlab/README.md -gem 'omniauth-google-oauth2', '~> 0.6.0' +gem 'omniauth-google-oauth2', '~> 0.6.0', path: 'vendor/gems/omniauth-google-oauth2' # See gem README.md gem 'omniauth-oauth2-generic', '~> 0.2.2' gem 'omniauth-saml', '~> 1.10' gem 'omniauth-shibboleth', '~> 1.3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5bfda48ca3a..9451f114bae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,6 +48,14 @@ PATH omniauth-oauth2 (~> 1.7.1) PATH + remote: vendor/gems/omniauth-google-oauth2 + specs: + omniauth-google-oauth2 (0.6.0) + jwt (>= 2.0) + omniauth (>= 1.9, < 3) + omniauth-oauth2 (>= 1.5) + +PATH remote: vendor/gems/omniauth-salesforce specs: omniauth-salesforce (1.0.5) @@ -925,10 +933,6 @@ GEM omniauth-github (1.4.0) omniauth (~> 1.5) omniauth-oauth2 (>= 1.4.0, < 2.0) - omniauth-google-oauth2 (0.6.0) - jwt (>= 2.0) - omniauth (>= 1.1.1) - omniauth-oauth2 (>= 1.5) omniauth-oauth (1.2.0) oauth omniauth (>= 1.0, < 3) @@ -1673,7 +1677,7 @@ DEPENDENCIES omniauth-facebook (~> 4.0.0) omniauth-github (~> 1.4) omniauth-gitlab (~> 4.0.0)! - omniauth-google-oauth2 (~> 0.6.0) + omniauth-google-oauth2 (~> 0.6.0)! omniauth-oauth2-generic (~> 0.2.2) omniauth-salesforce (~> 1.0.5)! omniauth-saml (~> 1.10) diff --git a/app/services/packages/debian/process_changes_service.rb b/app/services/packages/debian/process_changes_service.rb index b6e81012656..a29cbd3f65f 100644 --- a/app/services/packages/debian/process_changes_service.rb +++ b/app/services/packages/debian/process_changes_service.rb @@ -42,22 +42,30 @@ module Packages def update_files_metadata files.each do |filename, entry| - entry.package_file.package = package - file_metadata = ::Packages::Debian::ExtractMetadataService.new(entry.package_file).execute + ::Packages::UpdatePackageFileService.new(entry.package_file, package_id: package.id) + .execute + + # Force reload from database, as package has changed + entry.package_file.reload_package + entry.package_file.debian_file_metadatum.update!( file_type: file_metadata[:file_type], component: files[filename].component, architecture: file_metadata[:architecture], fields: file_metadata[:fields] ) - entry.package_file.save! end end def update_changes_metadata - package_file.update!(package: package) + ::Packages::UpdatePackageFileService.new(package_file, package_id: package.id) + .execute + + # Force reload from database, as package has changed + package_file.reload_package + package_file.debian_file_metadatum.update!( file_type: metadata[:file_type], fields: metadata[:fields] diff --git a/app/uploaders/packages/package_file_uploader.rb b/app/uploaders/packages/package_file_uploader.rb index 4b6dbe5b358..9c0a88c9bf8 100644 --- a/app/uploaders/packages/package_file_uploader.rb +++ b/app/uploaders/packages/package_file_uploader.rb @@ -22,8 +22,6 @@ class Packages::PackageFileUploader < GitlabUploader def dynamic_segment raise ObjectNotReadyError, "Package model not ready" unless model.id - package_segment = model.package.debian? ? 'debian' : model.package.id - - Gitlab::HashedPath.new('packages', package_segment, 'files', model.id, root_hash: model.package.project_id) + Gitlab::HashedPath.new('packages', model.package_id, 'files', model.id, root_hash: model.package.project_id) end end diff --git a/config/initializers/load_balancing.rb b/config/initializers/load_balancing.rb index 290481f7296..bcc62a6be5d 100644 --- a/config/initializers/load_balancing.rb +++ b/config/initializers/load_balancing.rb @@ -2,15 +2,6 @@ Gitlab::Application.configure do |config| config.middleware.use(Gitlab::Database::LoadBalancing::RackMiddleware) - - # We need re-rerun the setup when code reloads in development - config.reloader.to_prepare do - if Rails.env.development? || Rails.env.test? - Gitlab::Database::LoadBalancing.base_models.each do |model| - Gitlab::Database::LoadBalancing::Setup.new(model).setup - end - end - end end Gitlab::Database::LoadBalancing.base_models.each do |model| @@ -23,8 +14,9 @@ Gitlab::Database::LoadBalancing.base_models.each do |model| # information. Gitlab::Database::LoadBalancing::Setup.new(model).setup + # We need re-rerun the setup when code reloads in development Rails.application.reloader.to_prepare do - if Rails.env.development? + if Rails.env.development? || Rails.env.test? Gitlab::Database::LoadBalancing::Setup.new(model).setup end end diff --git a/doc/administration/auth/index.md b/doc/administration/auth/index.md index a412875501e..2644e3a1f50 100644 --- a/doc/administration/auth/index.md +++ b/doc/administration/auth/index.md @@ -34,31 +34,3 @@ For more information, see the links shown on this page for each external provide | **User Removal** | SCIM (remove user from top-level group) | LDAP (remove user from groups and block from the instance) | 1. Using Just-In-Time (JIT) provisioning, user accounts are created when the user first signs in. - -## Change apps or configuration - -When GitLab doesn't support having multiple providers (such as OAuth), GitLab configuration and user identification must be -updated at the same time if the provider or app is changed. - -These instructions apply to all methods of authentication where GitLab stores an `extern_uid` and it is the only data used -for user authentication. - -When changing apps within a provider, if the user `extern_uid` does not change, only the GitLab configuration must be -updated. - -To swap configurations: - -1. Change provider configuration in your `gitlab.rb` file. -1. Update `extern_uid` for all users that have an identity in GitLab for the previous provider. - -To find the `extern_uid`, look at an existing user's current `extern_uid` for an ID that matches the appropriate field in -your current provider for the same user. - -There are two methods to update the `extern_uid`: - -- Using the [Users API](../../api/users.md#user-modification). Pass the provider name and the new `extern_uid`. -- Using the [Rails console](../operations/rails_console.md): - - ```ruby - Identity.where(extern_uid: 'old-id').update!(extern_uid: 'new-id')` - ``` diff --git a/doc/integration/omniauth.md b/doc/integration/omniauth.md index af8c5cc37f0..b74203f61a9 100644 --- a/doc/integration/omniauth.md +++ b/doc/integration/omniauth.md @@ -437,6 +437,34 @@ then override the icon in one of two ways: } ``` +## Change apps or configuration + +Because GitLab doesn't support having multiple providers in OAuth, GitLab configuration and user identification must be +updated at the same time if the provider or app is changed. + +These instructions apply to all methods of authentication where GitLab stores an `extern_uid` and it is the only data used +for user authentication. + +When changing apps within a provider, if the user `extern_uid` does not change, only the GitLab configuration must be +updated. + +To swap configurations: + +1. Change provider configuration in your `gitlab.rb` file. +1. Update `extern_uid` for all users that have an identity in GitLab for the previous provider. + +To find the `extern_uid`, look at an existing user's current `extern_uid` for an ID that matches the appropriate field in +your current provider for the same user. + +There are two methods to update the `extern_uid`: + +- Using the [Users API](../api/users.md#user-modification). Pass the provider name and the new `extern_uid`. +- Using the [Rails console](../administration/operations/rails_console.md): + + ```ruby + Identity.where(extern_uid: 'old-id').update!(extern_uid: 'new-id')` + ``` + ## Limitations Most supported OmniAuth providers don't support Git over HTTP password authentication. diff --git a/qa/qa/specs/features/browser_ui/4_verify/pipeline/pipeline_with_image_pull_policy_spec.rb b/qa/qa/specs/features/browser_ui/4_verify/pipeline/pipeline_with_image_pull_policy_spec.rb index f852a028dc5..f9113573295 100644 --- a/qa/qa/specs/features/browser_ui/4_verify/pipeline/pipeline_with_image_pull_policy_spec.rb +++ b/qa/qa/specs/features/browser_ui/4_verify/pipeline/pipeline_with_image_pull_policy_spec.rb @@ -33,7 +33,10 @@ module QA runner.remove_via_api! end - context 'when policy is allowed' do + context( + 'when policy is allowed', + quarantine: { type: :flaky, issue: 'https://gitlab.com/gitlab-org/gitlab/-/issues/369397' } + ) do let(:allowed_policies) { %w[if-not-present always never] } where do diff --git a/scripts/verify-tff-mapping b/scripts/verify-tff-mapping index 9eb1d43c65b..b4974f71ebf 100755 --- a/scripts/verify-tff-mapping +++ b/scripts/verify-tff-mapping @@ -116,6 +116,18 @@ tests = [ explanation: 'Whats New should map to its respective spec', source: 'data/whats_new/202101140001_13_08.yml', expected: ['spec/lib/release_highlights/validator_spec.rb'] + }, + + { + explanation: 'Spec for every sidekiq worker', + source: 'app/workers/new_worker.rb', + expected: ['spec/workers/every_sidekiq_worker_spec.rb'] + }, + + { + explanation: 'Known events', + source: 'lib/gitlab/usage_data_counters/known_events/common.yml', + expected: ['spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb', 'spec/lib/gitlab/usage_data_spec.rb'] } ] diff --git a/spec/initializers/load_balancing_spec.rb b/spec/initializers/load_balancing_spec.rb index d7d0afd9159..d9162acd2cd 100644 --- a/spec/initializers/load_balancing_spec.rb +++ b/spec/initializers/load_balancing_spec.rb @@ -7,9 +7,12 @@ RSpec.describe 'load_balancing', :delete, :reestablished_active_record_base do load Rails.root.join('config/initializers/load_balancing.rb') end - context 'for a clustered puma worker' do - let!(:group) { create(:group, name: 'my group') } + before do + # Stub out middleware call, as not idempotent + allow(Gitlab::Application.instance.middleware).to receive(:use) + end + context 'with replica hosts configured' do before do # Setup host-based load balancing # Patch in our load balancer config, simply pointing at the test database twice @@ -18,12 +21,6 @@ RSpec.describe 'load_balancing', :delete, :reestablished_active_record_base do Gitlab::Database::LoadBalancing::Configuration.new(base_model, [db_host, db_host]) end - - # Pretend we are in clustered environment - allow(Gitlab::Cluster::LifecycleEvents).to receive(:in_clustered_puma?).and_return(true) - - # Stub out middleware call, as not idempotent - allow(Gitlab::Application.instance.middleware).to receive(:use) end after do @@ -34,51 +31,70 @@ RSpec.describe 'load_balancing', :delete, :reestablished_active_record_base do load Rails.root.join('config/initializers/load_balancing.rb') end - it 'configures load balancer to have two replica hosts' do + it 'configures load balancer with two replica hosts' do + expect(ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(0) + expect(Ci::ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(0) + initialize_load_balancer - simulate_puma_worker do - expect(ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) - expect(Ci::ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) - end + expect(ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) + expect(Ci::ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) end - # We tried using Process.fork for a more realistic simulation - # but run into bugs where GPRC cannot be used before forking processes. - # See https://gitlab.com/gitlab-org/gitlab/-/issues/333184#note_1081658113 - def simulate_puma_worker - # Called in https://github.com/rails/rails/blob/6-1-stable/activerecord/lib/active_record/connection_adapters/pool_config.rb#L73 - ActiveRecord::ConnectionAdapters::PoolConfig.discard_pools! + context 'for a clustered puma worker' do + let!(:group) { create(:group, name: 'my group') } - # Called in config/puma.rb - Gitlab::Cluster::LifecycleEvents.do_worker_start + before do + # Pretend we are in clustered environment + allow(Gitlab::Cluster::LifecycleEvents).to receive(:in_clustered_puma?).and_return(true) + end - yield - end + it 'configures load balancer to have two replica hosts' do + initialize_load_balancer + + simulate_puma_worker do + expect(ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) + expect(Ci::ApplicationRecord.connection.load_balancer.configuration.hosts.size).to eq(2) + end + end - it 'makes a read query successfully' do - # Clear any previous sticky writes - ::Gitlab::Database::LoadBalancing::Session.clear_session + # We tried using Process.fork for a more realistic simulation + # but run into bugs where GPRC cannot be used before forking processes. + # See https://gitlab.com/gitlab-org/gitlab/-/issues/333184#note_1081658113 + def simulate_puma_worker + # Called in https://github.com/rails/rails/blob/6-1-stable/activerecord/lib/active_record/connection_adapters/pool_config.rb#L73 + ActiveRecord::ConnectionAdapters::PoolConfig.discard_pools! - initialize_load_balancer + # Called in config/puma.rb + Gitlab::Cluster::LifecycleEvents.do_worker_start - group_name = simulate_puma_worker do - Group.find_by_name('my group').name + yield end - expect(group_name).to eq(group.name) - end + it 'makes a read query successfully' do + # Clear any previous sticky writes + ::Gitlab::Database::LoadBalancing::Session.clear_session - it 'makes a write query successfully' do - initialize_load_balancer + initialize_load_balancer - expect do - simulate_puma_worker do - Group.touch_all + group_name = simulate_puma_worker do + Group.find_by_name('my group').name end - group.reload - end.to change(group, :updated_at) + expect(group_name).to eq(group.name) + end + + it 'makes a write query successfully' do + initialize_load_balancer + + expect do + simulate_puma_worker do + Group.touch_all + end + + group.reload + end.to change(group, :updated_at) + end end end end diff --git a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb index ae0c0f53acd..1aedba63057 100644 --- a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb +++ b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb @@ -18,8 +18,8 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do shared_examples 'executing fallback' do it 'redirects to package registry' do - expect(helper).to receive(:registry_url).never - expect(helper).to receive(:redirect).never + expect(helper).not_to receive(:registry_url) + expect(helper).not_to receive(:redirect) expect(helper).to receive(:fallback).once subject @@ -30,7 +30,7 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do it 'redirects to package registry', :snowplow do expect(helper).to receive(:registry_url).once expect(helper).to receive(:redirect).once - expect(helper).to receive(:fallback).never + expect(helper).not_to receive(:fallback) subject diff --git a/spec/lib/api/helpers/packages_helpers_spec.rb b/spec/lib/api/helpers/packages_helpers_spec.rb index 69303a12d43..cd6e718ce98 100644 --- a/spec/lib/api/helpers/packages_helpers_spec.rb +++ b/spec/lib/api/helpers/packages_helpers_spec.rb @@ -80,7 +80,7 @@ RSpec.describe API::Helpers::PackagesHelpers do context 'with packages enabled' do it "doesn't call not_found!" do - expect(helper).to receive(:not_found!).never + expect(helper).not_to receive(:not_found!) expect(subject).to eq nil end diff --git a/spec/lib/banzai/renderer_spec.rb b/spec/lib/banzai/renderer_spec.rb index ae9cf4c5068..705f44baf16 100644 --- a/spec/lib/banzai/renderer_spec.rb +++ b/spec/lib/banzai/renderer_spec.rb @@ -76,7 +76,7 @@ RSpec.describe Banzai::Renderer do let(:object) { fake_object(fresh: true) } it 'uses the cache' do - expect(object).to receive(:refresh_markdown_cache!).never + expect(object).not_to receive(:refresh_markdown_cache!) is_expected.to eq('field_html') end diff --git a/spec/lib/gitlab/database/partitioning_spec.rb b/spec/lib/gitlab/database/partitioning_spec.rb index 36c8b0811fe..31c30ddfac7 100644 --- a/spec/lib/gitlab/database/partitioning_spec.rb +++ b/spec/lib/gitlab/database/partitioning_spec.rb @@ -56,7 +56,7 @@ RSpec.describe Gitlab::Database::Partitioning do end it 'does not call sync_partitions' do - expect(described_class).to receive(:sync_partitions).never + expect(described_class).not_to receive(:sync_partitions) described_class.sync_partitions_ignore_db_error end diff --git a/spec/lib/gitlab/import_export/file_importer_spec.rb b/spec/lib/gitlab/import_export/file_importer_spec.rb index 7b27f7183b0..5a75631ec4d 100644 --- a/spec/lib/gitlab/import_export/file_importer_spec.rb +++ b/spec/lib/gitlab/import_export/file_importer_spec.rb @@ -169,7 +169,7 @@ RSpec.describe Gitlab::ImportExport::FileImporter do end it 'skips validation' do - expect(subject).to receive(:validate_decompressed_archive_size).never + expect(subject).not_to receive(:validate_decompressed_archive_size) subject.import end diff --git a/spec/models/concerns/reactive_caching_spec.rb b/spec/models/concerns/reactive_caching_spec.rb index cb9bb676ede..039b9e574fe 100644 --- a/spec/models/concerns/reactive_caching_spec.rb +++ b/spec/models/concerns/reactive_caching_spec.rb @@ -281,7 +281,7 @@ RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do end it 'does not delete the value key' do - expect(Rails.cache).to receive(:delete).with(cache_key).never + expect(Rails.cache).not_to receive(:delete).with(cache_key) go! end @@ -338,7 +338,7 @@ RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do context 'when lifetime is exceeded' do it 'skips the calculation' do - expect(instance).to receive(:calculate_reactive_cache).never + expect(instance).not_to receive(:calculate_reactive_cache) go! end @@ -354,7 +354,7 @@ RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do it 'skips the calculation' do stub_exclusive_lease_taken(cache_key) - expect(instance).to receive(:calculate_reactive_cache).never + expect(instance).not_to receive(:calculate_reactive_cache) go! end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 331d8261dcf..d28c25d8f3d 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -116,7 +116,7 @@ RSpec.describe API::Users do end it "returns a 403 if the target user is an admin" do - expect(TwoFactor::DestroyService).to receive(:new).never + expect(TwoFactor::DestroyService).not_to receive(:new) expect do patch api("/users/#{admin_with_2fa.id}/disable_two_factor", admin) @@ -127,7 +127,7 @@ RSpec.describe API::Users do end it "returns a 404 if the target user cannot be found" do - expect(TwoFactor::DestroyService).to receive(:new).never + expect(TwoFactor::DestroyService).not_to receive(:new) patch api("/users/#{non_existing_record_id}/disable_two_factor", admin) diff --git a/spec/services/packages/debian/process_changes_service_spec.rb b/spec/services/packages/debian/process_changes_service_spec.rb index 52bcddb6f5e..a45dd68cd6e 100644 --- a/spec/services/packages/debian/process_changes_service_spec.rb +++ b/spec/services/packages/debian/process_changes_service_spec.rb @@ -5,7 +5,8 @@ RSpec.describe Packages::Debian::ProcessChangesService do describe '#execute' do let_it_be(:user) { create(:user) } let_it_be_with_reload(:distribution) { create(:debian_project_distribution, :with_file, codename: 'unstable') } - let_it_be(:incoming) { create(:debian_incoming, project: distribution.project) } + + let!(:incoming) { create(:debian_incoming, project: distribution.project) } let(:package_file) { incoming.package_files.last } @@ -33,10 +34,11 @@ RSpec.describe Packages::Debian::ProcessChangesService do existing_package.update!(debian_distribution: distribution) end - it 'does not create a package' do + it 'does not create a package and assigns the package_file to the existing package' do expect { subject.execute } .to not_change { Packages::Package.count } .and not_change { Packages::PackageFile.count } + .and change(package_file, :package).to(existing_package) end context 'marked as pending_destruction' do diff --git a/spec/uploaders/packages/package_file_uploader_spec.rb b/spec/uploaders/packages/package_file_uploader_spec.rb index e8f4cae7b04..0c7bf6432cb 100644 --- a/spec/uploaders/packages/package_file_uploader_spec.rb +++ b/spec/uploaders/packages/package_file_uploader_spec.rb @@ -2,50 +2,43 @@ require 'spec_helper' RSpec.describe Packages::PackageFileUploader do - { - package_file: %r[^\h{2}/\h{2}/\h{64}/packages/\d+/files/\d+$], - debian_package_file: %r[^\h{2}/\h{2}/\h{64}/packages/debian/files/\d+$] - }.each do |factory, store_dir_regex| - context factory.to_s do - let(:package_file) { create(factory) } # rubocop:disable Rails/SaveBang - let(:uploader) { described_class.new(package_file, :file) } - let(:path) { Gitlab.config.packages.storage_path } - - subject { uploader } - - it_behaves_like "builds correct paths", - store_dir: store_dir_regex, - cache_dir: %r[/packages/tmp/cache], - work_dir: %r[/packages/tmp/work] - - context 'object store is remote' do - before do - stub_package_file_object_storage - end - - include_context 'with storage', described_class::Store::REMOTE - - it_behaves_like "builds correct paths", - store_dir: store_dir_regex - end + let(:package_file) { create(:package_file) } + let(:uploader) { described_class.new(package_file, :file) } + let(:path) { Gitlab.config.packages.storage_path } + + subject { uploader } + + it_behaves_like "builds correct paths", + store_dir: %r[^\h{2}/\h{2}/\h{64}/packages/\d+/files/\d+$], + cache_dir: %r[/packages/tmp/cache], + work_dir: %r[/packages/tmp/work] + + context 'object store is remote' do + before do + stub_package_file_object_storage + end - describe 'remote file' do - let(:package_file) { create(factory, :object_storage) } + include_context 'with storage', described_class::Store::REMOTE - context 'with object storage enabled' do - before do - stub_package_file_object_storage - end + it_behaves_like "builds correct paths", + store_dir: %r[^\h{2}/\h{2}/\h{64}/packages/\d+/files/\d+$] + end + + describe 'remote file' do + let(:package_file) { create(:package_file, :object_storage) } + + context 'with object storage enabled' do + before do + stub_package_file_object_storage + end - it 'can store file remotely' do - allow(ObjectStorage::BackgroundMoveWorker).to receive(:perform_async) + it 'can store file remotely' do + allow(ObjectStorage::BackgroundMoveWorker).to receive(:perform_async) - package_file + package_file - expect(package_file.file_store).to eq(described_class::Store::REMOTE) - expect(package_file.file.path).not_to be_blank - end - end + expect(package_file.file_store).to eq(described_class::Store::REMOTE) + expect(package_file.file.path).not_to be_blank end end end diff --git a/tests.yml b/tests.yml index 3b1ac26a195..d38b05af991 100644 --- a/tests.yml +++ b/tests.yml @@ -60,3 +60,12 @@ mapping: # Whats New should map to its respective spec - source: data/whats_new/\w*.yml test: spec/lib/release_highlights/validator_spec.rb + + - source: (ee/)?app/workers/.+\.rb + test: spec/workers/every_sidekiq_worker_spec.rb + + - source: lib/gitlab/usage_data_counters/known_events/.+\.yml + test: spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb + - source: lib/gitlab/usage_data_counters/known_events/.+\.yml + test: spec/lib/gitlab/usage_data_spec.rb + diff --git a/vendor/gems/omniauth-google-oauth2/.gitlab-ci.yml b/vendor/gems/omniauth-google-oauth2/.gitlab-ci.yml new file mode 100644 index 00000000000..dbc4a7c16b1 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/.gitlab-ci.yml @@ -0,0 +1,28 @@ +workflow: + rules: + - if: $CI_MERGE_REQUEST_ID + +.rspec: + cache: + key: omniauth-google-oauth2 + paths: + - vendor/gems/omniauth-google-oauth2/vendor/ruby + before_script: + - cd vendor/gems/omniauth-google-oauth2 + - ruby -v # Print out ruby version for debugging + - gem install bundler --no-document # Bundler is not installed with the image + - bundle config set --local path 'vendor' # Install dependencies into ./vendor/ruby + - bundle config set with 'development' + - bundle config set --local frozen 'true' # Disallow Gemfile.lock changes on CI + - bundle config # Show bundler configuration + - bundle install -j $(nproc) + script: + - bundle exec rspec + +rspec-2.7: + image: "ruby:2.7" + extends: .rspec + +rspec-3.0: + image: "ruby:3.0" + extends: .rspec diff --git a/vendor/gems/omniauth-google-oauth2/CHANGELOG.md b/vendor/gems/omniauth-google-oauth2/CHANGELOG.md new file mode 100644 index 00000000000..7d3e1234f2d --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/CHANGELOG.md @@ -0,0 +1,253 @@ +# Changelog +All notable changes to this project will be documented in this file. + +## 0.6.0 - 2018-12-28 + +### Added +- Support for JWT 2.x. + +### Deprecated +- Nothing. + +### Removed +- Support for JWT 1.x. +- Support for `raw_friend_info` and `raw_image_info`. + +### Fixed +- Nothing. + +## 0.5.4 - 2018-12-07 + +### Added +- New recommended endpoints for Google OAuth. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Nothing. + +## 0.5.3 - 2018-01-25 + +### Added +- Added support for the JWT 2.x gem. +- Now fully qualifies the `JWT` class to prevent conflicts with the `Omniauth::JWT` strategy. + +### Deprecated +- Nothing. + +### Removed +- Removed the `multijson` dependency. +- Support for versions of `omniauth-oauth2` < 1.5. + +### Fixed +- Nothing. + +## 0.5.2 - 2017-07-30 + +### Added +- Nothing. + +### Deprecated +- Nothing. + +### Removed +- New `authorize_url` and `token_url` endpoints are reverted until JWT 2.0 ships. + +### Fixed +- Nothing. + +## 0.5.1 - 2017-07-19 + +### Added +- *Breaking* JWT iss verification can be enabled/disabled with the `verify_iss` flag - see the README for more details. +- Authorize options now includes `device_id` and `device_name` for private ip ranges. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Updated `authorize_url` and `token_url` to new endpoints. + +## 0.5.0 - 2017-05-29 + +### Added +- Rubocop checks to specs. +- Defaulted dev environment to ruby 2.3.4. + +### Deprecated +- Nothing. + +### Removed +- Testing support for older versions of ruby not supported by OmniAuth 1.5. +- Key `[:urls]['Google']` no longer exists, it has been renamed to `[:urls][:google]`. + +### Fixed +- Updated all code to rubocop conventions. This includes the Ruby 1.9 hash syntax when appropriate. +- Example javascript flow now picks up ENV vars for google key and secret. + +## 0.4.1 - 2016-03-14 + +### Added +- Nothing. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Fixed JWT iat leeway by requiring ruby-jwt 1.5.2 + +## 0.4.0 - 2016-03-11 + +### Added +- Addedd ability to specify multiple hosted domains. +- Added a default leeway of 1 minute to JWT token validation. +- Now requires ruby-jwt 1.5.x. + +### Deprecated +- Nothing. + +### Removed +- Removed support for ruby 1.9.3 as ruby-jwt 1.5.x does not support it. + +### Fixed +- Nothing. + +## 0.3.1 - 2016-01-28 + +### Added +- Verify Hosted Domain if hd is set in options. + +### Deprecated +- Nothing. + +### Removed +- Dependency on addressable. + +### Fixed +- Nothing. + +## 0.3.0 - 2016-01-09 + +### Added +- Updated verify_token to use the v3 tokeninfo endpoint. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Compatibility with omniauth-oauth2 1.4.0 + +## 0.2.10 - 2015-11-05 + +### Added +- Nothing. + +### Deprecated +- Nothing. + +### Removed +- Removed some checks on the id_token. Now only parses the id_token in the JWT processing. + +### Fixed +- Nothing. + +## 0.2.9 - 2015-10-29 + +### Added +- Nothing. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Issue with omniauth-oauth2 where redirect_uri was handled improperly. We now lock the dependency to ~> 1.3.1 + +## 0.2.8 - 2015-10-01 + +### Added +- Added skip_jwt option to bypass JWT decoding in case you get decoding errors. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Resolved JWT::InvalidIatError. https://github.com/zquestz/omniauth-google-oauth2/issues/195 + +## 0.2.7 - 2015-09-25 + +### Added +- Now strips out the 'sz' parameter from profile image urls. +- Now uses 'addressable' gem for URI actions. +- Added image data to extras hash. +- Override validation on JWT token for open_id token. +- Handle authorization codes coming from an installed applications. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Fixes double slashes in google image urls. + +## 0.2.6 - 2014-10-26 + +### Added +- Nothing. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Hybrid authorization issues due to bad method alias. + +## 0.2.5 - 2014-07-09 + +### Added +- Support for versions of omniauth past 1.0.x. + +### Deprecated +- Nothing. + +### Removed +- Nothing. + +### Fixed +- Nothing. + +## 0.2.4 - 2014-04-25 + +### Added +- Now requiring the "Contacts API" and "Google+ API" to be enabled in your Google API console. + +### Deprecated +- The old Google OAuth API support was removed without deprecation. + +### Removed +- Support for the old Google OAuth API. `OAuth2::Error` will be thrown and state that access is not configured when you attempt to authenticate using the old API. See Added section for this release. + +### Fixed +- Nothing. diff --git a/vendor/gems/omniauth-google-oauth2/Gemfile b/vendor/gems/omniauth-google-oauth2/Gemfile new file mode 100644 index 00000000000..7f4f5e950d1 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gemspec diff --git a/vendor/gems/omniauth-google-oauth2/Gemfile.lock b/vendor/gems/omniauth-google-oauth2/Gemfile.lock new file mode 100644 index 00000000000..a7f71e0a376 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/Gemfile.lock @@ -0,0 +1,83 @@ +PATH + remote: . + specs: + omniauth-google-oauth2 (0.6.0) + jwt (>= 2.0) + omniauth (>= 1.9, < 3) + omniauth-oauth2 (>= 1.5) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + diff-lcs (1.5.0) + faraday (2.5.2) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.0) + hashie (5.0.0) + jwt (2.4.1) + multi_xml (0.6.0) + oauth2 (2.0.3) + faraday (>= 0.17.3, < 3.0) + jwt (>= 1.0, < 3.0) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + rash_alt (>= 0.4, < 1) + version_gem (~> 1.0) + omniauth (1.9.2) + hashie (>= 3.4.6) + rack (>= 1.6.2, < 3) + omniauth-oauth2 (1.7.3) + oauth2 (>= 1.4, < 3) + omniauth (>= 1.9, < 3) + parallel (1.22.1) + parser (3.1.2.0) + ast (~> 2.4.1) + rack (2.2.4) + rainbow (3.1.1) + rake (12.3.3) + rash_alt (0.4.12) + hashie (>= 3.4) + regexp_parser (2.5.0) + rexml (3.2.5) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) + rubocop (0.93.1) + parallel (~> 1.10) + parser (>= 2.7.1.5) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8) + rexml + rubocop-ast (>= 0.6.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + rubocop-ast (1.19.1) + parser (>= 3.1.1.0) + ruby-progressbar (1.11.0) + ruby2_keywords (0.0.5) + unicode-display_width (1.8.0) + version_gem (1.1.0) + +PLATFORMS + ruby + +DEPENDENCIES + omniauth-google-oauth2! + rake (~> 12.0) + rspec (~> 3.6) + rubocop (~> 0.49) + +BUNDLED WITH + 2.3.21 diff --git a/vendor/gems/omniauth-google-oauth2/README.md b/vendor/gems/omniauth-google-oauth2/README.md new file mode 100644 index 00000000000..5d811693a60 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/README.md @@ -0,0 +1,300 @@ +[![Gem Version](https://badge.fury.io/rb/omniauth-google-oauth2.svg)](https://badge.fury.io/rb/omniauth-google-oauth2) +[![Build Status](https://travis-ci.org/zquestz/omniauth-google-oauth2.svg)](https://travis-ci.org/zquestz/omniauth-google-oauth2) + +# OmniAuth Google OAuth2 Strategy + +Strategy to authenticate with Google via OAuth2 in OmniAuth. + +Get your API key at: https://code.google.com/apis/console/ Note the Client ID and the Client Secret. + +For more details, read the Google docs: https://developers.google.com/accounts/docs/OAuth2 + +## Installation + +Add to your `Gemfile`: + +```ruby +gem 'omniauth-google-oauth2' +``` + +Then `bundle install`. + +## Google API Setup + +* Go to 'https://console.developers.google.com' +* Select your project. +* Go to Credentials, then select the "OAuth consent screen" tab on top, and provide an 'EMAIL ADDRESS' and a 'PRODUCT NAME' +* Wait 10 minutes for changes to take effect. + +## Usage + +Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`: + +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'] +end +``` + +You can now access the OmniAuth Google OAuth2 URL: `/auth/google_oauth2` + +For more examples please check out `examples/omni_auth.rb` + +NOTE: While developing your application, if you change the scope in the initializer you will need to restart your app server. Remember that either the 'email' or 'profile' scope is required! + +## Configuration + +You can configure several options, which you pass in to the `provider` method via a hash: + +* `scope`: A comma-separated list of permissions you want to request from the user. See the [Google OAuth 2.0 Playground](https://developers.google.com/oauthplayground/) for a full list of available permissions. Caveats: + * The `email` and `profile` scopes are used by default. By defining your own `scope`, you override these defaults, but Google requires at least one of `email` or `profile`, so make sure to add at least one of them to your scope! + * Scopes starting with `https://www.googleapis.com/auth/` do not need that prefix specified. So while you can use the smaller scope `books` since that permission starts with the mentioned prefix, you should use the full scope URL `https://docs.google.com/feeds/` to access a user's docs, for example. + +* `redirect_uri`: Override the redirect_uri used by the gem. + +* `prompt`: A space-delimited list of string values that determines whether the user is re-prompted for authentication and/or consent. Possible values are: + * `none`: No authentication or consent pages will be displayed; it will return an error if the user is not already authenticated and has not pre-configured consent for the requested scopes. This can be used as a method to check for existing authentication and/or consent. + * `consent`: The user will always be prompted for consent, even if he has previously allowed access a given set of scopes. + * `select_account`: The user will always be prompted to select a user account. This allows a user who has multiple current account sessions to select one amongst them. + + 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. + +* `image_aspect_ratio`: The shape of the user's profile picture. Possible values are: + * `original`: Picture maintains its original aspect ratio. + * `square`: Picture presents equal width and height. + + Defaults to `original`. + +* `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 requested 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. + +* `name`: The name of the strategy. The default name is `google_oauth2` but it can be changed to any value, for example `google`. The OmniAuth URL will thus change to `/auth/google` and the `provider` key in the auth hash will then return `google`. + +* `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`. More about [offline access](https://developers.google.com/identity/protocols/OAuth2WebServer#offline) + +* `hd`: (Optional) Limit sign-in to a particular Google Apps hosted domain. This can be simply string `'domain.com'` or an array `%w(domain.com domain.co)`. More information at: https://developers.google.com/accounts/docs/OpenIDConnect#hd-param + +* `jwt_leeway`: Number of seconds passed to the JWT library as leeway. Defaults to 60 seconds. + +* `skip_jwt`: Skip JWT processing. This is for users who are seeing JWT decoding errors with the `iat` field. Always try adjusting the leeway before disabling JWT processing. + +* `login_hint`: When your app knows which user it is trying to authenticate, it can provide this parameter as a hint to the authentication server. Passing this hint suppresses the account chooser and either pre-fill the email box on the sign-in form, or select the proper session (if the user is using multiple sign-in), which can help you avoid problems that occur if your app logs in the wrong user account. The value can be either an email address or the sub string, which is equivalent to the user's Google+ ID. + +* `include_granted_scopes`: If this is provided with the value true, and the authorization request is granted, the authorization will include any previous authorizations granted to this user/application combination for other scopes. See Google's [Incremental Authorization](https://developers.google.com/accounts/docs/OAuth2WebServer#incrementalAuth) for additional details. + +* `openid_realm`: Set the OpenID realm value, to allow upgrading from OpenID based authentication to OAuth 2 based authentication. When this is set correctly an `openid_id` value will be set in `[:extra][:id_info]` in the authentication hash with the value of the user's OpenID ID URL. + +Here's an example of a possible configuration where the strategy name is changed, 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: + +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], + { + scope: 'userinfo.email, userinfo.profile, http://gdata.youtube.com', + prompt: 'select_account', + image_aspect_ratio: 'square', + image_size: 50 + } +end +``` + +## Auth Hash + +Here's an example of an authentication hash available in the callback by accessing `request.env['omniauth.auth']`: + +```ruby +{ + "provider" => "google_oauth2", + "uid" => "100000000000000000000", + "info" => { + "name" => "John Smith", + "email" => "john@example.com", + "first_name" => "John", + "last_name" => "Smith", + "image" => "https://lh4.googleusercontent.com/photo.jpg", + "urls" => { + "google" => "https://plus.google.com/+JohnSmith" + } + }, + "credentials" => { + "token" => "TOKEN", + "refresh_token" => "REFRESH_TOKEN", + "expires_at" => 1496120719, + "expires" => true + }, + "extra" => { + "id_token" => "ID_TOKEN", + "id_info" => { + "azp" => "APP_ID", + "aud" => "APP_ID", + "sub" => "100000000000000000000", + "email" => "john@example.com", + "email_verified" => true, + "at_hash" => "HK6E_P6Dh8Y93mRNtsDB1Q", + "iss" => "accounts.google.com", + "iat" => 1496117119, + "exp" => 1496120719 + }, + "raw_info" => { + "sub" => "100000000000000000000", + "name" => "John Smith", + "given_name" => "John", + "family_name" => "Smith", + "profile" => "https://plus.google.com/+JohnSmith", + "picture" => "https://lh4.googleusercontent.com/photo.jpg?sz=50", + "email" => "john@example.com", + "email_verified" => "true", + "locale" => "en", + "hd" => "company.com" + } + } +} +``` + +### Devise + +First define your application id and secret in `config/initializers/devise.rb`. Do not use the snippet mentioned in the [Usage](https://github.com/zquestz/omniauth-google-oauth2#usage) section. + +Configuration options can be passed as the last parameter here as key/value pairs. + +```ruby +config.omniauth :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {} +``` +NOTE: If you are using this gem with devise with above snippet in `config/initializers/devise.rb` then do not create `config/initializers/omniauth.rb` which will conflict with devise configurations. + +Then add the following to 'config/routes.rb' so the callback routes are defined. + +```ruby +devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } +``` + +Make sure your model is omniauthable. Generally this is "/app/models/user.rb" + +```ruby +devise :omniauthable, omniauth_providers: [:google_oauth2] +``` + +Then make sure your callbacks controller is setup. + +```ruby +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def google_oauth2 + # You need to implement the method below in your model (e.g. app/models/user.rb) + @user = User.from_omniauth(request.env['omniauth.auth']) + + if @user.persisted? + flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google' + sign_in_and_redirect @user, event: :authentication + else + session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores + redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n") + end + end +end +``` + +and bind to or create the user + +```ruby +def self.from_omniauth(access_token) + data = access_token.info + user = User.where(email: data['email']).first + + # Uncomment the section below if you want users to be created if they don't exist + # unless user + # user = User.create(name: data['name'], + # email: data['email'], + # password: Devise.friendly_token[0,20] + # ) + # end + user +end +``` + +For your views you can login using: + +```erb +<%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path %> + +<%# Devise prior 4.1.0: %> +<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %> +``` + +An overview is available at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview + +### One-time Code Flow (Hybrid Authentication) + +Google describes the One-time Code Flow [here](https://developers.google.com/+/web/signin/server-side-flow). This hybrid authentication flow has significant functional and security advantages over a pure server-side or pure client-side flow. The following steps occur in this flow: + +1. The client (web browser) authenticates the user directly via Google's JS API. During this process assorted modals may be rendered by Google. +2. On successful authentication, Google returns a one-time use code, which requires the Google client secret (which is only available server-side). +3. Using a AJAX request, the code is POSTed to the Omniauth Google OAuth2 callback. +4. The Omniauth Google OAuth2 gem will validate the code via a server-side request to Google. If the code is valid, then Google will return an access token and, if this is the first time this user is authenticating against this application, a refresh token. Both of these should be stored on the server. The response to the AJAX request indicates the success or failure of this process. + +This flow is immune to replay attacks, and conveys no useful information to a man in the middle. + +The omniauth-google-oauth2 gem supports this mode of operation out of the box. Implementors simply need to add the appropriate JavaScript to their web page, and they can take advantage of this flow. An example JavaScript snippet follows. + +```javascript +// Basic hybrid auth example following the pattern at: +// https://developers.google.com/identity/sign-in/web/reference + +<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script> + +... + +function init() { + gapi.load('auth2', function() { + // Ready. + $('.google-login-button').click(function(e) { + e.preventDefault(); + + gapi.auth2.authorize({ + client_id: 'YOUR_CLIENT_ID', + cookie_policy: 'single_host_origin', + scope: 'email profile', + response_type: 'code' + }, function(response) { + if (response && !response.error) { + // google authentication succeed, now post data to server. + jQuery.ajax({type: 'POST', url: '/auth/google_oauth2/callback', data: response, + success: function(data) { + // response from server + } + }); + } else { + // google authentication failed + } + }); + }); + }); +}; +``` + +#### Note about mobile clients (iOS, Android) + +The documentation at https://developers.google.com/identity/sign-in/ios/offline-access specifies the _REDIRECT_URI_ to be either a set value or an EMPTY string for mobile logins to work. Else, you will run into _redirect_uri_mismatch_ errors. + +In that case, ensure to send an additional parameter `redirect_uri=` (empty string) to the `/auth/google_oauth2/callback` URL from your mobile device. + +#### Note about CORS + +If you're making POST requests to `/auth/google_oauth2/callback` from another domain, then you need to make sure `'X-Requested-With': 'XMLHttpRequest'` header is included with your request, otherwise your server might respond with `OAuth2::Error, : Invalid Value` error. + +## Fixing Protocol Mismatch for `redirect_uri` in Rails + +Just set the `full_host` in OmniAuth based on the Rails.env. + +``` +# config/initializers/omniauth.rb +OmniAuth.config.full_host = Rails.env.production? ? 'https://domain.com' : 'http://localhost:3000' +``` + +## License + +Copyright (c) 2018 by Josh Ellithorpe + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/gems/omniauth-google-oauth2/Rakefile b/vendor/gems/omniauth-google-oauth2/Rakefile new file mode 100644 index 00000000000..db36369bc12 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require File.join('bundler', 'gem_tasks') +require File.join('rspec', 'core', 'rake_task') + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/vendor/gems/omniauth-google-oauth2/examples/Gemfile b/vendor/gems/omniauth-google-oauth2/examples/Gemfile new file mode 100644 index 00000000000..cd02ff9bf7d --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/examples/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'omniauth-google-oauth2', '~> 0.5' +gem 'rubocop' +gem 'sinatra', '~> 1.4' diff --git a/vendor/gems/omniauth-google-oauth2/examples/config.ru b/vendor/gems/omniauth-google-oauth2/examples/config.ru new file mode 100644 index 00000000000..ee17929094c --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/examples/config.ru @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +# Sample app for Google OAuth2 Strategy +# Make sure to setup the ENV variables GOOGLE_KEY and GOOGLE_SECRET +# Run with "bundle exec rackup" + +require 'rubygems' +require 'bundler' +require 'sinatra' +require 'omniauth' +require 'omniauth-google-oauth2' + +# Do not use for production code. +# This is only to make setup easier when running through the sample. +# +# If you do have issues with certs in production code, this could help: +# http://railsapps.github.io/openssl-certificate-verify-failed.html +OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE + +# Main example app for omniauth-google-oauth2 +class App < Sinatra::Base + get '/' do + <<-HTML + <!DOCTYPE html> + <html> + <head> + <title>Google OAuth2 Example</title> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> + <script> + jQuery(function() { + return $.ajax({ + url: 'https://apis.google.com/js/client:plus.js?onload=gpAsyncInit', + dataType: 'script', + cache: true + }); + }); + + window.gpAsyncInit = function() { + gapi.auth.authorize({ + immediate: true, + response_type: 'code', + cookie_policy: 'single_host_origin', + client_id: '#{ENV['GOOGLE_KEY']}', + scope: 'email profile' + }, function(response) { + return; + }); + $('.googleplus-login').click(function(e) { + e.preventDefault(); + gapi.auth.authorize({ + immediate: false, + response_type: 'code', + cookie_policy: 'single_host_origin', + client_id: '#{ENV['GOOGLE_KEY']}', + scope: 'email profile' + }, function(response) { + if (response && !response.error) { + // google authentication succeed, now post data to server. + jQuery.ajax({type: 'POST', url: "/auth/google_oauth2/callback", data: response, + success: function(data) { + // Log the data returning from google. + console.log(data) + } + }); + } else { + // google authentication failed. + console.log("FAILED") + } + }); + }); + }; + </script> + </head> + <body> + <ul> + <li><a href='/auth/google_oauth2'>Sign in with Google</a></li> + <li><a href='#' class="googleplus-login">Sign in with Google via AJAX</a></li> + </ul> + </body> + </html> + HTML + end + + post '/auth/:provider/callback' do + content_type 'text/plain' + begin + request.env['omniauth.auth'].to_hash.inspect + rescue StandardError + 'No Data' + end + end + + get '/auth/:provider/callback' do + content_type 'text/plain' + begin + request.env['omniauth.auth'].to_hash.inspect + rescue StandardError + 'No Data' + end + end + + get '/auth/failure' do + content_type 'text/plain' + begin + request.env['omniauth.auth'].to_hash.inspect + rescue StandardError + 'No Data' + end + end +end + +use Rack::Session::Cookie, secret: ENV['RACK_COOKIE_SECRET'] + +use OmniAuth::Builder do + # For additional provider examples please look at 'omni_auth.rb' + # The key provider_ignores_state is only for AJAX flows. It is not recommended for normal logins. + provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], access_type: 'offline', prompt: 'consent', provider_ignores_state: true, scope: 'email,profile,calendar' +end + +run App.new diff --git a/vendor/gems/omniauth-google-oauth2/examples/omni_auth.rb b/vendor/gems/omniauth-google-oauth2/examples/omni_auth.rb new file mode 100644 index 00000000000..127b62a9aaf --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/examples/omni_auth.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Google's OAuth2 docs. Make sure you are familiar with all the options +# before attempting to configure this gem. +# https://developers.google.com/accounts/docs/OAuth2Login + +Rails.application.config.middleware.use OmniAuth::Builder do + # Default usage, this will give you offline access and a refresh token + # using default scopes 'email' and 'profile' + # + provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], scope: 'email,profile' + + # Manual setup for offline access with a refresh token. + # + # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], access_type: 'offline' + + # Custom scope supporting youtube. If you are customizing scopes, remember + # to include the default scopes 'email' and 'profile' + # + # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], scope: 'http://gdata.youtube.com,email,profile,plus.me' + + # Custom scope for users only using Google for account creation/auth and do not require a refresh token. + # + # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], access_type: 'online', prompt: '' + + # To include information about people in your circles you must include the 'plus.login' scope. + # + # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], skip_friends: false, scope: 'email,profile,plus.login' + + # If you need to acquire whether user picture is a default one or uploaded by user. + # + # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], skip_image_info: false +end diff --git a/vendor/gems/omniauth-google-oauth2/lib/omniauth-google-oauth2.rb b/vendor/gems/omniauth-google-oauth2/lib/omniauth-google-oauth2.rb new file mode 100644 index 00000000000..a5cc57ab4c9 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/lib/omniauth-google-oauth2.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require 'omniauth/google_oauth2' diff --git a/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2.rb b/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2.rb new file mode 100644 index 00000000000..594c9a4eed9 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require 'omniauth/strategies/google_oauth2' diff --git a/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2/version.rb b/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2/version.rb new file mode 100644 index 00000000000..565fb5d6642 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/lib/omniauth/google_oauth2/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module OmniAuth + module GoogleOauth2 + VERSION = '0.6.0' + end +end diff --git a/vendor/gems/omniauth-google-oauth2/lib/omniauth/strategies/google_oauth2.rb b/vendor/gems/omniauth-google-oauth2/lib/omniauth/strategies/google_oauth2.rb new file mode 100644 index 00000000000..62e2d4462e5 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/lib/omniauth/strategies/google_oauth2.rb @@ -0,0 +1,212 @@ +# frozen_string_literal: true + +require 'jwt' +require 'omniauth/strategies/oauth2' +require 'uri' + +module OmniAuth + module Strategies + # Main class for Google OAuth2 strategy. + class GoogleOauth2 < OmniAuth::Strategies::OAuth2 + ALLOWED_ISSUERS = ['accounts.google.com', 'https://accounts.google.com'].freeze + BASE_SCOPE_URL = 'https://www.googleapis.com/auth/' + BASE_SCOPES = %w[profile email openid].freeze + DEFAULT_SCOPE = 'email,profile' + USER_INFO_URL = 'https://www.googleapis.com/oauth2/v3/userinfo' + + option :name, 'google_oauth2' + option :skip_friends, true + option :skip_image_info, true + option :skip_jwt, false + option :jwt_leeway, 60 + option :authorize_options, %i[access_type hd login_hint prompt request_visible_actions scope state redirect_uri include_granted_scopes openid_realm device_id device_name] + option :authorized_client_ids, [] + + option :client_options, + site: 'https://oauth2.googleapis.com', + authorize_url: 'https://accounts.google.com/o/oauth2/auth', + token_url: '/token' + + def authorize_params + super.tap do |params| + options[:authorize_options].each do |k| + params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s]) + end + + params[:scope] = get_scope(params) + params[:access_type] = 'offline' if params[:access_type].nil? + params['openid.realm'] = params.delete(:openid_realm) unless params[:openid_realm].nil? + + session['omniauth.state'] = params[:state] if params[:state] + end + end + + uid { raw_info['sub'] } + + info do + prune!( + name: raw_info['name'], + email: verified_email, + first_name: raw_info['given_name'], + last_name: raw_info['family_name'], + image: image_url, + urls: { + google: raw_info['profile'] + } + ) + end + + extra do + hash = {} + hash[:id_token] = access_token['id_token'] + if !options[:skip_jwt] && !access_token['id_token'].nil? + decoded = ::JWT.decode(access_token['id_token'], nil, false).first + + # We have to manually verify the claims because the third parameter to + # JWT.decode is false since no verification key is provided. + ::JWT::Verify.verify_claims(decoded, + verify_iss: true, + iss: ALLOWED_ISSUERS, + verify_aud: true, + aud: options.client_id, + verify_sub: false, + verify_expiration: true, + verify_not_before: true, + verify_iat: true, + verify_jti: false, + leeway: options[:jwt_leeway]) + + hash[:id_info] = decoded + end + hash[:raw_info] = raw_info unless skip_info? + prune! hash + end + + def raw_info + @raw_info ||= access_token.get(USER_INFO_URL).parsed + end + + def custom_build_access_token + access_token = get_access_token(request) + + verify_hd(access_token) + access_token + end + alias build_access_token custom_build_access_token + + private + + def callback_url + options[:redirect_uri] || (full_host + script_name + callback_path) + end + + def get_access_token(request) + if request.xhr? && request.params['code'] + verifier = request.params['code'] + redirect_uri = request.params['redirect_uri'] || 'postmessage' + client.auth_code.get_token(verifier, get_token_options(redirect_uri), deep_symbolize(options.auth_token_params || {})) + elsif request.params['code'] && request.params['redirect_uri'] + verifier = request.params['code'] + redirect_uri = request.params['redirect_uri'] + client.auth_code.get_token(verifier, get_token_options(redirect_uri), deep_symbolize(options.auth_token_params || {})) + elsif verify_token(request.params['access_token']) + ::OAuth2::AccessToken.from_hash(client, request.params.dup) + else + verifier = request.params['code'] + client.auth_code.get_token(verifier, get_token_options(callback_url), deep_symbolize(options.auth_token_params)) + end + end + + def get_scope(params) + raw_scope = params[:scope] || DEFAULT_SCOPE + scope_list = raw_scope.split(' ').map { |item| item.split(',') }.flatten + scope_list.map! { |s| s =~ %r{^https?://} || BASE_SCOPES.include?(s) ? s : "#{BASE_SCOPE_URL}#{s}" } + scope_list.join(' ') + end + + def get_token_options(redirect_uri) + { redirect_uri: redirect_uri }.merge(token_params.to_hash(symbolize_keys: true)) + end + + def prune!(hash) + hash.delete_if do |_, v| + prune!(v) if v.is_a?(Hash) + v.nil? || (v.respond_to?(:empty?) && v.empty?) + end + end + + def verified_email + raw_info['email_verified'] ? raw_info['email'] : nil + end + + def image_url + return nil unless raw_info['picture'] + + u = URI.parse(raw_info['picture'].gsub('https:https', 'https')) + + path_index = u.path.to_s.index('/photo.jpg') + + if path_index && image_size_opts_passed? + u.path.insert(path_index, image_params) + u.path = u.path.gsub('//', '/') + end + + u.query = strip_unnecessary_query_parameters(u.query) + + u.to_s + end + + def image_size_opts_passed? + options[:image_size] || options[:image_aspect_ratio] + end + + def image_params + image_params = [] + if options[:image_size].is_a?(Integer) + image_params << "s#{options[:image_size]}" + elsif options[:image_size].is_a?(Hash) + image_params << "w#{options[:image_size][:width]}" if options[:image_size][:width] + image_params << "h#{options[:image_size][:height]}" if options[:image_size][:height] + end + image_params << 'c' if options[:image_aspect_ratio] == 'square' + + '/' + image_params.join('-') + end + + def strip_unnecessary_query_parameters(query_parameters) + # strip `sz` parameter (defaults to sz=50) which overrides `image_size` options + return nil if query_parameters.nil? + + params = CGI.parse(query_parameters) + stripped_params = params.delete_if { |key| key == 'sz' } + + # don't return an empty Hash since that would result + # in URLs with a trailing ? character: http://image.url? + return nil if stripped_params.empty? + + URI.encode_www_form(stripped_params) + end + + def verify_token(access_token) + return false unless access_token + + raw_response = client.request(:get, 'https://www.googleapis.com/oauth2/v3/tokeninfo', + params: { access_token: access_token }).parsed + raw_response['aud'] == options.client_id || options.authorized_client_ids.include?(raw_response['aud']) + end + + def verify_hd(access_token) + return true unless options.hd + + @raw_info ||= access_token.get(USER_INFO_URL).parsed + + options.hd = options.hd.call if options.hd.is_a? Proc + allowed_hosted_domains = Array(options.hd) + + raise CallbackError.new(:invalid_hd, 'Invalid Hosted Domain') unless allowed_hosted_domains.include?(@raw_info['hd']) || options.hd == '*' + + true + end + end + end +end diff --git a/vendor/gems/omniauth-google-oauth2/omniauth-google-oauth2.gemspec b/vendor/gems/omniauth-google-oauth2/omniauth-google-oauth2.gemspec new file mode 100644 index 00000000000..fb66d7ee282 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/omniauth-google-oauth2.gemspec @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require File.expand_path( + File.join('..', 'lib', 'omniauth', 'google_oauth2', 'version'), + __FILE__ +) + +Gem::Specification.new do |gem| + gem.name = 'omniauth-google-oauth2' + gem.version = OmniAuth::GoogleOauth2::VERSION + gem.license = 'MIT' + gem.summary = %(A Google OAuth2 strategy for OmniAuth 1.x) + gem.description = %(A Google OAuth2 strategy for OmniAuth 1.x. This allows you to login to Google with your ruby app.) + gem.authors = ['Josh Ellithorpe', 'Yury Korolev'] + gem.email = ['quest@mac.com'] + gem.homepage = 'https://github.com/zquestz/omniauth-google-oauth2' + + gem.files = Dir.glob("lib/**/*.*") + gem.require_paths = ['lib'] + + gem.required_ruby_version = '>= 2.1' + + gem.add_runtime_dependency 'jwt', '>= 2.0' + gem.add_runtime_dependency 'omniauth', '>= 1.9', '< 3' + gem.add_runtime_dependency 'omniauth-oauth2', '>= 1.5' + + gem.add_development_dependency 'rake', '~> 12.0' + gem.add_development_dependency 'rspec', '~> 3.6' + gem.add_development_dependency 'rubocop', '~> 0.49' +end diff --git a/vendor/gems/omniauth-google-oauth2/spec/omniauth/strategies/google_oauth2_spec.rb b/vendor/gems/omniauth-google-oauth2/spec/omniauth/strategies/google_oauth2_spec.rb new file mode 100644 index 00000000000..6bc6f6d9e95 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/spec/omniauth/strategies/google_oauth2_spec.rb @@ -0,0 +1,692 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'json' +require 'omniauth-google-oauth2' + +describe OmniAuth::Strategies::GoogleOauth2 do + let(:request) { double('Request', params: {}, cookies: {}, env: {}) } + let(:app) do + lambda do + [200, {}, ['Hello.']] + end + end + + subject do + OmniAuth::Strategies::GoogleOauth2.new(app, 'appid', 'secret', @options || {}).tap do |strategy| + allow(strategy).to receive(:request) do + request + end + end + end + + before do + OmniAuth.config.test_mode = true + end + + after do + OmniAuth.config.test_mode = false + end + + describe '#client_options' do + it 'has correct site' do + expect(subject.client.site).to eq('https://oauth2.googleapis.com') + end + + it 'has correct authorize_url' do + expect(subject.client.options[:authorize_url]).to eq('https://accounts.google.com/o/oauth2/auth') + end + + it 'has correct token_url' do + expect(subject.client.options[:token_url]).to eq('/token') + end + + describe 'overrides' do + context 'as strings' do + it 'should allow overriding the site' do + @options = { client_options: { 'site' => 'https://example.com' } } + expect(subject.client.site).to eq('https://example.com') + end + + it 'should allow overriding the authorize_url' do + @options = { client_options: { 'authorize_url' => 'https://example.com' } } + expect(subject.client.options[:authorize_url]).to eq('https://example.com') + end + + it 'should allow overriding the token_url' do + @options = { client_options: { 'token_url' => 'https://example.com' } } + expect(subject.client.options[:token_url]).to eq('https://example.com') + end + end + + context 'as symbols' do + it 'should allow overriding the site' do + @options = { client_options: { site: 'https://example.com' } } + expect(subject.client.site).to eq('https://example.com') + end + + it 'should allow overriding the authorize_url' do + @options = { client_options: { authorize_url: 'https://example.com' } } + expect(subject.client.options[:authorize_url]).to eq('https://example.com') + end + + it 'should allow overriding the token_url' do + @options = { client_options: { token_url: 'https://example.com' } } + expect(subject.client.options[:token_url]).to eq('https://example.com') + end + end + end + end + + describe '#authorize_options' do + %i[access_type hd login_hint prompt scope state device_id device_name].each do |k| + it "should support #{k}" do + @options = { k => 'http://someval' } + expect(subject.authorize_params[k.to_s]).to eq('http://someval') + end + end + + describe 'redirect_uri' do + it 'should default to nil' do + @options = {} + expect(subject.authorize_params['redirect_uri']).to eq(nil) + end + + it 'should set the redirect_uri parameter if present' do + @options = { redirect_uri: 'https://example.com' } + expect(subject.authorize_params['redirect_uri']).to eq('https://example.com') + end + end + + describe 'access_type' do + it 'should default to "offline"' do + @options = {} + expect(subject.authorize_params['access_type']).to eq('offline') + end + + it 'should set the access_type parameter if present' do + @options = { access_type: 'online' } + expect(subject.authorize_params['access_type']).to eq('online') + end + end + + describe 'hd' do + it 'should default to nil' do + expect(subject.authorize_params['hd']).to eq(nil) + end + + it 'should set the hd (hosted domain) parameter if present' do + @options = { hd: 'example.com' } + expect(subject.authorize_params['hd']).to eq('example.com') + end + + it 'should set the hd parameter and work with nil hd (gmail)' do + @options = { hd: nil } + expect(subject.authorize_params['hd']).to eq(nil) + end + + it 'should set the hd parameter to * if set (only allows G Suite emails)' do + @options = { hd: '*' } + expect(subject.authorize_params['hd']).to eq('*') + end + end + + describe 'login_hint' do + it 'should default to nil' do + expect(subject.authorize_params['login_hint']).to eq(nil) + end + + it 'should set the login_hint parameter if present' do + @options = { login_hint: 'john@example.com' } + expect(subject.authorize_params['login_hint']).to eq('john@example.com') + end + end + + describe 'prompt' do + it 'should default to nil' do + expect(subject.authorize_params['prompt']).to eq(nil) + end + + it 'should set the prompt parameter if present' do + @options = { prompt: 'consent select_account' } + expect(subject.authorize_params['prompt']).to eq('consent select_account') + end + end + + describe 'request_visible_actions' do + it 'should default to nil' do + expect(subject.authorize_params['request_visible_actions']).to eq(nil) + end + + it 'should set the request_visible_actions parameter if present' do + @options = { request_visible_actions: 'something' } + expect(subject.authorize_params['request_visible_actions']).to eq('something') + end + end + + describe 'include_granted_scopes' do + it 'should default to nil' do + expect(subject.authorize_params['include_granted_scopes']).to eq(nil) + end + + it 'should set the include_granted_scopes parameter if present' do + @options = { include_granted_scopes: 'true' } + expect(subject.authorize_params['include_granted_scopes']).to eq('true') + end + end + + describe 'scope' do + it 'should expand scope shortcuts' do + @options = { scope: 'plus.me' } + expect(subject.authorize_params['scope']).to eq('https://www.googleapis.com/auth/plus.me') + end + + it 'should leave base scopes as is' do + @options = { scope: 'profile' } + expect(subject.authorize_params['scope']).to eq('profile') + end + + it 'should join scopes' do + @options = { scope: 'profile,email' } + expect(subject.authorize_params['scope']).to eq('profile email') + end + + it 'should deal with whitespace when joining scopes' do + @options = { scope: 'profile, email' } + expect(subject.authorize_params['scope']).to eq('profile email') + end + + it 'should set default scope to email,profile' do + expect(subject.authorize_params['scope']).to eq('email profile') + end + + it 'should support space delimited scopes' do + @options = { scope: 'profile email' } + expect(subject.authorize_params['scope']).to eq('profile email') + end + + it 'should support extremely badly formed scopes' do + @options = { scope: 'profile email,foo,steve yeah http://example.com' } + expect(subject.authorize_params['scope']).to eq('profile email https://www.googleapis.com/auth/foo https://www.googleapis.com/auth/steve https://www.googleapis.com/auth/yeah http://example.com') + end + end + + describe 'state' do + it 'should set the state parameter' do + @options = { state: 'some_state' } + expect(subject.authorize_params['state']).to eq('some_state') + expect(subject.authorize_params[:state]).to eq('some_state') + expect(subject.session['omniauth.state']).to eq('some_state') + end + + it 'should set the omniauth.state dynamically' do + allow(subject).to receive(:request) { double('Request', params: { 'state' => 'some_state' }, env: {}) } + expect(subject.authorize_params['state']).to eq('some_state') + expect(subject.authorize_params[:state]).to eq('some_state') + expect(subject.session['omniauth.state']).to eq('some_state') + end + end + + describe 'overrides' do + it 'should include top-level options that are marked as :authorize_options' do + @options = { authorize_options: %i[scope foo request_visible_actions], scope: 'http://bar', foo: 'baz', hd: 'wow', request_visible_actions: 'something' } + expect(subject.authorize_params['scope']).to eq('http://bar') + expect(subject.authorize_params['foo']).to eq('baz') + expect(subject.authorize_params['hd']).to eq(nil) + expect(subject.authorize_params['request_visible_actions']).to eq('something') + end + + describe 'request overrides' do + %i[access_type hd login_hint prompt scope state].each do |k| + context "authorize option #{k}" do + let(:request) { double('Request', params: { k.to_s => 'http://example.com' }, cookies: {}, env: {}) } + + it "should set the #{k} authorize option dynamically in the request" do + @options = { k: '' } + expect(subject.authorize_params[k.to_s]).to eq('http://example.com') + end + end + end + + describe 'custom authorize_options' do + let(:request) { double('Request', params: { 'foo' => 'something' }, cookies: {}, env: {}) } + + it 'should support request overrides from custom authorize_options' do + @options = { authorize_options: [:foo], foo: '' } + expect(subject.authorize_params['foo']).to eq('something') + end + end + end + end + end + + describe '#authorize_params' do + it 'should include any authorize params passed in the :authorize_params option' do + @options = { authorize_params: { request_visible_actions: 'something', foo: 'bar', baz: 'zip' }, hd: 'wow', bad: 'not_included' } + expect(subject.authorize_params['request_visible_actions']).to eq('something') + expect(subject.authorize_params['foo']).to eq('bar') + expect(subject.authorize_params['baz']).to eq('zip') + expect(subject.authorize_params['hd']).to eq('wow') + expect(subject.authorize_params['bad']).to eq(nil) + end + end + + describe '#token_params' do + it 'should include any token params passed in the :token_params option' do + @options = { token_params: { foo: 'bar', baz: 'zip' } } + expect(subject.token_params['foo']).to eq('bar') + expect(subject.token_params['baz']).to eq('zip') + end + end + + describe '#token_options' do + it 'should include top-level options that are marked as :token_options' do + @options = { token_options: %i[scope foo], scope: 'bar', foo: 'baz', bad: 'not_included' } + expect(subject.token_params['scope']).to eq('bar') + expect(subject.token_params['foo']).to eq('baz') + expect(subject.token_params['bad']).to eq(nil) + end + end + + describe '#callback_path' do + it 'has the correct default callback path' do + expect(subject.callback_path).to eq('/auth/google_oauth2/callback') + end + + it 'should set the callback_path parameter if present' do + @options = { callback_path: '/auth/foo/callback' } + expect(subject.callback_path).to eq('/auth/foo/callback') + end + end + + describe '#extra' do + let(:client) do + OAuth2::Client.new('abc', 'def') do |builder| + builder.request :url_encoded + builder.adapter :test do |stub| + stub.get('/oauth2/v3/userinfo') { [200, { 'content-type' => 'application/json' }, '{"sub": "12345"}'] } + end + end + end + let(:access_token) { OAuth2::AccessToken.from_hash(client, {}) } + + before { allow(subject).to receive(:access_token).and_return(access_token) } + + describe 'id_token' do + shared_examples 'id_token issued by valid issuer' do |issuer| # rubocop:disable Metrics/BlockLength + context 'when the id_token is passed into the access token' do + let(:token_info) do + { + 'abc' => 'xyz', + 'exp' => Time.now.to_i + 3600, + 'nbf' => Time.now.to_i - 60, + 'iat' => Time.now.to_i, + 'aud' => 'appid', + 'iss' => issuer + } + end + let(:id_token) { JWT.encode(token_info, 'secret') } + let(:access_token) { OAuth2::AccessToken.from_hash(client, 'id_token' => id_token) } + + it 'should include id_token when set on the access_token' do + expect(subject.extra).to include(id_token: id_token) + end + + it 'should include id_info when id_token is set on the access_token and skip_jwt is false' do + subject.options[:skip_jwt] = false + expect(subject.extra).to include(id_info: token_info) + end + + it 'should not include id_info when id_token is set on the access_token and skip_jwt is true' do + subject.options[:skip_jwt] = true + expect(subject.extra).not_to have_key(:id_info) + end + + it 'should include id_info when id_token is set on the access_token by default' do + expect(subject.extra).to include(id_info: token_info) + end + end + end + + it_behaves_like 'id_token issued by valid issuer', 'accounts.google.com' + it_behaves_like 'id_token issued by valid issuer', 'https://accounts.google.com' + + context 'when the id_token is issued by an invalid issuer' do + let(:token_info) do + { + 'abc' => 'xyz', + 'exp' => Time.now.to_i + 3600, + 'nbf' => Time.now.to_i - 60, + 'iat' => Time.now.to_i, + 'aud' => 'appid', + 'iss' => 'fake.google.com' + } + end + let(:id_token) { JWT.encode(token_info, 'secret') } + let(:access_token) { OAuth2::AccessToken.from_hash(client, 'id_token' => id_token) } + + it 'raises JWT::InvalidIssuerError' do + expect { subject.extra }.to raise_error(JWT::InvalidIssuerError) + end + end + + context 'when the id_token is missing' do + it 'should not include id_token' do + expect(subject.extra).not_to have_key(:id_token) + end + + it 'should not include id_info' do + expect(subject.extra).not_to have_key(:id_info) + end + end + end + + describe 'raw_info' do + context 'when skip_info is true' do + before { subject.options[:skip_info] = true } + + it 'should not include raw_info' do + expect(subject.extra).not_to have_key(:raw_info) + end + end + + context 'when skip_info is false' do + before { subject.options[:skip_info] = false } + + it 'should include raw_info' do + expect(subject.extra[:raw_info]).to eq('sub' => '12345') + end + end + end + end + + describe 'populate auth hash urls' do + it 'should populate url map in auth hash if link present in raw_info' do + allow(subject).to receive(:raw_info) { { 'name' => 'Foo', 'profile' => 'https://plus.google.com/123456' } } + expect(subject.info[:urls][:google]).to eq('https://plus.google.com/123456') + end + + it 'should not populate url map in auth hash if no link present in raw_info' do + allow(subject).to receive(:raw_info) { { 'name' => 'Foo' } } + expect(subject.info).not_to have_key(:urls) + end + end + + describe 'image options' do + it 'should have no image if a picture is not present' do + @options = { image_aspect_ratio: 'square' } + allow(subject).to receive(:raw_info) { { 'name' => 'User Without Pic' } } + expect(subject.info[:image]).to be_nil + end + + describe 'when a picture is returned from google' do + it 'should return the image with size specified in the `image_size` option' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg') + end + + it 'should handle a picture with too many slashes correctly' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url//photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg') + end + + it 'should handle a picture with a size query parameter correctly' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg?sz=50' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg') + end + + it 'should handle a picture with a size query parameter and other valid query parameters correctly' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg?sz=50&hello=true&life=42' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg?hello=true&life=42') + end + + it 'should handle a picture with other valid query parameters correctly' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg?hello=true&life=42' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50/photo.jpg?hello=true&life=42') + end + + it 'should return the image with width and height specified in the `image_size` option' do + @options = { image_size: { width: 50, height: 40 } } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40/photo.jpg') + end + + it 'should return square image when `image_aspect_ratio` is specified' do + @options = { image_aspect_ratio: 'square' } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/c/photo.jpg') + end + + it 'should return square sized image when `image_aspect_ratio` and `image_size` is set' do + @options = { image_aspect_ratio: 'square', image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/s50-c/photo.jpg') + end + + it 'should return square sized image when `image_aspect_ratio` and `image_size` has height and width' do + @options = { image_aspect_ratio: 'square', image_size: { width: 50, height: 40 } } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/w50-h40-c/photo.jpg') + end + + it 'should return original image if image url does not end in `photo.jpg`' do + @options = { image_size: 50 } + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photograph.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/photograph.jpg') + end + end + + it 'should return original image if no options are provided' do + allow(subject).to receive(:raw_info) { { 'picture' => 'https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/photo.jpg') + end + + it 'should return correct image if google image url has double https' do + allow(subject).to receive(:raw_info) { { 'picture' => 'https:https://lh3.googleusercontent.com/url/photo.jpg' } } + expect(subject.info[:image]).to eq('https://lh3.googleusercontent.com/url/photo.jpg') + end + end + + describe 'build_access_token' do + it 'should use a hybrid authorization request_uri if this is an AJAX request with a code parameter' do + allow(request).to receive(:xhr?).and_return(true) + allow(request).to receive(:params).and_return('code' => 'valid_code') + + client = double(:client) + auth_code = double(:auth_code) + allow(client).to receive(:auth_code).and_return(auth_code) + expect(subject).to receive(:client).and_return(client) + expect(auth_code).to receive(:get_token).with('valid_code', { redirect_uri: 'postmessage' }, {}) + + expect(subject).not_to receive(:orig_build_access_token) + subject.build_access_token + end + + it 'should use a hybrid authorization request_uri if this is an AJAX request (mobile) with a code parameter' do + allow(request).to receive(:xhr?).and_return(true) + allow(request).to receive(:params).and_return('code' => 'valid_code', 'redirect_uri' => '') + + client = double(:client) + auth_code = double(:auth_code) + allow(client).to receive(:auth_code).and_return(auth_code) + expect(subject).to receive(:client).and_return(client) + expect(auth_code).to receive(:get_token).with('valid_code', { redirect_uri: '' }, {}) + + expect(subject).not_to receive(:orig_build_access_token) + subject.build_access_token + end + + it 'should use the request_uri from params if this not an AJAX request (request from installed app) with a code parameter' do + allow(request).to receive(:xhr?).and_return(false) + allow(request).to receive(:params).and_return('code' => 'valid_code', 'redirect_uri' => 'redirect_uri') + + client = double(:client) + auth_code = double(:auth_code) + allow(client).to receive(:auth_code).and_return(auth_code) + expect(subject).to receive(:client).and_return(client) + expect(auth_code).to receive(:get_token).with('valid_code', { redirect_uri: 'redirect_uri' }, {}) + + expect(subject).not_to receive(:orig_build_access_token) + subject.build_access_token + end + + it 'should read access_token from hash if this is not an AJAX request with a code parameter' do + allow(request).to receive(:xhr?).and_return(false) + allow(request).to receive(:params).and_return('access_token' => 'valid_access_token') + expect(subject).to receive(:verify_token).with('valid_access_token').and_return true + expect(subject).to receive(:client).and_return(:client) + + token = subject.build_access_token + expect(token).to be_instance_of(::OAuth2::AccessToken) + expect(token.token).to eq('valid_access_token') + expect(token.client).to eq(:client) + end + + it 'should use callback_url without query_string if this is not an AJAX request' do + allow(request).to receive(:xhr?).and_return(false) + allow(request).to receive(:params).and_return('code' => 'valid_code') + + client = double(:client) + auth_code = double(:auth_code) + allow(client).to receive(:auth_code).and_return(auth_code) + allow(subject).to receive(:callback_url).and_return('redirect_uri_without_query_string') + + expect(subject).to receive(:client).and_return(client) + expect(auth_code).to receive(:get_token).with('valid_code', { redirect_uri: 'redirect_uri_without_query_string' }, {}) + subject.build_access_token + end + end + + describe 'verify_token' do + before(:each) do + subject.options.client_options[:connection_build] = proc do |builder| + builder.request :url_encoded + builder.adapter :test do |stub| + stub.get('/oauth2/v3/tokeninfo?access_token=valid_access_token') do + [200, { 'Content-Type' => 'application/json; charset=UTF-8' }, JSON.dump( + aud: '000000000000.apps.googleusercontent.com', + sub: '123456789', + email_verified: 'true', + email: 'example@example.com', + access_type: 'offline', + scope: 'profile email', + expires_in: 436 + )] + end + stub.get('/oauth2/v3/tokeninfo?access_token=invalid_access_token') do + [400, { 'Content-Type' => 'application/json; charset=UTF-8' }, JSON.dump(error_description: 'Invalid Value')] + end + end + end + end + + it 'should verify token if access_token is valid and app_id equals' do + subject.options.client_id = '000000000000.apps.googleusercontent.com' + expect(subject.send(:verify_token, 'valid_access_token')).to eq(true) + end + + it 'should verify token if access_token is valid and app_id authorized' do + subject.options.authorized_client_ids = ['000000000000.apps.googleusercontent.com'] + expect(subject.send(:verify_token, 'valid_access_token')).to eq(true) + end + + it 'should not verify token if access_token is valid but app_id is false' do + expect(subject.send(:verify_token, 'valid_access_token')).to eq(false) + end + + it 'should raise error if access_token is invalid' do + expect do + subject.send(:verify_token, 'invalid_access_token') + end.to raise_error(OAuth2::Error) + end + end + + describe 'verify_hd' do + let(:client) do + OAuth2::Client.new('abc', 'def') do |builder| + builder.request :url_encoded + builder.adapter :test do |stub| + stub.get('/oauth2/v3/userinfo') do + [200, { 'Content-Type' => 'application/json; charset=UTF-8' }, JSON.dump( + hd: 'example.com' + )] + end + end + end + end + let(:access_token) { OAuth2::AccessToken.from_hash(client, {}) } + + context 'when domain is nil' do + let(:client) do + OAuth2::Client.new('abc', 'def') do |builder| + builder.request :url_encoded + builder.adapter :test do |stub| + stub.get('/oauth2/v3/userinfo') do + [200, { 'Content-Type' => 'application/json; charset=UTF-8' }, JSON.dump({})] + end + end + end + end + + it 'should verify hd if options hd is set and correct' do + subject.options.hd = nil + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should verify hd if options hd is set as an array and is correct' do + subject.options.hd = ['example.com', 'example.co', nil] + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should raise an exception if nil is not included' do + subject.options.hd = ['example.com', 'example.co'] + expect do + subject.send(:verify_hd, access_token) + end.to raise_error(OmniAuth::Strategies::OAuth2::CallbackError) + end + end + + it 'should verify hd if options hd is not set' do + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should verify hd if options hd is set and correct' do + subject.options.hd = 'example.com' + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should verify hd if options hd is set as an array and is correct' do + subject.options.hd = ['example.com', 'example.co', nil] + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should verify hd if options hd is set as an Proc and is correct' do + subject.options.hd = proc { 'example.com' } + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should verify hd if options hd is set as an Proc returning an array and is correct' do + subject.options.hd = proc { ['example.com', 'example.co'] } + expect(subject.send(:verify_hd, access_token)).to eq(true) + end + + it 'should raise error if options hd is set and wrong' do + subject.options.hd = 'invalid.com' + expect do + subject.send(:verify_hd, access_token) + end.to raise_error(OmniAuth::Strategies::GoogleOauth2::CallbackError) + end + + it 'should raise error if options hd is set as an array and is not correct' do + subject.options.hd = ['invalid.com', 'invalid.co'] + expect do + subject.send(:verify_hd, access_token) + end.to raise_error(OmniAuth::Strategies::GoogleOauth2::CallbackError) + end + end +end diff --git a/vendor/gems/omniauth-google-oauth2/spec/spec_helper.rb b/vendor/gems/omniauth-google-oauth2/spec/spec_helper.rb new file mode 100644 index 00000000000..3c2325389c2 --- /dev/null +++ b/vendor/gems/omniauth-google-oauth2/spec/spec_helper.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require File.join('bundler', 'setup') +require 'rspec' |