omn​​iauth-ldap如何工作?

bok*_*oka 3 ruby-on-rails omniauth

我目前正在开发一个我负责用户身份验证的rails项目.我们决定使用第三方身份验证,并尝试按照示例设置进行操作.这个例子是由Kevin Thompson完成的,被称​​为例子.

根据LDAP服务器的文档,我需要做的步骤是:

  1. 连接到LDAP服务器.
  2. 匿名绑定(无DN和密码).
  3. 使用用户名搜索LDAP条目
  4. 如果找到,则检索用户名的DN.
  5. 使用他们提供的用户DN和密码重新绑定.
  6. 如果此重新绑定成功,则对用户进行身份验证.

我跟着汤普森的例子,除了我没有用漂亮的东西; 使用devise进行用户管理,使用omniauth-ldap进行身份验证.但是,它不太起作用,我想知道它是否与服务器文档告诉我要做的事与omniauth-ldap实际做的事之间的差异有关...

具体来说,我的问题是我总是收到"无效凭据"错误.这是因为我需要做什么和omniauth-ldap做什么不匹配?

非常感谢您的建议或建议!

关于我如何设置的更多信息(为了保持匿名,我替换了一些东西)我可以根据请求发布更多我的代码.

配置/初始化/ devise.rb:

  config.omniauth :ldap,
    :host => 'ldap1.its.domain.ext',
    :base => 'ou=People, dc=domain, dc=ext',
    :port => 389,
    :attrs => 'uid',
    :method => :plain,
    :uid => 'uid'
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/用户/ omniauth_callbacks_controller.rb:

class Users::OmniauthCallbacksController <     Devise::OmniauthCallbacksController
  skip_before_filter :verify_authenticity_token
  def ldap
    ldap_return = request.env["omniauth.auth"]["extra"]["raw_info"]
    username = ldap_return.uid[0].to_s

    if @user = User.find_by_username(username)
      sign_in_and_redirect @user
    else
      @user = User.create(:username => username,)
      sign_in_and_redirect @user
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

小智 5

我刚刚解决了类似的问题.

首先,您需要确定您的域是否允许匿名绑定.我的情况不允许这样做.使用当前用户绑定的一个很好的拉取请求,dorren/omniauth-ldap.否则,您将需要一个系统帐户.为了最初移动,我使用我的用户名(即userPrincipalName)/密码:bind_dn和:password.

其次,对于LDAP身份验证,用于身份验证的两个uid值是sAMAccountName(username) userPrincipalName(username@ldap.domain.ext).我发现我的系统使用userPrincipalName.为了防止用户输入,我只是在提交表单之前连接了域.

试试这个配置.

config.omniauth :ldap,
  :host => 'ldap.domain.ext',
  :base => 'dc=ldap, dc=domain, dc=ext',
  :port => 389,
  :method => :plain,
  :uid => 'userPrincipalName',
  :bind_dn => 'bind_dn',
  :password => 'password' 
Run Code Online (Sandbox Code Playgroud)

我相信:bind_dn可以是这样的形式:

'CN = LastName \,FirstName,OU = People,DC = ldap,DC = domain,DC = ext'

要么

'username@ldap.domain.ext'

我还发现使用Net :: LDAP编写ruby脚本,绑定和搜索确实帮助我了解Active Directory,因为在此任务之前我不知道这个主题.