如何使用 LDAP 进行 Ruby on Rails 身份验证? [英] How to Ruby on Rails authentication with LDAP?
本文介绍了如何使用 LDAP 进行 Ruby on Rails 身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 Web 应用程序,我有一个使用 bcrypt gem
的身份验证方法,它工作正常,但我想将身份验证方法更改为 LDAP,因为我使用的是 Intranet 环境并且想要我的用户能够使用 Windows 凭据登录.
I'm developing a web app and I have an authentication method using bcrypt gem
It works fine, but I wanted to change the authentication method to LDAP because I'm using an intranet environment and want my users to be able to sign in with windows credentials.
我正在寻找使用 net-ldap gem
,但我在网上找不到任何关于如何在我的 Web 应用程序中实现它的好的教程/解释.
I'm looking to use net-ldap gem
but I can't find any good toturials/explanations online on how to implement this into my web application.
- 你能帮我解决这个问题吗?
- 我该怎么做?
推荐答案
这是我过去用来做多服务器 LDAP 检查的实用程序类:
Here's a utility class I've used in the past to do multi-server LDAP check:
require 'net/ldap'
# Ldap.authenticate('user', 'password')
# => `true` if valid
# => `false` if invalid
# => `nil` if LDAP unavailable
class Ldap
def self.config
{
domain: 'mydomain',
servers: ['server1', 'server2']
}
end
def self.authenticate(login, pass)
return false if login.empty? or pass.empty?
config['servers'].each do |server|
auth = authenticate_against_server(login, pass, server, config['domain'])
return auth unless auth.nil?
end
nil
end
private
def self.authenticate_against_server(login, pass, host, domain)
conn = Net::LDAP.new(
host: host,
port: 636,
base: "dc=#{domain}, dc=local",
encryption: :simple_tls,
auth: { username: "#{login}@#{domain}.local",
password: pass,
method: :simple }
)
Timeout::timeout(15) do
return conn.bind ? true : false
end
rescue Net::LDAP::LdapError => e
notify_ldap_admin(host, 'Error', e)
return nil
rescue Timeout::Error => e
notify_ldap_admin(host, 'Timeout', e)
return nil
end
def self.notify_ldap_admin(host, error_type, error)
msg = "LDAP #{error_type} on #{host}"
RAILS_DEFAULT_LOGGER.debug(msg)
DeveloperMailer.deliver_ldap_failure_msg(msg, error)
end
end
这篇关于如何使用 LDAP 进行 Ruby on Rails 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文