轨道 3 和devise_ldap_authenticatable:针对 Active Directory 的授权?

发布于 2025-01-04 08:33:11 字数 1328 浏览 1 评论 0原文

我成功地使用 devise 和 devise_ldap_authenticatable 运行我的 Rails 3 应用程序,以针对本地 Active Directory 进行身份验证。

现在我想添加授权功能,以便仅允许属于特定 AD 组的 AD 用户进行访问。

因此,为了简单地开始,我首先使用 linux 命令 ldapsearch 在 AD 中查找我自己的用户。结果包含类似...

(...)
memberOf: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
memberOf: CN=my,OU=foo,DC=bar2,DC=role,DC=domain,DC=com
memberOf: (...)
(...)

好吧,现在我决定,我想限制对 CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com 成员的访问。

因此,我将 ldap.yml 更改为包含:

authorizations: &AUTHORIZATIONS
  group_base: ou=role,dc=domain,dc=com
  required_groups:
    - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

development:
  (...)
  <<: *AUTHORIZATIONS

另外,将 devise.rb 更改为包含:

Devise.setup do |config|
  config.ldap_logger = true
  config.ldap_create_user = true
  config.ldap_update_password = false
  config.ldap_check_group_membership = true # <-- activated this line
  config.ldap_use_admin_to_bind = true
  #config.ldap_ad_group_check = true <-- don't know what this is good for

现在,当尝试进行身份验证时,访问被拒绝,这是我没想到的:

User CN=myuser,OU=org,DC=domain,DC=com is not in group: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

任何想法,如何针对 AD 进行授权完成于devise_ldap_authenticatable?在授权方面,该模块的文档还不够全面。

I successfully have my Rails 3 app running with devise and devise_ldap_authenticatable to authenticate against the local Active Directory.

Now I want to add authorization capabilities in order to allow access only to AD users that belong to certain AD groups.

So to start simple, I first have looked up my own user in AD with the linux command ldapsearch. The result contained something like ...

(...)
memberOf: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
memberOf: CN=my,OU=foo,DC=bar2,DC=role,DC=domain,DC=com
memberOf: (...)
(...)

Ok, now I decided, that I want to restrict access to members of CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com.

So, I changed my ldap.yml to contain:

authorizations: &AUTHORIZATIONS
  group_base: ou=role,dc=domain,dc=com
  required_groups:
    - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

development:
  (...)
  <<: *AUTHORIZATIONS

And in addition changed my devise.rb to contain:

Devise.setup do |config|
  config.ldap_logger = true
  config.ldap_create_user = true
  config.ldap_update_password = false
  config.ldap_check_group_membership = true # <-- activated this line
  config.ldap_use_admin_to_bind = true
  #config.ldap_ad_group_check = true <-- don't know what this is good for

Now, when trying to authenticate, access is denied, which I did not expect:

User CN=myuser,OU=org,DC=domain,DC=com is not in group: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

Any idea, how authorization against AD is accomplished with devise_ldap_authenticatable? The documentation of this module is not yet that comprehensive when it comes to authorization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2025-01-11 08:33:11

注意:代表 OP 添加答案 (@kwirschau)


  1. 更改 devise .rb 激活属性 ldap_check_attributes 并删除/注释掉 ldap_check_group,因为它不检查 AD 特定属性memberOf
  2. 更改 ldap.yml 并注释掉 group_baserequired_groups。将所需的组成员身份添加到 require_attribute

总之,问题中示例的设置如下所示:

# devise.rb

Devise.setup do |config|
  # [ ... ]
  config.ldap_check_attributes = true
  # [ ... ]
end

# ldap.yml

# [ ... ]
authorizations: &AUTHORIZATIONS
  #group_base: ou=role,dc=domain,dc=com
  #required_groups:
  #  - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
  require_attribute:
    memberOf: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
# [ ... ]

Note: Adding an answer on behalf of the OP (@kwirschau)


  1. Change devise.rb to activate the attribute ldap_check_attributes and remove/comment out ldap_check_group as it does not check against the AD-specific attribute memberOf
  2. Change ldap.yml and comment out group_base and required_groups. Add the required group membership to require_attribute.

In summary, the setup for the example in the question looks like this:

# devise.rb

Devise.setup do |config|
  # [ ... ]
  config.ldap_check_attributes = true
  # [ ... ]
end

and

# ldap.yml

# [ ... ]
authorizations: &AUTHORIZATIONS
  #group_base: ou=role,dc=domain,dc=com
  #required_groups:
  #  - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
  require_attribute:
    memberOf: CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com
# [ ... ]
凉月流沐 2025-01-11 08:33:11

因此,zekus 的答案仅在用户直接是给定组的成员时才有效。
它不会递归搜索组。 kwirschau,您的初始配置已经差不多完成了。您指出:

"#config.ldap_ad_group_check = true <-- don't know what this is good for"

设置该标志后,ldap 查询将使用具有 LDAP_MATCHING_RULE_IN_CHAIN 规则的过滤器来搜索嵌套组,该规则递归地搜索用户的组。

因此,在您的 devise.rb 中,设置:

  config.ldap_check_group_membership = true
  config.ldap_use_admin_to_bind = true
  config.ldap_ad_group_check = true

在 ldap.yml 中设置您的授权组

authorizations: &AUTHORIZATIONS
  group_base: ou=role,dc=domain,dc=com
  required_groups:
    - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

此外,在您的代码中,如果您想根据另一个组检查用户,请不要使用“memberOf”,因为这不会搜索嵌套组。相反,使用:

ldap_connect = Devise::LdapAdapter.ldap_connect(username)
ldap_connect.in_group?(group_name)

So, the answer by zekus will only work if the user is directly a member of the given group.
It will not search groups recursively. kwirschau, you were almost there with your initial configuration. You stated:

"#config.ldap_ad_group_check = true <-- don't know what this is good for"

When that flag is set, the ldap query will search nested groups by using a filter with the LDAP_MATCHING_RULE_IN_CHAIN rule, which searches the user's groups recursively.

Thus, in your devise.rb, set:

  config.ldap_check_group_membership = true
  config.ldap_use_admin_to_bind = true
  config.ldap_ad_group_check = true

Set your authorization group in your ldap.yml

authorizations: &AUTHORIZATIONS
  group_base: ou=role,dc=domain,dc=com
  required_groups:
    - CN=my,OU=foo,DC=bar,DC=role,DC=domain,DC=com

Additionally, in your code, if you want to check a user against another group, don't use "memberOf", as this will not search nested groups. Instead, use:

ldap_connect = Devise::LdapAdapter.ldap_connect(username)
ldap_connect.in_group?(group_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文