Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in custom authentication #207

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions lib/casserver/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions spec/authenticators/custom_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class CASServer::Authenticators::CustomAuth < CASServer::Authenticators::Test
end
4 changes: 2 additions & 2 deletions spec/casserver/authenticators/active_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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

Expand Down
24 changes: 12 additions & 12 deletions spec/casserver/authenticators/ldap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/casserver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions spec/config/alt_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down