34 lines
842 B
Crystal
34 lines
842 B
Crystal
require "ldap"
|
|
require "socket"
|
|
require "ldap_escape"
|
|
|
|
module Backend
|
|
module Ldap
|
|
extend self
|
|
|
|
def create_client : LDAP::Client
|
|
LDAP::Client.new(TCPSocket.new(Backend.config.ldap.host, Backend.config.ldap.port))
|
|
end
|
|
|
|
def cn(username : String) : String
|
|
"cn=#{LdapEscape.dn(username)},#{Backend.config.ldap.user_dn}"
|
|
end
|
|
|
|
def uid(uid : String) : String
|
|
"uid=#{LdapEscape.dn(uid)},#{Backend.config.ldap.user_dn}"
|
|
end
|
|
|
|
def user(dn : String) : Array(Hash(String, Array(String)))
|
|
create_client
|
|
.authenticate(Backend.config.ldap.bind_dn, Backend.config.ldap.bind_password)
|
|
.search(base: dn)
|
|
end
|
|
|
|
def authenticate?(dn : String, password : String) : Bool
|
|
!!create_client.authenticate(dn, password)
|
|
rescue LDAP::Client::AuthError
|
|
false
|
|
end
|
|
end
|
|
end
|