Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use case insensitive login matching for oauth remapping

Markus Kahlbab7d02068ba
diff --git a/app/services/authentication/omniauth_service.rb b/app/services/authentication/omniauth_service.rb
index 6983bb32117..4769ba8847a 100644
--- a/app/services/authentication/omniauth_service.rb
+++ b/app/services/authentication/omniauth_service.rb
@@ -162,7 +162,7 @@ module Authentication
def remap_existing_user
return unless Setting.oauth_allow_remapping_of_existing_users?
- User.not_builtin.find_by(login: user_attributes[:login])
+ User.not_builtin.find_by_login(user_attributes[:login]) # rubocop:disable Rails/DynamicFindBy
end
##
diff --git a/spec/services/authentication/omniauth_service_spec.rb b/spec/services/authentication/omniauth_service_spec.rb
index 1e42dbefd07..e508a1c65cd 100644
--- a/spec/services/authentication/omniauth_service_spec.rb
+++ b/spec/services/authentication/omniauth_service_spec.rb
@@ -34,11 +34,12 @@ RSpec.describe Authentication::OmniauthService do
provider: 'google',
uid: '123545',
info: { name: 'foo',
- email: '[email protected]',
+ email: auth_email,
first_name: 'foo',
last_name: 'bar' }
)
end
+ let(:auth_email) { '[email protected]' }
let(:controller) { double('Controller', session: session_stub) }
let(:session_stub) { [] }
@@ -127,27 +128,40 @@ RSpec.describe Authentication::OmniauthService do
context 'with an active user remapped',
with_settings: { oauth_allow_remapping_of_existing_users?: true } do
- let!(:user) { create(:user, identity_url: 'foo', login: '[email protected]') }
+ let!(:user) { create(:user, identity_url: 'foo', login: auth_email.downcase) }
- it 'does not call register user service and logs in the user' do
- allow(Users::RegisterUserService).to receive(:new)
+ shared_examples_for 'a successful remapping of foo' do
+ it 'does not call register user service and logs in the user' do
+ allow(Users::RegisterUserService).to receive(:new)
- expect(OpenProject::OmniAuth::Authorization)
- .to(receive(:after_login!))
- .with(user, auth_hash, instance)
+ expect(OpenProject::OmniAuth::Authorization)
+ .to(receive(:after_login!))
+ .with(user, auth_hash, instance)
- expect(call).to be_success
- expect(call.result).to eq user
- expect(call.result.firstname).to eq 'foo'
- expect(call.result.lastname).to eq 'bar'
- expect(call.result.mail).to eq '[email protected]'
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(call.result.firstname).to eq 'foo'
+ expect(call.result.lastname).to eq 'bar'
+ expect(call.result.login).to eq auth_email
+ expect(call.result.mail).to eq auth_email
+
+ user.reload
+ expect(user.firstname).to eq 'foo'
+ expect(user.lastname).to eq 'bar'
+ expect(user.identity_url).to eq 'google:123545'
+
+ expect(Users::RegisterUserService).not_to have_received(:new)
+ end
+ end
+
+ context 'with an all lower case login on the IdP side' do
+ it_behaves_like 'a successful remapping of foo'
+ end
- user.reload
- expect(user.firstname).to eq 'foo'
- expect(user.lastname).to eq 'bar'
- expect(user.identity_url).to eq 'google:123545'
+ context 'with a partially upper case login on the IdP side' do
+ let(:auth_email) { '[email protected]' }
- expect(Users::RegisterUserService).not_to have_received(:new)
+ it_behaves_like 'a successful remapping of foo'
end
end