Class: Rack::Ntlm

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-ntlm/ntlm.rb,
lib/rack-ntlm/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ Ntlm

Returns a new instance of Ntlm.



6
7
8
9
# File 'lib/rack-ntlm/ntlm.rb', line 6

def initialize(app, config = {})
  @app = app
  @config = config
end

Instance Method Details

#auth(username) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rack-ntlm/ntlm.rb', line 11

def auth(username)
  # Ignore the password. We should probably do something about this
  begin
    if @config[:auth]
      username == @config[:auth][:username]
    else
      true
    end
  rescue => e
    false
  end
end

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack-ntlm/ntlm.rb', line 24

def call(env)
  if env['PATH_INFO'] =~ @config[:uri_pattern] && (env['HTTP_AUTHORIZATION'].nil? || env['HTTP_AUTHORIZATION'] == "")
    return [401, {'WWW-Authenticate' => "NTLM"}, []]
  end

  if /^(NTLM|Negotiate) (.+)/ =~ env["HTTP_AUTHORIZATION"]

    message = Net::NTLM::Message.decode64($2)

    if message.type == 1 
      type2 = Net::NTLM::Message::Type2.new
      return [401, {"WWW-Authenticate" => "NTLM " + type2.encode64}, []]
    end

    if message.type == 3 && env['PATH_INFO'] =~ @config[:uri_pattern]
      user = Net::NTLM::decode_utf16le(message.user)
      if auth(user)
        env['REMOTE_USER'] = user
      else
        return [401, {}, ["You are not authorized to see this page"]]
      end
    end
	end

  @app.call(env)
end