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

Add better logging of the SAML response object

Oliver Günther5ebb53897a1c
diff --git a/app/services/authentication/omniauth_service.rb b/app/services/authentication/omniauth_service.rb
index 1baeb67156e..4f0f7681ee5 100644
--- a/app/services/authentication/omniauth_service.rb
+++ b/app/services/authentication/omniauth_service.rb
@@ -50,11 +50,14 @@ module Authentication
end
def call(additional_user_params = nil)
- Rails.logger.debug { "Returning from omniauth with hash #{auth_hash&.to_hash.inspect} Valid? #{auth_hash&.valid?}" }
+ inspect_response(Logger::DEBUG)
unless contract.validate
result = ServiceResult.new(success: false, errors: contract.errors)
- Rails.logger.warn { "Failed to process omniauth response for #{auth_uid}: #{result.message}" }
+ Rails.logger.error do
+ "[OmniAuth strategy #{strategy.name}] Failed to process omniauth response for #{auth_uid}: #{result.message}"
+ end
+ inspect_response(Logger::ERROR)
return result
end
@@ -73,6 +76,24 @@ module Authentication
private
+ ##
+ # Inspect the response object, trying to find out what got returned
+ def inspect_response(log_level)
+ case strategy
+ when ::OmniAuth::Strategies::SAML
+ ::OpenProject::AuthSaml::Inspector.inspect_response(auth_hash) do |message|
+ Rails.logger.add log_level, message
+ end
+ else
+ Rails.logger.add(log_level) do
+ "[OmniAuth strategy #{strategy.name}] Returning from omniauth with hash " \
+ "#{auth_hash&.to_hash.inspect} Valid? #{auth_hash&.valid?}"
+ end
+ end
+ rescue StandardError => e
+ OpenProject.logger.error "[OmniAuth strategy #{strategy.name}] Failed to inspect OmniAuth response: #{e.message}"
+ end
+
##
# After login flow
def tap_service_result(call)
diff --git a/modules/auth_saml/lib/open_project/auth_saml/inspector.rb b/modules/auth_saml/lib/open_project/auth_saml/inspector.rb
new file mode 100644
index 00000000000..2f76b321997
--- /dev/null
+++ b/modules/auth_saml/lib/open_project/auth_saml/inspector.rb
@@ -0,0 +1,29 @@
+module OpenProject
+ module AuthSaml
+ class Inspector
+ def self.inspect_response(auth_hash)
+ response = auth_hash.dig(:extra, :response_object)
+ if response
+ code = response.status_code ? "(CODE #{response.status_code})" : nil
+ message = response.status_message ? "(MESSAGE #{response.status_code})" : nil
+ yield "SAML response success ? #{response.success?} #{code} #{message}"
+
+ errors = Array(response.errors).map(&:to_s).join(", ")
+ yield "SAML errors: #{errors}" if errors.present?
+
+ yield "SAML response XML: #{response.response || '(not present)'}"
+ end
+ uid = auth_hash[:uid]
+ yield "SAML response uid (name identifier): #{uid || '(not present)'}"
+
+ info = auth_hash[:info]
+ yield "SAML retrieved attributes: #{info.inspect}"
+
+ yield "SAML auth hash is invalid, attributes are missing." unless auth_hash.valid?
+
+ session_idx = auth_hash.dig(:extra, :session_index)
+ yield "SAML session index: #{session_idx}"
+ end
+ end
+ end
+end
diff --git a/spec/controllers/concerns/omniauth_login_spec.rb b/spec/controllers/concerns/omniauth_login_spec.rb
index 348ca9fa9e4..67bb8d1f5d7 100644
--- a/spec/controllers/concerns/omniauth_login_spec.rb
+++ b/spec/controllers/concerns/omniauth_login_spec.rb
@@ -30,6 +30,24 @@ require 'spec_helper'
# Concern is included into AccountController and depends on methods available there
describe AccountController, type: :controller do
+ let(:omniauth_strategy) { double('Google Strategy', name: 'google') }
+ let(:omniauth_hash) do
+ OmniAuth::AuthHash.new(
+ provider: 'google',
+ strategy: omniauth_strategy,
+ uid: '123545',
+ info: { name: 'foo',
+ email: '[email protected]',
+ first_name: 'foo',
+ last_name: 'bar' }
+ )
+ end
+
+ before do
+ request.env['omniauth.auth'] = omniauth_hash
+ request.env['omniauth.strategy'] = omniauth_strategy
+ end
+
after do
User.current = nil
end
@@ -37,19 +55,7 @@ describe AccountController, type: :controller do
context 'GET #omniauth_login', with_settings: { self_registration: Setting::SelfRegistration.automatic } do
describe 'with on-the-fly registration' do
context 'providing all required fields' do
- let(:omniauth_hash) do
- OmniAuth::AuthHash.new(
- provider: 'google',
- uid: '123545',
- info: { name: 'foo',
- email: '[email protected]',
- first_name: 'foo',
- last_name: 'bar' }
- )
- end
-
before do
- request.env['omniauth.auth'] = omniauth_hash
request.env['omniauth.origin'] = 'https://example.net/some_back_url'
post :omniauth_login, params: { provider: :google }
end
@@ -72,10 +78,10 @@ describe AccountController, type: :controller do
end
describe 'strategy uid mapping override' do
- let(:omniauth_strategy) { double('Google Strategy') }
let(:omniauth_hash) do
OmniAuth::AuthHash.new(
provider: 'google',
+ strategy: omniauth_strategy,
uid: 'foo',
info: {
uid: 'internal',
@@ -86,11 +92,6 @@ describe AccountController, type: :controller do
)
end
- before do
- request.env['omniauth.auth'] = omniauth_hash
- request.env['omniauth.strategy'] = omniauth_strategy
- end
-
it 'takes the uid from the mapped attributes' do
post :omniauth_login, params: { provider: :google }
@@ -101,10 +102,10 @@ describe AccountController, type: :controller do
end
describe 'strategy attribute mapping override' do
- let(:omniauth_strategy) { double('Google Strategy') }
let(:omniauth_hash) do
OmniAuth::AuthHash.new(
provider: 'google',
+ strategy: omniauth_strategy,
uid: 'foo',
info: { email: '[email protected]',
first_name: 'what',
@@ -117,10 +118,6 @@ describe AccountController, type: :controller do
)
end
- before do
- request.env['omniauth.auth'] = omniauth_hash
- request.env['omniauth.strategy'] = omniauth_strategy
- end
context 'available' do
it 'merges the strategy mapping' do
@@ -167,7 +164,6 @@ describe AccountController, type: :controller do
end
it 'renders user form' do
- request.env['omniauth.auth'] = omniauth_hash
post :omniauth_login, params: { provider: :google }
expect(response).to render_template :register
expect(assigns(:user).mail).to eql('[email protected]')
@@ -257,7 +253,6 @@ describe AccountController, type: :controller do
end
before do
- request.env['omniauth.auth'] = omniauth_hash
request.env['omniauth.origin'] = 'https://example.net/some_back_url'
post :omniauth_login, params: { provider: :google }
@@ -289,10 +284,6 @@ describe AccountController, type: :controller do
identity_url: 'google:123545')
end
- before do
- request.env['omniauth.auth'] = omniauth_hash
- end
-
context 'with an active account' do
before do
user.save!
@@ -505,8 +496,6 @@ describe AccountController, type: :controller do
end
before do
- request.env['omniauth.auth'] = omniauth_hash
-
post :omniauth_login, params: { provider: :google }
end
diff --git a/spec/services/authentication/omniauth_service_spec.rb b/spec/services/authentication/omniauth_service_spec.rb
index 55cb10c055e..af057bd438e 100644
--- a/spec/services/authentication/omniauth_service_spec.rb
+++ b/spec/services/authentication/omniauth_service_spec.rb
@@ -30,7 +30,7 @@
require 'spec_helper'
describe Authentication::OmniauthService do
- let(:strategy) { double('Omniauth Strategy') }
+ let(:strategy) { double('Omniauth Strategy', name: 'saml') }
let(:auth_hash) do
OmniAuth::AuthHash.new(
provider: 'google',