OK i've fixed it now by using a bit of code based on the OpenID
tutorial.
I must say I liked my previous attempt better, since it felt more
natural to overwrite Authlogic::Session::Base#valid_credentials? than
to overwrite the Authlogic::Session::Basewhole save method.
Anyways, hopefully somebody can benifit from this code.
class UserSession < Authlogic::Session::Base
def save
record = search_for_record(find_by_login_method, send
(login_field))
if record.blank?
errors.add(login_field, I18n.t
('error_messages.login_not_found', :default => "does not exist"))
return false
end
# the User exists locally, now try to authenticate against the
LDAP server
ldap = Net::LDAP.new
ldap.host = LDAP_HOST
# first create the username/password strings to send to the LDAP
server
# in our case we need to add the domain so it looks like COMPANY
\firstname.lastname
ldap.auth "#{LDAP_DOMAIN}\\" + record.send(login_field), send
("protected_#{password_field}")
# now the actual authentication
if !ldap.bind
RAILS_DEFAULT_LOGGER.info { "LDAP authentication failed" }
errors.add(password_field, I18n.t
('error_messages.password_invalid', :default => "is not valid"))
return false
end
self.unauthorized_record = record
super
end
end