Created
September 2, 2014 16:00
-
-
Save jdanielnd/a0e6a73f3f5f9b3462a6 to your computer and use it in GitHub Desktop.
Devise LDAP Authenticable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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