-
Notifications
You must be signed in to change notification settings - Fork 17
/
authdigest.py
245 lines (190 loc) · 7.57 KB
/
authdigest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-
"""
werkzeug.contrib.authdigest
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The authdigest module contains classes to support
digest authentication compliant with RFC 2617.
Usage
=====
::
from werkzeug.contrib.authdigest import RealmDigestDB
authDB = RealmDigestDB('test-realm')
authDB.add_user('admin', 'test')
def protectedResource(environ, start_reponse):
request = Request(environ)
if not authDB.isAuthenticated(request):
return authDB.challenge()
return get_protected_response(request)
:copyright: (c) 2010 by the Werkzeug Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os
import weakref
import hashlib
import werkzeug
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Realm Digest Credentials Database
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class RealmDigestDB(object):
"""Database mapping user to hashed password.
Passwords are hashed using realm key, and specified
digest algorithm.
:param realm: string identifing the hashing realm
:param algorthm: string identifying hash algorithm to use,
default is 'md5'
"""
def __init__(self, realm, algorithm='md5'):
self.realm = realm
self.alg = self.newAlgorithm(algorithm)
self.db = self.newDB()
@property
def algorithm(self):
return self.alg.algorithm
def toDict(self):
r = {'cfg':{ 'algorithm': self.alg.algorithm,
'realm': self.realm},
'db': self.db, }
return r
def toJson(self, **kw):
import json
kw.setdefault('sort_keys', True)
kw.setdefault('indent', 2)
return json.dumps(self.toDict(), **kw)
def add_user(self, user, password):
r = self.alg.hashPassword(user, self.realm, password)
self.db[user] = r
return r
def __contains__(self, user):
return user in self.db
def get(self, user, default=None):
return self.db.get(user, default)
def __getitem__(self, user):
return self.db.get(user)
def __setitem__(self, user, password):
return self.add_user(user, password)
def __delitem__(self, user):
return self.db.pop(user, None)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def newDB(self):
return dict()
def newAlgorithm(self, algorithm):
return DigestAuthentication(algorithm)
def isAuthenticated(self, request, **kw):
authResult = AuthenticationResult(self)
request.authentication = authResult
authorization = request.authorization
if authorization is None:
return authResult.deny('initial', None)
authorization.result = authResult
hashPass = self[authorization.username]
if hashPass is None:
return authResult.deny('unknown_user')
elif not self.alg.verify(authorization, hashPass, method=request.method, **kw):
return authResult.deny('invalid_password')
else:
return authResult.approve('success')
challenge_class = werkzeug.Response
def challenge(self, response=None, status=401):
try:
authReq = response.www_authenticate
except AttributeError:
response = self.challenge_class(response, status)
authReq = response.www_authenticate
else:
if isinstance(status, (int, long)):
response.status_code = status
else: response.status = status
authReq.set_digest(self.realm, os.urandom(8).encode('hex'))
return response
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Authentication Result
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class AuthenticationResult(object):
"""Authentication Result object
Created by RealmDigestDB.isAuthenticated to operate as a boolean result,
and storage of authentication information."""
authenticated = None
reason = None
status = 500
def __init__(self, authDB):
self.authDB = weakref.ref(authDB)
def __repr__(self):
return '<authenticated: %r reason: %r>' % (
self.authenticated, self.reason)
def __nonzero__(self):
return bool(self.authenticated)
def deny(self, reason, authenticated=False):
if bool(authenticated):
raise ValueError("Denied authenticated parameter must evaluate as False")
self.authenticated = authenticated
self.reason = reason
self.status = 401
return self
def approve(self, reason, authenticated=True):
if not bool(authenticated):
raise ValueError("Approved authenticated parameter must evaluate as True")
self.authenticated = authenticated
self.reason = reason
self.status = 200
return self
def challenge(self, response=None, force=False):
if force or not self:
return self.authDB().challenge(response, self.status)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Digest Authentication Algorithm
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class DigestAuthentication(object):
"""Digest Authentication implementation.
references:
"HTTP Authentication: Basic and Digest Access Authentication". RFC 2617.
http://tools.ietf.org/html/rfc2617
"Digest access authentication"
http://en.wikipedia.org/wiki/Digest_access_authentication
"""
def __init__(self, algorithm='md5'):
self.algorithm = algorithm.lower()
self.H = self.hashAlgorithms[self.algorithm]
def verify(self, authorization, hashPass=None, **kw):
reqResponse = self.digest(authorization, hashPass, **kw)
if reqResponse:
return (authorization.response.lower() == reqResponse.lower())
def digest(self, authorization, hashPass=None, **kw):
if authorization is None:
return None
if hashPass is None:
hA1 = self._compute_hA1(authorization, kw['password'])
else: hA1 = hashPass
hA2 = self._compute_hA2(authorization, kw.pop('method', 'GET'))
if 'auth' in authorization.qop:
res = self._compute_qop_auth(authorization, hA1, hA2)
elif not authorization.qop:
res = self._compute_qop_empty(authorization, hA1, hA2)
else:
raise ValueError("Unsupported qop: %r" % (authorization.qop,))
return res
def hashPassword(self, username, realm, password):
return self.H(username, realm, password)
def _compute_hA1(self, auth, password=None):
return self.hashPassword(auth.username, auth.realm, password or auth.password)
def _compute_hA2(self, auth, method):
return self.H(method, auth.uri)
def _compute_qop_auth(self, auth, hA1, hA2):
return self.H(hA1, auth.nonce, auth.nc, auth.cnonce, auth.qop, hA2)
def _compute_qop_empty(self, auth, hA1, hA2):
return self.H(hA1, auth.nonce, hA2)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hashAlgorithms = {}
@classmethod
def addDigestHashAlg(klass, key, hashObj):
key = key.lower()
def H(*args):
x = ':'.join(map(str, args))
return hashObj(x).hexdigest()
H.__name__ = "H_"+key
klass.hashAlgorithms[key] = H
return H
DigestAuthentication.addDigestHashAlg('md5', hashlib.md5)
DigestAuthentication.addDigestHashAlg('sha', hashlib.sha1)