saml_database.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class OmniAuth::Strategies::SamlDatabase < OmniAuth::Strategies::SAML
  3. option :name, 'saml'
  4. def self.setup
  5. http_type = Setting.get('http_type')
  6. fqdn = Setting.get('fqdn')
  7. # Use meta URL as entity id/issues as it is best practice.
  8. # See: https://community.zammad.org/t/saml-oidc-third-party-authentication/2533/13
  9. entity_id = "#{http_type}://#{fqdn}/auth/saml/metadata"
  10. assertion_consumer_service_url = "#{http_type}://#{fqdn}/auth/saml/callback"
  11. single_logout_service_url = "#{http_type}://#{fqdn}/auth/saml/slo"
  12. config = Setting.get('auth_saml_credentials') || {}
  13. config.compact_blank
  14. .merge(
  15. assertion_consumer_service_url: assertion_consumer_service_url,
  16. sp_entity_id: entity_id,
  17. single_logout_service_url: single_logout_service_url,
  18. idp_slo_session_destroy: proc { |env, session| destroy_session(env, session) },
  19. )
  20. end
  21. def self.destroy_session(env, session)
  22. session.delete('saml_uid')
  23. session.delete('saml_transaction_id')
  24. session.delete('saml_session_index')
  25. @_current_user = nil
  26. env['rack.session.options'][:expire_after] = nil
  27. session.destroy
  28. end
  29. def initialize(app, *args, &)
  30. args[0] = self.class.setup
  31. super
  32. end
  33. private
  34. def handle_logout_response(raw_response, settings)
  35. logout_response = OneLogin::RubySaml::Logoutresponse.new(raw_response, settings, matches_request_id: session['saml_transaction_id'])
  36. logout_response.soft = false
  37. logout_response.validate
  38. redirect_path = session['omniauth.origin']&.include?('/mobile') ? '/mobile' : '/'
  39. self.class.destroy_session(env, session)
  40. redirect "#{Setting.get('http_type')}://#{Setting.get('fqdn')}#{redirect_path}"
  41. end
  42. end