-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworknoteBookServer_AuthModule.py
210 lines (187 loc) · 7.39 KB
/
worknoteBookServer_AuthModule.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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 13:02:05 2015
@author: appel
Originally copied from:
http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions
"""
import cherrypy
import urllib
def print_enter(name):
print '-'*len(name)
print name
print '-'*len(name)
SESSION_KEY = '_cp_username'
def check_credentials(username, password, auth_file_fn):
"""Verifies credentials for username and password.
Returns None on success or a string describing the error on failure"""
print_enter('check_credentials')
import md5
users = {}
try:
print 'Reading user dict...'
with open(auth_file_fn, 'r') as auth_file:
for line in auth_file:
try:
user, passwd = line.split()
users[user] = passwd
except ValueError:
pass
except IOError:
print 'Auth file not found'
return 'Auth file not found'
print 'Username:', username
if username in users:
if users[username] == password:
print 'User/pass matched'
return None
else:
print 'Trying MD5 hash...'
password = md5.new(password).hexdigest()
if users[username] == password:
print 'User/pass matched'
return None
else:
print 'Incorrect password'
return 'Incorrect password'
else:
print 'Unknown user'
return 'Unknown user'
def check_auth(*args, **kwargs):
"""A tool that looks in config for 'auth.require'. If found and it
is not None, a login is required and the entry is evaluated as alist of
conditions that the user must fulfill"""
print_enter('check_auth')
conditions = cherrypy.request.config.get('auth.require', None)
# format GET params
get_params = urllib.quote(cherrypy.request.request_line.split()[1])
if conditions is not None:
username = cherrypy.session.get(SESSION_KEY)
if username:
cherrypy.request.login = username
for condition in conditions:
# A condition is just a callable that returns true orfalse
if not condition():
# Send old page as from_page parameter
raise cherrypy.HTTPRedirect("/auth/login?from_page=%s" % get_params)
else:
# Send old page as from_page parameter
raise cherrypy.HTTPRedirect("/auth/login?from_page=%s" % get_params)
cherrypy.tools.auth = cherrypy.Tool('before_handler', check_auth)
def require(*conditions):
"""A decorator that appends conditions to the auth.require config
variable."""
def decorate(f):
if not hasattr(f, '_cp_config'):
f._cp_config = dict()
if 'auth.require' not in f._cp_config:
f._cp_config['auth.require'] = []
f._cp_config['auth.require'].extend(conditions)
return f
return decorate
def member_of(groupname):
def check():
# replace with actual check if <username> is in <groupname>
#return cherrypy.request.login == 'joe' and groupname == 'admin'
return False
return check
def name_is(reqd_username):
return lambda: reqd_username == cherrypy.request.login
# These might be handy
def any_of(*conditions):
"""Returns True if any of the conditions match"""
def check():
for c in conditions:
if c():
return True
return False
return check
# By default all conditions are required, but this might still be
# needed if you want to use it inside of an any_of(...) condition
def all_of(*conditions):
"""Returns True if all of the conditions match"""
def check():
for c in conditions:
if not c():
return False
return True
return check
# Controller to provide login and logout actions
class AuthController(object):
def __init__(self, auth_file = '', head='', foot='', staticdir=''):
from worknoteBookServer import StaticDir
self.auth_file = auth_file
self.head = head
self.foot = foot
self.staticdir = staticdir
cherrypy.tree.mount(StaticDir(), '/auth/static', config = {'/': {
'tools.staticdir.on': True,
'tools.staticdir.root': self.staticdir,
'tools.staticdir.dir': '.'
}})
self.logged_in = None
def on_login(self, username):
"""Called on successful login"""
self.logged_in = username
def on_logout(self, username):
"""Called on logout"""
self.logged_in = None
def get_loginform(self, username, msg="Enter login information", from_page="/"):
print_enter('AuthController.get_loginform')
head = self.head.format(metadata='<title>worknoteBook - Login</title>\n')
foot = self.foot.format()
frame = """{head:s}
<form method="post" action="/auth/login">
<input type="hidden" name="from_page" value="{from_page:s}" />
<b>{msg:s}</b><br/>
Username: <input type="text" name="username" value="{username:s}" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Log in" />
{foot:s}"""
return frame.format(head=head, foot=foot, from_page=from_page,
msg=msg, username=username)
@cherrypy.expose
def login(self, username=None, password=None, from_page="/"):
print_enter('AuthController.login')
from base64 import b64decode
if 'Python-urllib' in cherrypy.request.headers['User-Agent']:
print 'CLI client header found'
if 'Authorization' in cherrypy.request.headers:
print 'Authorization header found, parsing...'
auth_header = cherrypy.request.headers['Authorization']
auth_header = b64decode(auth_header)
username, password = auth_header.split(':')
cli_client = True
else:
cli_client = False
if username is None or password is None:
if not cli_client:
return self.get_loginform("", from_page=from_page)
else:
raise cherrypy.HTTPError("403 Forbidden", "Login needed for this action")
print 'Username:', username
print 'Checking credentials...'
error_msg = check_credentials(username, password, self.auth_file)
if error_msg:
print 'Login unsuccessful'
if not cli_client:
return self.get_loginform(username, error_msg, from_page)
else:
raise cherrypy.HTTPError("403 Forbidden", "Login needed for this action")
else:
print 'Login successful'
cherrypy.session.regenerate()
cherrypy.session[SESSION_KEY] = cherrypy.request.login = username
self.on_login(username)
if not cli_client:
raise cherrypy.HTTPRedirect(from_page or "/")
@cherrypy.expose
def logout(self, from_page="/"):
print_enter('AuthController.logout')
sess = cherrypy.session
username = sess.get(SESSION_KEY, None)
sess[SESSION_KEY] = None
if username:
cherrypy.request.login = None
self.on_logout(username)
raise cherrypy.HTTPRedirect(from_page or "/")