@@ -47,7 +47,7 @@ from trac import siteconfig
47
47
from trac .Environment import Environment
48
48
49
49
class DigestAuth :
50
- """A simple HTTP DigestAuth implementation (rfc2069 )"""
50
+ """A simple HTTP DigestAuth implementation (rfc2617 )"""
51
51
MAX_NONCES = 100
52
52
def __init__ (self , htdigest , realm ):
53
53
self .active_nonces = []
@@ -89,7 +89,7 @@ class DigestAuth:
89
89
self .active_nonces = self .active_nonces [- DigestAuth .MAX_NONCES :]
90
90
req .send_response (401 )
91
91
req .send_header ('WWW-Authenticate' ,
92
- 'Digest realm="%s", nonce="%s", stale="%s"'
92
+ 'Digest realm="%s", nonce="%s", qop="auth", stale="%s"'
93
93
% (self .realm , nonce , stale ))
94
94
req .end_headers ()
95
95
@@ -99,17 +99,25 @@ class DigestAuth:
99
99
self .send_auth_request (req )
100
100
return None
101
101
auth = self .parse_auth_header (req .headers ['Authorization' ][7 :])
102
- if not auth .has_key ('username' ) or not auth .has_key ('realm' ) or \
103
- not auth .has_key ('nonce' ) or not auth .has_key ('uri' ) or \
104
- not auth .has_key ('response' ) or \
105
- not self .hash .has_key (auth ['username' ]):
102
+ required_keys = ['username' , 'realm' , 'nonce' , 'uri' , 'response' ,
103
+ 'nc' , 'cnonce' ]
104
+ # Invalid response?
105
+ for key in required_keys :
106
+ if not auth .has_key (key ):
107
+ self .send_auth_request (req )
108
+ return None
109
+ # Unknown user?
110
+ if not self .hash .has_key (auth ['username' ]):
106
111
self .send_auth_request (req )
107
112
return None
113
+
108
114
kd = lambda x : md5 .md5 (':' .join (x )).hexdigest ()
109
115
a1 = self .hash [auth ['username' ]]
110
116
a2 = kd ([req .command , auth ['uri' ]])
111
117
# Is the response correct?
112
- if kd ([a1 , auth ['nonce' ], a2 ]) != auth ['response' ]:
118
+ correct = kd ([a1 , auth ['nonce' ], auth ['nc' ],
119
+ auth ['cnonce' ], auth ['qop' ], a2 ])
120
+ if auth ['response' ] != correct :
113
121
self .send_auth_request (req )
114
122
return None
115
123
# Is the nonce active, if not ask the client to use a new one
0 commit comments