diff --git a/lib/saml_idp.rb b/lib/saml_idp.rb index 99df05e1..543111a3 100644 --- a/lib/saml_idp.rb +++ b/lib/saml_idp.rb @@ -9,7 +9,7 @@ module SamlIdp require 'saml_idp/metadata_builder' require 'saml_idp/version' require 'saml_idp/fingerprint' - require 'saml_idp/engine' if defined?(::Rails) + require 'saml_idp/engine' if defined?(::Rails::Engine) def self.config @config ||= SamlIdp::Configurator.new diff --git a/lib/saml_idp/configurator.rb b/lib/saml_idp/configurator.rb index 4998869a..e645f912 100644 --- a/lib/saml_idp/configurator.rb +++ b/lib/saml_idp/configurator.rb @@ -35,7 +35,7 @@ def initialize self.service_provider.persisted_metadata_getter = ->(id, service_provider) { } self.session_expiry = 0 self.attributes = {} - self.logger = defined?(::Rails) ? Rails.logger : ->(msg) { puts msg } + self.logger = (defined?(::Rails) && Rails.respond_to?(:logger)) ? Rails.logger : ->(msg) { puts msg } end # formats diff --git a/saml_idp.gemspec b/saml_idp.gemspec index d8346128..027139e5 100644 --- a/saml_idp.gemspec +++ b/saml_idp.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |s| s.authors = ['Jon Phenow'] s.email = 'jon.phenow@sportngin.com' s.homepage = 'https://github.com/saml-idp/saml_idp' - s.summary = 'SAML Indentity Provider for Ruby' + s.summary = 'SAML Identity Provider for Ruby' s.description = 'SAML IdP (Identity Provider) Library for Ruby' s.date = Time.now.utc.strftime('%Y-%m-%d') s.files = Dir['lib/**/*', 'LICENSE', 'README.md', 'Gemfile', 'saml_idp.gemspec'] @@ -46,6 +46,7 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', '>= 5.2') s.add_dependency('builder', '>= 3.0') s.add_dependency('nokogiri', '>= 1.6.2') + s.add_dependency('ostruct') s.add_dependency('rexml') s.add_dependency('xmlenc', '>= 0.7.1') @@ -56,6 +57,7 @@ Gem::Specification.new do |s| s.add_development_dependency('rails', '>= 5.2') s.add_development_dependency('debug') s.add_development_dependency('rake') + s.add_development_dependency('debug') s.add_development_dependency('rspec', '>= 3.7.0') s.add_development_dependency('ruby-saml', '>= 1.7.2') s.add_development_dependency('simplecov') diff --git a/spec/lib/saml_idp/configurator_spec.rb b/spec/lib/saml_idp/configurator_spec.rb index 5148a289..33141117 100644 --- a/spec/lib/saml_idp/configurator_spec.rb +++ b/spec/lib/saml_idp/configurator_spec.rb @@ -47,5 +47,34 @@ module SamlIdp it 'has a valid session_expiry' do expect(subject.session_expiry).to eq(0) end + + context "logger initialization" do + context 'when Rails has been properly initialized' do + it 'sets logger to Rails.logger' do + rails_logger = double("Rails.logger") + stub_const("Rails", double(logger: rails_logger)) + + expect(subject.logger).to eq(Rails.logger) + end + end + + context 'when Rails is not fully initialized' do + it 'sets logger to a lambda' do + stub_const("Rails", Class.new) + + expect(subject.logger).to be_a(Proc) + expect { subject.logger.call("test") }.to output("test\n").to_stdout + end + end + + context 'when Rails is not defined' do + it 'sets logger to a lambda' do + hide_const("Rails") + + expect(subject.logger).to be_a(Proc) + expect { subject.logger.call("test") }.to output("test\n").to_stdout + end + end + end end end diff --git a/spec/support/saml_request_macros.rb b/spec/support/saml_request_macros.rb index 5efbdf29..d4abf3d6 100644 --- a/spec/support/saml_request_macros.rb +++ b/spec/support/saml_request_macros.rb @@ -96,6 +96,17 @@ def idp_configure(saml_acs_url = "https://foo.example.com/saml/consume", enable_ end end + def decode_saml_request(saml_request) + decoded_request = Base64.decode64(saml_request) + begin + # Try to decompress, since SAMLRequest might be compressed + Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(decoded_request) + rescue Zlib::DataError + # If it's not compressed, just return the decoded request + decoded_request + end + end + def print_pretty_xml(xml_string) doc = REXML::Document.new xml_string outbuf = ""