-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
70 lines (62 loc) · 1.92 KB
/
auth.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
import os
from hashlib import md5
import cherrypy
from reprconf import Config
from couchdbkit import *
from couchdbkit_mapper import *
config_file = os.path.join(os.path.dirname(__file__), "../conf/mg.conf") # that's hardcoded, because this module is invoked by Apache
config = Config({
'couch': {
'url': 'http://localhost:5984',
'user': None,
'password': None,
'database': {'quests': 'quests', 'zt': 'zt', 'users': 'users'}
}
})
another_config = Config(config_file)
try:
config['couch'].update(another_config['couch'])
except KeyError:
pass
config = config['couch']
class User(object):
def __init__(self, _id, password, privileges):
self._id = _id
self.password = password
self.privileges = privileges
server = Server(config['url'])
db_name = config['database']['users']
users = map(server[db_name])
users.add(User)
def get_realm_hash(environ, user_name, realm):
try:
# user:realm:password
user = users[user_name]
try:
return user.hashes[realm]
except (KeyError, AttributeError):
value = md5()
try:
value.update(':'.join([user_name, realm, user.password]))
except AttributeError:
return None
hash = value.hexdigest()
try:
user.hashes[realm] = hash
except AttributeError:
user.hashes = {realm: hash}
users[user_name] = user
return hash
except ResourceNotFound:
return None
ANONYMOUS = None
def current_user():
"Get authorisation from HTTP header information."
try:
header = cherrypy.request.headers['Authorization']
length = 17 # length of 'Digest username="'
start = header.find('Digest username="')
end = header.find('"', start+length)
return header[start+length:end]
except KeyError:
return ANONYMOUS