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

Adapt auth_source_sso with improved finding of users

Oliver Günther8a938b37c107
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index b055a92dbe0..a66b018587a 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -272,7 +272,7 @@ class AccountController < ApplicationController
def auth_source_sso_failed
failure = session.delete :auth_source_sso_failure
login = failure[:login]
- user = find_or_create_sso_user(login, save: false)
+ user = build_user_from_auth_source(login)
if user.try(:new_record?)
return onthefly_creation_failed user, login: user.login, ldap_auth_source_id: user.ldap_auth_source_id
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index 1f4ef875767..dcf8ebc47fd 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -37,7 +37,8 @@ module AuthSourceSSO
Rails.logger.debug { "Starting header-based auth source SSO for #{header_name}='#{op_auth_header_value}'" }
- user = find_or_create_sso_user(login, save: true)
+ # Try to find an existing, or autocreate a new user for onthefly ldap connections
+ user = find_auth_source_user(login)
handle_sso_for! user, login
end
@@ -51,6 +52,16 @@ 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
@@ -110,43 +121,16 @@ module AuthSourceSSO
end
end
- def find_or_create_sso_user(login, save: false)
- find_user_from_auth_source(login) || create_user_from_auth_source(login, save:)
- end
-
- def find_user_from_auth_source(login)
- User
- .by_login(login)
- .where.not(ldap_auth_source_id: nil)
- .first
- end
-
- def create_user_from_auth_source(login, save:)
- attrs = LdapAuthSource.find_user(login)
+ def build_user_from_auth_source(login)
+ attrs = LdapAuthSource.get_user_attributes(login)
return unless attrs
- attrs[:login] = login
-
- call =
- if save
- Users::CreateService
- .new(user: User.system)
- .call(attrs)
- else
- Users::SetAttributesService
- .new(model: User.new, user: User.system, contract_class: Users::CreateContract)
- .call(attrs)
- end
+ call = Users::SetAttributesService
+ .new(model: User.new, user: User.system, contract_class: Users::CreateContract)
+ .call(attrs.merge(login:))
user = call.result
- call.on_success do
- logger.info(
- "User '#{user.login}' created from external auth source: " +
- "#{user.ldap_auth_source.type} - #{user.ldap_auth_source.name}"
- )
- end
-
call.on_failure do
logger.error "Tried to create user '#{login}' from external auth source but failed: #{call.message}"
end
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rb
index 56c650bf125..24ff578ca18 100644
--- a/app/models/ldap_auth_source.rb
+++ b/app/models/ldap_auth_source.rb
@@ -94,6 +94,21 @@ class LdapAuthSource < ApplicationRecord
nil
end
+ def self.get_user_attributes(login)
+ where(onthefly_register: true).find_each do |source|
+ begin
+ Rails.logger.debug { "Looking up '#{login}' in '#{source.name}'" }
+ attrs = source.get_user_attributes login
+ rescue StandardError => e
+ Rails.logger.error "Error during authentication: #{e.message}"
+ attrs = nil
+ end
+
+ return attrs.except(:dn) if attrs
+ end
+ nil
+ end
+
def seeded_from_env?
Setting.seed_ldap&.key?(name)
end
@@ -132,6 +147,29 @@ class LdapAuthSource < ApplicationRecord
raise LdapAuthSource::Error, "LdapError: #{e.message}"
end
+ # Get the user's dn and any attributes for them, given their login
+ def get_user_attributes(login)
+ ldap_con = initialize_ldap_con(account, account_password)
+
+ attrs = {}
+
+ filter = login_filter(login)
+ Rails.logger.debug do
+ "LDAP initializing search (BASE=#{base_dn}), (FILTER=#{filter})"
+ end
+
+ ldap_con.search(base: base_dn,
+ filter:,
+ attributes: search_attributes) do |entry|
+ attrs = get_user_attributes_from_ldap_entry(entry)
+ Rails.logger.debug { "DN found for #{login}: #{attrs[:dn]}" }
+ end
+
+ attrs
+ rescue Net::LDAP::Error => e
+ raise LdapAuthSource::Error, "LdapError: #{e.message}"
+ end
+
# Open and return a system connection
def with_connection
yield initialize_ldap_con(account, account_password)
@@ -290,27 +328,6 @@ class LdapAuthSource < ApplicationRecord
end
end
- # Get the user's dn and any attributes for them, given their login
- def get_user_attributes(login)
- ldap_con = initialize_ldap_con(account, account_password)
-
- attrs = {}
-
- filter = login_filter(login)
- Rails.logger.debug do
- "LDAP initializing search (BASE=#{base_dn}), (FILTER=#{filter})"
- end
-
- ldap_con.search(base: base_dn,
- filter:,
- attributes: search_attributes) do |entry|
- attrs = get_user_attributes_from_ldap_entry(entry)
- Rails.logger.debug { "DN found for #{login}: #{attrs[:dn]}" }
- end
-
- attrs
- end
-
def self.get_attr(entry, attr_name)
if attr_name.present?
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
diff --git a/spec/controllers/concerns/auth_source_sso_spec.rb b/spec/controllers/concerns/auth_source_sso_spec.rb
index 7842976bbc4..bfea1e11023 100644
--- a/spec/controllers/concerns/auth_source_sso_spec.rb
+++ b/spec/controllers/concerns/auth_source_sso_spec.rb
@@ -46,6 +46,7 @@ RSpec.describe MyController, :skip_2fa_stage do
let(:login) { "h.wurst" }
let(:header_login_value) { login }
let(:header_value) { "#{header_login_value}#{secret ? ':' : ''}#{secret}" }
+ let(:find_user_result) { user }
shared_examples 'should log in the user' do
it "logs in given user" do
@@ -97,9 +98,13 @@ RSpec.describe MyController, :skip_2fa_stage do
if sso_config
allow(OpenProject::Configuration)
.to receive(:auth_source_sso)
- .and_return(sso_config)
+ .and_return(sso_config)
end
+ allow(LdapAuthSource)
+ .to(receive(:find_user))
+ .and_return(find_user_result)
+
request.headers[header] = header_value
end
diff --git a/spec/requests/auth/auth_source_sso_spec.rb b/spec/requests/auth/auth_source_sso_spec.rb
index 26eb723c72c..8a073c326a7 100644
--- a/spec/requests/auth/auth_source_sso_spec.rb
+++ b/spec/requests/auth/auth_source_sso_spec.rb
@@ -37,18 +37,38 @@ RSpec.describe AuthSourceSSO, :skip_2fa_stage, # Prevent redirects to 2FA stage
}
end
- let(:ldap_auth_source) { create(:ldap_auth_source) }
- let(:user) { create(:user, login: 'bob', ldap_auth_source:) }
-
before do
allow(OpenProject::Configuration)
.to receive(:auth_source_sso)
.and_return(sso_config)
+ end
- get '/projects?foo=bar', headers: { 'X-Remote-User' => user.login }
+ describe 'with an existing LDAP user' do
+ let(:ldap_auth_source) { create(:ldap_auth_source) }
+ let(:user) { create(:user, login: 'bob', ldap_auth_source:) }
+
+ 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'
+ end
end
- it 'redirects the user to that URL' do
- expect(response).to redirect_to '/projects?foo=bar'
+ describe 'with an actual LDAP connection' do
+ include_context 'with temporary LDAP'
+
+ context 'when LDAP is onthefly_register' do
+ let(:onthefly_register) { true }
+ end
+
+ context 'when LDAP is not onthefly_register' do
+ let(:onthefly_register) { false }
+
+ 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
+ end
end
end