# Active directory

**URL:** <https://rubytalk.org/t/active-directory/40878>\
**Category:** ruby-talk\
**Created:** [24 September 2007 19:06 UTC](https://rubytalk.org/t/active-directory/40878 "2007-09-24T19:06:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [24 September 2007 19:06 UTC](https://rubytalk.org/t/active-directory/40878/1 "2007-09-24T19:06:06Z")

</div>

Is there any ruby library that will let me authenticate against an  
active directory server from a linux machine? I looked through the AD  
gems but the documentation assumed that I knew what I was doing  
already, and \*seemed\* to imply that I needed to be running this from a  
windows box sitting in a windows domain.

I don't need to run queries or anything like that - simply  
authenticate a user and get a yes/no answer.

martin

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [24 September 2007 19:32 UTC](https://rubytalk.org/t/active-directory/40878/2 "2007-09-24T19:32:26Z")

</div>

Do you want to do an LDAP bind-authentication from Linux with a username and  
password? If so, look at Net::LDAP. If you're trying to do a Kerberos-style  
authentication, there are several NTLM gems you can look at.

> **···**
>
> On 9/24/07, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > Is there any ruby library that will let me authenticate against an  
> > active directory server from a linux machine? I looked through the AD  
> > gems but the documentation assumed that I knew what I was doing  
> > already, and \*seemed\* to imply that I needed to be running this from a  
> > windows box sitting in a windows domain.
> > 
> > I don't need to run queries or anything like that - simply  
> > authenticate a user and get a yes/no answer.
> > 
> > martin

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [26 September 2007 16:08 UTC](https://rubytalk.org/t/active-directory/40878/3 "2007-09-26T16:08:46Z")

</div>

I recently had this problem.

class LDAPAuth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize dn, host, port=389  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dn = dn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c = LDAP::Conn.new host, port  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c.set\_option LDAP::LDAP\_OPT\_PROTOCOL\_VERSION, 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bound = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bind user, pass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bound = !@c.bind(user, pass).nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def groups\_of user, dn=@dn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise Exception, "Not bound." unless @bound  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c.search2(dn, LDAP::LDAP\_SCOPE\_SUBTREE,  
"sAMAccountName=#{user}", ['memberOf']).first['memberOf'].map { |  
e\> /CN=([^,]+?)[,$]/i.match(e).captures.first }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def close  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c.unbind unless @c.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing n, \*a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c.send n, \*a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

Check the line wrapping since it may catch you out. This is a tad quick  
and dirty, but may do the trick. To authenticate like you'd wish:

l = LDAPAuth.new "OU=Accounts,DC=company,DC=com,DC=au",  
"pdc.company.com.au" # note that LDAP won't let you search the root of a  
DN, an OU must be specified  
begin  
&nbsp;&nbsp;&nbsp;&nbsp;l.bind "joe@company.com.au", "mypassword"

&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise Exception, "unauthorized" unless  
l.groups\_of("joe").include? "Enterprise Admins"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# not in the right group!  
&nbsp;&nbsp;&nbsp;&nbsp;end  
rescue  
&nbsp;&nbsp;&nbsp;&nbsp;# credentials are bad!  
&nbsp;&nbsp;&nbsp;&nbsp;...  
end

My example code is fairly ugly, but I hope you get it. Note that we're  
bound to AD with the user's own credentials - there may be a case where  
the user doesn't even have permissions to check their own group  
memberships. In this case, you'd need to authenticate by trying to bind  
with user credentials, but then authorise by binding with some (system)  
account privileged for the purpose of checking memberships like this.  
It's a bit iffy, yes, but there's probably a better way.

Also note this implementation uses the sAMAccountName LDAP attribute for  
looking up a user to determine group membership status; thus a  
non-domain-qualified name is used with LDAPAuth#groups\_of ("joe", not  
"joe@company.com.au"). Your schema may vary!

HTH

Arlen

> **···**
>
> On Tue, 2007-09-25 at 04:06 +0900, Martin DeMello wrote:
> 
> > Is there any ruby library that will let me authenticate against an  
> > active directory server from a linux machine? I looked through the AD  
> > gems but the documentation assumed that I knew what I was doing  
> > already, and \*seemed\* to imply that I needed to be running this from a  
> > windows box sitting in a windows domain.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [24 September 2007 20:39 UTC](https://rubytalk.org/t/active-directory/40878/4 "2007-09-24T20:39:17Z")

</div>

The former. Will check out Net::LDAP - is there anything different I  
have to do, or do I simply treat AD as just another LDAP server?

martin

> **···**
>
> On 9/24/07, Francis Cianfrocca \<garbagecat10@gmail.com\> wrote:
> 
> > Do you want to do an LDAP bind-authentication from Linux with a username and  
> > password? If so, look at Net::LDAP. If you're trying to do a Kerberos-style  
> > authentication, there are several NTLM gems you can look at.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [26 September 2007 17:22 UTC](https://rubytalk.org/t/active-directory/40878/5 "2007-09-26T17:22:41Z")

</div>

Thanks a lot, Arlen, this looks extremely helpful!

martin

> **···**
>
> On 9/26/07, Arlen Christian Mart Cuss \<celtic@sairyx.org\> wrote:
> 
> > class LDAPAuth

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [26 September 2007 18:30 UTC](https://rubytalk.org/t/active-directory/40878/6 "2007-09-26T18:30:52Z")

</div>

Some users have found Net::LDAP simpler because it doesn't require the  
installation of an outboard LDAP library. Here's how the equivalent solution  
might look in Net::LDAP (as you said, "your schema may vary"):

require 'rubygems'  
require 'net/ldap'

ldap = Net::LDAP.new  
ldap.host = your\_server\_ip\_address  
ldap.port = your\_server\_port # typically 389 or 3268 for A/D  
ldap.auth "CN=John Smith,CN=Users,dc=yourcompany,dc=com", "a-password"

ldap.search(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:filter=\>"sAMAccountName=John Smith",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:attributes=\>[:memberOf],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:base=\>"cn=Users,dc=yourcompany,dc=com",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) {|entry|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p entry[:memberof]  
}

> **···**
>
> On 9/26/07, Arlen Christian Mart Cuss \<celtic@sairyx.org\> wrote:
> 
> > On Tue, 2007-09-25 at 04:06 +0900, Martin DeMello wrote:
> > 
> > \> Is there any ruby library that will let me authenticate against an  
> > \> active directory server from a linux machine? I looked through the AD  
> > \> gems but the documentation assumed that I knew what I was doing  
> > \> already, and \*seemed\* to imply that I needed to be running this from a  
> > \> windows box sitting in a windows domain.
> > 
> > I recently had this problem.

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [24 September 2007 21:14 UTC](https://rubytalk.org/t/active-directory/40878/7 "2007-09-24T21:14:40Z")

</div>

You can treat AD as any LDAP server. Look at the Net::LDAP#bind and #bind\_as  
methods.

> **···**
>
> On 9/24/07, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > On 9/24/07, Francis Cianfrocca \<garbagecat10@gmail.com\> wrote:  
> > \>  
> > \> Do you want to do an LDAP bind-authentication from Linux with a username  
> > and  
> > \> password? If so, look at Net::LDAP. If you're trying to do a  
> > Kerberos-style  
> > \> authentication, there are several NTLM gems you can look at.
> > 
> > The former. Will check out Net::LDAP - is there anything different I  
> > have to do, or do I simply treat AD as just another LDAP server?
> > 
> > martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [24 September 2007 21:56 UTC](https://rubytalk.org/t/active-directory/40878/8 "2007-09-24T21:56:00Z")

</div>

thanks.

martin

> **···**
>
> On 9/24/07, Francis Cianfrocca \<garbagecat10@gmail.com\> wrote:
> 
> > You can treat AD as any LDAP server. Look at the Net::LDAP#bind and #bind\_as  
> > methods.

---

<div class="post-metadata">

**Author:** ![Julian\_Julik\_Tarkhan](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@Julian\_Julik\_Tarkhan](https://rubytalk.org/u/Julian_Julik_Tarkhan)\
**Post date:** [24 September 2007 22:02 UTC](https://rubytalk.org/t/active-directory/40878/9 "2007-09-24T22:02:24Z")

</div>

Well, I recently did it the nasty way (I just post to the relevant Remote Web Workplace server for a specific domain).  
Certainly not the proper-clean way to do it, but beats the hell out of setting up the LDAP toolchain, in my book anyways.

> **···**
>
> On Sep 24, 2007, at 11:14 PM, Francis Cianfrocca wrote:
> 
> > You can treat AD as any LDAP server. Look at the Net::LDAP#bind and #bind\_as  
> > methods.

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [24 September 2007 23:41 UTC](https://rubytalk.org/t/active-directory/40878/10 "2007-09-24T23:41:24Z")

</div>

I'm not sure how that would be any easier, or what you mean by setting up an  
LDAP "toolchain" - Net:::LDAP is a self-contained library in pure Ruby. All  
you have to do is install it like any other Ruby library. Calling  
Net::LDAP#bind to authenticate against an LDAP directory or A/D is a  
one-liner. (Two lines if you count require 'net/ldap')

> **···**
>
> On 9/24/07, julik \<listbox@julik.nl\> wrote:
> 
> > On Sep 24, 2007, at 11:14 PM, Francis Cianfrocca wrote:  
> > \> You can treat AD as any LDAP server. Look at the Net::LDAP#bind and  
> > \> #bind\_as  
> > \> methods.
> > 
> > Well, I recently did it the nasty way (I just post to the relevant  
> > Remote Web Workplace server for a specific domain).  
> > Certainly not the proper-clean way to do it, but beats the hell out  
> > of setting up the LDAP toolchain, in my book anyways.
