Skip to content
Rack module for NTLM Authentication
Pull request Compare This branch is 4 commits ahead, 7 commits behind lukefx:master.
Failed to load latest commit information.




Rack middleware for transparent authentication with NTLM.

This is a fork from lukefx/rack-ntlm on Github. This makes the Rack middleware a gem and uses net/ldap to search the user against an ActiveDirectory server.

This is work in progress, so contributions are welcome.

Known Limitations (TO-DOs):

  • Due to the user-provided password not being available on the NTLM Type3 message, this middleware is only verifying the existence of the user on AD, and not binding as that user (which would require password)

  • Failure on the NTLM authentication or LDAP search will simply return 401 with a response body saying “You are not authorized to see this page”


  • rubyntlm (gem install rubyntlm)

  • net/ldap (gem install net-ldap)

Usage (with Rails):

On your config/environment.rb:

config.gem 'rubyntlm', :lib => 'net/ntlm'
config.gem 'net-ldap', :lib => 'net/ldap'
config.gem 'rack-ntlm'

config.middleware.use "Rack::Ntlm", {
  :uri_pattern => /\/login/                       # (default = /\//) (any URL)
  :host => '<Active Directory hostname>',
  :port => 389,                                   # default = 389
  :base => 'Base namespace for LDAP search',
  :search_filter => '(dn=%1)'                     # default = (sAMAccountName=%1)
  :auth => {
    :username => '<username to bind to LDAP>',
    :password => '<password to bind to LDAP>'

Then run:

rake gems:install
rake gems:unpack (optional, if you want to vendor the gem)


When a client needs to authenticate itself to a proxy or server using the NTLM scheme then the following 4-way handshake takes place (only parts of the request and status line and the relevant headers are shown here; “C” is the client, “S” the server):

1: C –> S GET …

2: C <– S 401 Unauthorized

WWW-Authenticate: NTLM

3: C –> S GET …

Authorization: NTLM <base64-encoded type-1-message>

4: C <– S 401 Unauthorized

WWW-Authenticate: NTLM <base64-encoded type-2-message>

5: C –> S GET …

Authorization: NTLM <base64-encoded type-3-message>

6: C <– S 200 Ok


Copyright © 2009-2010 [Rack-Ntlm], released under the MIT license

Something went wrong with that request. Please try again.