Last active
August 29, 2015 14:11
-
-
Save bungard/eec969d856a3cbe7e465 to your computer and use it in GitHub Desktop.
LDAP Bind/Authenticate
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 'ldap' | |
| LDAP_S ={ | |
| :use_ssl => true, | |
| :ldap_host => "domain.com", | |
| :domain => "domain.com", | |
| :start_tls => false, | |
| :ldap_version => 3 | |
| } |
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
| def self.bind?(user,pass) | |
| if pass == "" | |
| return false | |
| end | |
| port = 389 | |
| if LDAP_S[:use_ssl] | |
| port = 636 #secureSSL = 636 unsecure/TLS = 389 | |
| end | |
| begin | |
| conn = nil | |
| if LDAP_S[:use_ssl] | |
| conn = LDAP::SSLConn.new(LDAP_S[:ldap_host],port,LDAP_S[:start_tls]) #Conn.new = unsecure/TLS: SSLConn = secureSSL | |
| else | |
| conn = LDAP::Conn.new(LDAP_S[:ldap_host],port) | |
| if LDAP_S[:start_tls] | |
| conn.start_tls | |
| end | |
| end | |
| conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, LDAP_S[:ldap_version]) | |
| conn.simple_bind("#{user}@#{LDAP_S[:domain]}",pass) | |
| return true | |
| rescue => e | |
| conn.perror("debug") | |
| return false | |
| ensure | |
| conn.unbind unless conn == nil | |
| conn = nil | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment