Skip to content

Instantly share code, notes, and snippets.

@jdanielnd
Created September 2, 2014 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdanielnd/a0e6a73f3f5f9b3462a6 to your computer and use it in GitHub Desktop.
Save jdanielnd/a0e6a73f3f5f9b3462a6 to your computer and use it in GitHub Desktop.
Devise LDAP Authenticable
require 'net/ldap'
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class LdapAuthenticatable < Authenticatable
def authenticate!
if params[:user]
ldap = Net::LDAP.new
domain = "ou=users,dc=test,dc=com"
dn = "cn=#{email},#{domain}"
ldap.auth dn, password
if ldap.bind and user = User.find_by_email(email)
user.update_attribute(:password, password)
success!(user)
else
fail(:invalid_login)
end
end
end
def email
params[:user][:email]
end
def password
params[:user][:password]
end
def user_data
{:email => email, :password => password, :password_confirmation => password}
end
end
end
end
Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment