From 43cbb31d9cc67c4ea175a1407c562dd57e2a207c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=BD=93=E4=B8=80?= Date: Tue, 22 Apr 2014 15:28:40 +0800 Subject: [PATCH 1/3] Fix a bug in custom authentication Now custom authenticator can be used in an authenticators array. Also you can use more than one custom authenticator. --- lib/casserver/server.rb | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/lib/casserver/server.rb b/lib/casserver/server.rb index 77f7633a..d88c91ba 100644 --- a/lib/casserver/server.rb +++ b/lib/casserver/server.rb @@ -189,35 +189,21 @@ def self.init_authenticators! exit 1 end - begin - # attempt to instantiate the authenticator - config[:authenticator] = [config[:authenticator]] unless config[:authenticator].instance_of? Array - config[:authenticator].each { |authenticator| auth << authenticator[:class].constantize} - rescue NameError - if config[:authenticator].instance_of? Array - config[:authenticator].each do |authenticator| - if !authenticator[:source].nil? - # config.yml explicitly names source file - require authenticator[:source] - else - # the authenticator class hasn't yet been loaded, so lets try to load it from the casserver/authenticators directory - auth_rb = authenticator[:class].underscore.gsub('cas_server/', '') - require 'casserver/'+auth_rb - end - auth << authenticator[:class].constantize - end - else - if config[:authenticator][:source] + # attempt to instantiate the authenticator + config[:authenticator] = [config[:authenticator]] unless config[:authenticator].instance_of? Array + config[:authenticator].each do |authenticator| + begin + auth << authenticator[:class].constantize + rescue NameError + if !authenticator[:source].nil? # config.yml explicitly names source file - require config[:authenticator][:source] + require authenticator[:source] else # the authenticator class hasn't yet been loaded, so lets try to load it from the casserver/authenticators directory - auth_rb = config[:authenticator][:class].underscore.gsub('cas_server/', '') + auth_rb = authenticator[:class].underscore.gsub('cas_server/', '') require 'casserver/'+auth_rb end - - auth << config[:authenticator][:class].constantize - config[:authenticator] = [config[:authenticator]] + auth << authenticator[:class].constantize end end From c7834448fb3ac2fd23e6aacb86ced6cf42c94828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=BD=93=E4=B8=80?= Date: Wed, 23 Apr 2014 08:06:10 +0800 Subject: [PATCH 2/3] prevent DEPRECATION warnings. rspec-mocks now use stub instead of stub!, and double instead of mock. --- .../authenticators/active_resource_spec.rb | 4 ++-- spec/casserver/authenticators/ldap_spec.rb | 24 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/casserver/authenticators/active_resource_spec.rb b/spec/casserver/authenticators/active_resource_spec.rb index de60bb5b..0d17d960 100644 --- a/spec/casserver/authenticators/active_resource_spec.rb +++ b/spec/casserver/authenticators/active_resource_spec.rb @@ -64,7 +64,7 @@ def mock_authenticate identity = nil identity = CASServer::Authenticators::Helpers::Identity.new if identity.nil? - CASServer::Authenticators::Helpers::Identity.stub!(:authenticate).and_return(identity) + CASServer::Authenticators::Helpers::Identity.stub(:authenticate).and_return(identity) end def sample_identity attrs = {} @@ -87,7 +87,7 @@ def sample_identity attrs = {} end it "should return false when http raises" do - CASServer::Authenticators::Helpers::Identity.stub!(:authenticate).and_raise(ActiveResource::ForbiddenAccess.new({})) + CASServer::Authenticators::Helpers::Identity.stub(:authenticate).and_raise(ActiveResource::ForbiddenAccess.new({})) auth.validate(credentials).should be_false end diff --git a/spec/casserver/authenticators/ldap_spec.rb b/spec/casserver/authenticators/ldap_spec.rb index 3a87720c..af63ea49 100644 --- a/spec/casserver/authenticators/ldap_spec.rb +++ b/spec/casserver/authenticators/ldap_spec.rb @@ -11,18 +11,18 @@ # Trigger autoload to load net ldap CASServer::Authenticators::LDAP - @ldap_entry = mock(Net::LDAP::Entry.new) - @ldap_entry.stub!(:[]).and_return("Test") - - @ldap = mock(Net::LDAP) - @ldap.stub!(:host=) - @ldap.stub!(:port=) - @ldap.stub!(:encryption) - @ldap.stub!(:bind_as).and_return(true) - @ldap.stub!(:authenticate).and_return(true) - @ldap.stub!(:search).and_return([@ldap_entry]) - - Net::LDAP.stub!(:new).and_return(@ldap) + @ldap_entry = double(Net::LDAP::Entry.new) + @ldap_entry.stub(:[]).and_return("Test") + + @ldap = double(Net::LDAP) + @ldap.stub(:host=) + @ldap.stub(:port=) + @ldap.stub(:encryption) + @ldap.stub(:bind_as).and_return(true) + @ldap.stub(:authenticate).and_return(true) + @ldap.stub(:search).and_return([@ldap_entry]) + + Net::LDAP.stub(:new).and_return(@ldap) end describe '#validate' do From d649591ae0d8085d9fa51d9f6a877e9871848bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=BD=93=E4=B8=80?= Date: Wed, 23 Apr 2014 09:01:57 +0800 Subject: [PATCH 3/3] add test for loading custom authenticator --- spec/authenticators/custom_auth.rb | 2 ++ spec/casserver_spec.rb | 15 +++++++++++++++ spec/config/alt_config.yml | 9 +++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 spec/authenticators/custom_auth.rb diff --git a/spec/authenticators/custom_auth.rb b/spec/authenticators/custom_auth.rb new file mode 100644 index 00000000..07f079de --- /dev/null +++ b/spec/authenticators/custom_auth.rb @@ -0,0 +1,2 @@ +class CASServer::Authenticators::CustomAuth < CASServer::Authenticators::Test +end diff --git a/spec/casserver_spec.rb b/spec/casserver_spec.rb index b49c1a03..e0701e34 100644 --- a/spec/casserver_spec.rb +++ b/spec/casserver_spec.rb @@ -139,6 +139,21 @@ visit "/test/logout" page.status_code.should_not == 404 end + it "should load custom authenticator" do + load_server("alt_config") + app.auth.length.should == 2 + app.auth.should include CASServer::Authenticators::Test + app.auth.should include CASServer::Authenticators::CustomAuth + end + it "login with custom authenticator" do + load_server("alt_config") + reset_spec_database + visit "/login" + fill_in 'username', :with => VALID_USERNAME + fill_in 'password', :with => "custom_password" + click_button 'login-submit' + page.should have_content("You have successfully logged in") + end end describe 'validation' do diff --git a/spec/config/alt_config.yml b/spec/config/alt_config.yml index 70e75ffb..c02a25b1 100644 --- a/spec/config/alt_config.yml +++ b/spec/config/alt_config.yml @@ -20,8 +20,13 @@ disable_auto_migrations: true quiet: true authenticator: - class: CASServer::Authenticators::Test - password: spec_password + - + class: CASServer::Authenticators::Test + password: spec_password + - + class: CASServer::Authenticators::CustomAuth + source: authenticators/custom_auth.rb + password: custom_password theme: simple