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

Ensure we always look in LDAP for the user even if not onthefly

Oliver Günther692c73019511
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index dcf8ebc47fd..08d4d9d45d4 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -38,7 +38,7 @@ module AuthSourceSSO
Rails.logger.debug { "Starting header-based auth source SSO for #{header_name}='#{op_auth_header_value}'" }
# Try to find an existing, or autocreate a new user for onthefly ldap connections
- user = find_auth_source_user(login)
+ user = LdapAuthSource.find_user(login)
handle_sso_for! user, login
end
@@ -52,16 +52,6 @@ module AuthSourceSSO
nil
end
- def find_auth_source_user(login)
- user = User
- .active
- .by_login(login)
- .where.not(ldap_auth_source_id: nil)
- .first
-
- user || LdapAuthSource.find_user(login)
- end
-
def read_sso_login
get_validated_login! op_auth_header_value
end
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rb
index 24ff578ca18..fe3673fc078 100644
--- a/app/models/ldap_auth_source.rb
+++ b/app/models/ldap_auth_source.rb
@@ -43,6 +43,7 @@ class LdapAuthSource < ApplicationRecord
def self.unique_attribute
:name
end
+
prepend ::Mixins::UniqueFinder
enum tls_mode: {
@@ -79,18 +80,18 @@ class LdapAuthSource < ApplicationRecord
nil
end
+ ##
+ # Find a user by login in any of the available sources.
+ # If it's an onthefly_register ldap connection, this might implictly create the user.
def self.find_user(login)
- where(onthefly_register: true).find_each do |source|
- begin
- Rails.logger.debug { "Looking up '#{login}' in '#{source.name}'" }
- user = source.find_user login
- rescue StandardError => e
- Rails.logger.error "Error during authentication: #{e.message}"
- user = nil
- end
-
+ find_each do |source|
+ Rails.logger.debug { "Looking up '#{login}' in '#{source.name}'" }
+ user = source.find_user login
return user if user
+ rescue StandardError => e
+ Rails.logger.error "Error during authentication: #{e.message}"
end
+
nil
end
diff --git a/spec/features/auth/auth_source_sso_logout_spec.rb b/spec/features/auth/auth_source_sso_logout_spec.rb
index 3e259f590a5..82c20c1a984 100644
--- a/spec/features/auth/auth_source_sso_logout_spec.rb
+++ b/spec/features/auth/auth_source_sso_logout_spec.rb
@@ -34,12 +34,6 @@ end
RSpec.describe 'Login with auth source SSO',
driver: :auth_source_sso do
- before do
- allow(OpenProject::Configuration)
- .to receive(:auth_source_sso)
- .and_return(sso_config)
- end
-
let(:sso_config) do
{
header: 'X-Remote-User',
@@ -47,8 +41,14 @@ RSpec.describe 'Login with auth source SSO',
}
end
- let(:ldap_auth_source) { create(:ldap_auth_source) }
- let!(:user) { create(:user, login: 'bob', ldap_auth_source:) }
+ let!(:user) { create(:user, login: 'bob') }
+
+ before do
+ allow(OpenProject::Configuration)
+ .to receive(:auth_source_sso)
+ .and_return(sso_config)
+ allow(LdapAuthSource).to receive(:find_user).with('bob').and_return(user)
+ end
it 'can log out after multiple visits' do
visit home_path
diff --git a/spec/requests/auth/auth_source_sso_spec.rb b/spec/requests/auth/auth_source_sso_spec.rb
index 8a073c326a7..164649e4446 100644
--- a/spec/requests/auth/auth_source_sso_spec.rb
+++ b/spec/requests/auth/auth_source_sso_spec.rb
@@ -43,25 +43,53 @@ RSpec.describe AuthSourceSSO, :skip_2fa_stage, # Prevent redirects to 2FA stage
.and_return(sso_config)
end
- describe 'with an existing LDAP user' do
- let(:ldap_auth_source) { create(:ldap_auth_source) }
- let(:user) { create(:user, login: 'bob', ldap_auth_source:) }
+ include_context 'with temporary LDAP'
- it 'redirects the user to that URL' do
- get '/projects?foo=bar', headers: { 'X-Remote-User' => user.login }
- expect(response).to redirect_to '/projects?foo=bar'
+ context 'when LDAP is onthefly_register' do
+ let(:onthefly_register) { true }
+
+ it 'creates the user on the fly' do
+ expect(User.find_by(login: 'aa729')).to be_nil
+
+ expect do
+ get '/projects', headers: { 'X-Remote-User' => 'aa729' }
+ end.to change(User.not_builtin, :count).by(1)
+
+ user = User.find_by(login: 'aa729')
+ expect(user).to be_present
+ expect(user).to be_active
+ expect(session[:user_id]).to eq user.id
+ expect(session[:user_from_auth_header]).to eq true
+ expect(response).to redirect_to '/?first_time_user=true'
end
end
- describe 'with an actual LDAP connection' do
- include_context 'with temporary LDAP'
+ context 'when LDAP is not onthefly_register' do
+ let(:onthefly_register) { false }
- context 'when LDAP is onthefly_register' do
- let(:onthefly_register) { true }
+ it 'returns an error when the user does not exist' do
+ get '/projects', headers: { 'X-Remote-User' => 'nonexistent' }
+
+ expect(response).to redirect_to '/sso'
+ expect(session[:auth_source_sso_failure]).to be_present
+ end
+
+ context 'when the user exists, but is outdated' do
+ let(:user) { create(:user, login: 'ldap_admin', admin: false, ldap_auth_source:) }
+
+ it 'redirects the user to that URL' do
+ expect(user).not_to be_admin
+ get '/projects?foo=bar', headers: { 'X-Remote-User' => user.login }
+ expect(response).to redirect_to '/projects?foo=bar'
+
+ user.reload
+ expect(user).to be_admin
+ end
end
- context 'when LDAP is not onthefly_register' do
- let(:onthefly_register) { false }
+ context 'when the user exists in another auth source that is inaccessible' do
+ let(:other_ldap) { create(:ldap_auth_source, name: 'other_ldap') }
+ let(:user) { create(:user, login: 'ldap_admin', admin: false, ldap_auth_source: other_ldap) }
it 'returns an error when the user does not exist' do
get '/projects', headers: { 'X-Remote-User' => 'nonexistent' }
diff --git a/spec/support/shared/with_test_ldap.rb b/spec/support/shared/with_test_ldap.rb
index 1d7bca263d7..4ebfd693f8e 100644
--- a/spec/support/shared/with_test_ldap.rb
+++ b/spec/support/shared/with_test_ldap.rb
@@ -45,7 +45,7 @@ RSpec.shared_context 'with temporary LDAP' do
# Ldap has:
# three users aa729, bb459, cc414
# two groups foo (aa729), bar(aa729, bb459, cc414)
- let(:ldap_auth_source) do
+ let!(:ldap_auth_source) do
create(:ldap_auth_source,
port: ParallelHelper.port_for_ldap.to_s,
account: 'uid=admin,ou=system',
@@ -56,7 +56,8 @@ RSpec.shared_context 'with temporary LDAP' do
attr_login: 'uid',
attr_firstname: 'givenName',
attr_lastname: 'sn',
- attr_mail: 'mail')
+ attr_mail: 'mail',
+ attr_admin: 'isAdmin')
end
let(:onthefly_register) { false }