Skip to content

Instantly share code, notes, and snippets.

@bungard
Last active August 29, 2015 14:11
Show Gist options
  • Save bungard/eec969d856a3cbe7e465 to your computer and use it in GitHub Desktop.
Save bungard/eec969d856a3cbe7e465 to your computer and use it in GitHub Desktop.
LDAP Bind/Authenticate
require 'ldap'
LDAP_S ={
:use_ssl => true,
:ldap_host => "domain.com",
:domain => "domain.com",
:start_tls => false,
:ldap_version => 3
}
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