class Rack::Ntlm

Public Class Methods

new(app, config = {}) click to toggle source
   # File lib/rack/ntlm.rb
 8 def initialize(app, config = {})
 9   @app = app
10   @config = {
11     :uri_pattern => /\//,
12     :port => 389,
13     :search_filter => "(sAMAccountName=%1)"
14   }.merge(config)
15 end

Public Instance Methods

auth(user) click to toggle source
   # File lib/rack/ntlm.rb
17 def auth(user)
18   ldap = Net::LDAP.new
19   ldap.host = @config[:host]
20   ldap.port = @config[:port]
21   ldap.base = @config[:base]
22   ldap.auth @config[:auth][:username], @config[:auth][:password] if @config[:auth]
23   !ldap.search(:filter => @config[:search_filter].gsub("%1", user)).empty?
24 rescue => e
25   false
26 end
call(env) click to toggle source
   # File lib/rack/ntlm.rb
28 def call(env)
29   if env['PATH_INFO'] =~ @config[:uri_pattern] && env['HTTP_AUTHORIZATION'].blank?
30     return [401, {'WWW-Authenticate' => "NTLM"}, []]
31   end
32 
33   if /^(NTLM|Negotiate) (.+)/ =~ env["HTTP_AUTHORIZATION"]
34 
35     message = Net::NTLM::Message.decode64($2)
36 
37     if message.type == 1 
38       type2 = Net::NTLM::Message::Type2.new
39       return [401, {"WWW-Authenticate" => "NTLM " + type2.encode64}, []]
40     end
41 
42     if message.type == 3 && env['PATH_INFO'] =~ @config[:uri_pattern]
43       user = Net::NTLM::decode_utf16le(message.user)
44       if auth(user)
45         env['REMOTE_USER'] = user 
46       else
47         return [401, {}, ["You are not authorized to see this page"]]
48       end
49     end
50     end
51 
52   @app.call(env)
53 end