An extension of Authlogic to add LDAP authentication.
Ruby
Pull request Compare This branch is 14 commits ahead of binarylogic:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitignore
CHANGELOG.rdoc
README.rdoc
Rakefile
init.rb

README.rdoc

Basic operational state with authlogic 3.1.0 Only in testing phase at present. ldap columns not working well due to a mess on the ldap server side. I created a 'home' environment for testing without ldap connectivity.

authlogic_ldap


part of schema

create_table "users", :force => true do |t|
    t.string   "persistence_token"
    t.string   "ldap_login"
    t.datetime "created_at"
    t.datetime "updated_at"
end

app/models/user.rb

acts_as_authentic do |c|
  c.validate_password_field = false	
  c.validate_email_field = false
  c.require_password_confirmation = false
  unless Rails.env.home? || Rails.env.test?
    c.validate_ldap_login = false	 
  else
    c.login_field = :ldap_login
  end
end
#swift bypass for non ldap auth testing
def bypass?(password)
  if password == "qwertyuiop"
    return true
  else
    return false
  end
end

app/models/user_session.rb

class UserSession < Authlogic::Session::Base
  if Rails.env.home? || Rails.env.test?
    login_field = "ldap_login"
    verify_password_method :bypass?
  else
    login_field = "ldap_login"
    password_field = "ldap_password"
    ldap_host Settings.ldap.host
    ldap_port Settings.ldap.port
    ldap_base Settings.ldap.base
    ldap_user_dn Settings.ldap.user_dn
    self.find_by_ldap_login_method :find_by_ldap_login	
    #this should make a new record if there isn't one
    #self.find_by_ldap_login_method :find_or_create_by_ldap_login
  end
end

app/models/settings.rb requires settingslogic, github.com/binarylogic/settingslogic

class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
    namespace Rails.env
end

config/application.yml The user_dn for the server i use is of the form user_name@ldapservername

defaults: &defaults
  ldap:
    host: 'ldapservername'
    port: ''
    base: 'dc=ds....'
    user_dn: '@ldapservername'  
   loginfilter: 'cn'
development:
  <<: *defaults

app/views/user_sessions.rb fix from techoctave.com/c7/posts/37-authlogic-and-rails-3-0-solution

<%= form_for @user_session, :as => :user_session, :url => {:action => "create"} do |f| %>
<%= field_set_tag nil, :class => 'input' do %>
<% if @user_session.errors.any? %>
<% errors = @user_session.errors[:base] %>
<div id="error_explanation">
<h3><%= pluralize(@user_session.errors.count, "error") %> prohibited your login:</h3>
</div>
<% end %>
<ol>
<%= f.label "ISS Username" %>
<%= f.text_field :ldap_login %>	
<% if Rails.env.test? || Rails.env.home? %>
<%= f.label :password %>
<%= f.password_field :password %>
<% else %>
<%= f.label :password %>
<%= f.password_field :ldap_password %>
<% end %>	
</ol>
<%= f.submit "Login" %>
<% end %>
<% end %>