28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/ramaze/helper/httpdigest.rb', line 28
def httpdigest(uid, realm)
session[ SESSION_OPAQUE ] ||= {}
session[ SESSION_OPAQUE ][ realm ] ||= {}
if request.env['HTTP_AUTHORIZATION']
authorized = false
if session[ SESSION_NONCE ] and session[ SESSION_OPAQUE ][ realm ][ uid ]
auth_split = request.env['HTTP_AUTHORIZATION'].split
authentication_type = auth_split[0]
authorization = Rack::Auth::Digest::Params.parse( auth_split[1..-1].join(' ') )
digest_response, username, nonce, nc, cnonce, qop, opaque =
authorization.values_at(*%w[response username nonce nc cnonce qop opaque])
if authentication_type == 'Digest'
if nonce == session[SESSION_NONCE] and opaque == session[SESSION_OPAQUE][realm][uid]
h1 = nil
if respond_to?( :httpdigest_lookup_password )
ha1 = httpdigest_lookup_password( username )
else
if respond_to?( :httpdigest_lookup_plaintext_password )
ha1 = MD5.hexdigest( "#{username}:#{realm}:#{httpdigest_lookup_plaintext_password( username )}" )
else
if block_given?
ha1 = yield( username )
else
raise "No password lookup handler found"
end
end
end
ha2 = MD5.hexdigest([request.request_method,request.fullpath].join(':'))
md5 = MD5.hexdigest([ha1, nonce, nc, cnonce, qop, ha2].join(':'))
authorized = digest_response == md5
end
end
end
unless authorized
( uid, realm )
respond('Unauthorized', 401)
end
else
( uid, realm )
httpdigest_failure if respond_to?( :httpdigest_failure )
respond('Unauthorized', 401)
end
authorization["username"]
end
|