logesh
logesh

Reputation: 2662

net-ldap authentication format in rails?

 def authenticate(username, password)
        require 'net-ldap'
        ldap = Net::LDAP.new
        ldap.host = 'server.local'
        ldap.port = 389
        ldap.base = 'cn=users, dc=server, dc=local'
        ldap.auth username, password
        if ldap.bind
            puts "authentication succeeded"
            else
            puts "authentication failed"
        end

The above is the code i use in my method and i am not sure why my attempts fail. I am trying to authenticate user. I could not find where i am going wrong? It puts authentication failed every time. why? please help me.

Upvotes: 1

Views: 821

Answers (1)

Pseudomonkey
Pseudomonkey

Reputation: 143

First up see if the computer you are using can talk to the LDAP server

telnet server.local 389

Obviously you want to be replacing server.local with your actual server details. If you can't log into the server this way then port 389 isn't open and you may need to be running on the SSL port 636. Try the previous command with 636 rather than 389 to see if that is the case.

If you are unable to telnet into the server on either of those ports you've either got a firewall rule blocking you from talking to it, LDAP is configured on a non standard port or something else is seriously wrong.

A working port 636 will probably mean you need to run something like the following.

require "net-ldap"
ldap = Net::LDAP.new(
    host: "server.local"
    port: 636
    encryption: :simple_tls
)
ldap.auth username, password

Failing all of that an error message is going to be pretty useful so try running

if ldap.bind
    # Yay!
else
    puts ldap.get_operation_result
end

With some results from this maybe we can help you a bit more.

Upvotes: 1

Related Questions