-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.py
More file actions
247 lines (187 loc) · 6.84 KB
/
httpserver.py
File metadata and controls
247 lines (187 loc) · 6.84 KB
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
246
247
"""
Copyright (c) 2016 S-BEAT GbR and others
This file is part of S-BEAT.
S-BEAT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
S-BEAT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with S-BEAT. If not, see <http://www.gnu.org/licenses/>.
"""
import re
import sys
sys.path.append('lib')
from functools import wraps
from ConfigParser import RawConfigParser
from flask import Flask, abort, render_template, request, Response, g, send_file
import logging
import markdown
import codecs
import UserTools
import WebAPI
import Version
import DB
import StudentsWebAPI
config = RawConfigParser()
config.read('config/main.cfg')
logging.basicConfig(filename=config.get('http', 'logfile'), level=logging.INFO)
# logging.getLogger('').addHandler(logging.StreamHandler())
port = config.getint('http', 'port')
host = config.get('http', 'host')
debug = True if config.get('http', 'debug') == 'true' else False
document_root = config.get('http', 'document_root')
upload_folder = config.get('http', 'upload_folder')
if config.has_section('web'):
web_config = dict(config.items('web'))
else:
web_config = dict()
# check which authentication methods should be used
authentication_header = True
username_header = False
if config.has_option('http', 'authentication_header'):
authentication_header = config.getboolean('http', 'authentication_header')
if config.has_option('http', 'username_header'):
username_header = config.getboolean('http', 'username_header')
realm = 'S-BEAT Gesicherter Bereich'
app = Flask(__name__, static_url_path='', static_folder=document_root, template_folder=document_root)
app.config['UPLOAD_FOLDER'] = upload_folder
UserTools.set_user_roles_by_config(config)
def has_right(required_role):
return UserTools.has_right(required_role, g.user_role)
@app.before_request
def before_request():
g.user_role = 'guest'
g.has_right = has_right
g.user = None
g.username = ''
g.web_config = web_config
g.students_view = config.has_section('http_students')
g.settings = DB.Settings.load_dict([
'cp_label',
'student_list_limit',
'contact_software',
'contact_hosting',
'contact_data',
'import_applicants',
'privacy_notice'
])
g.version = Version.get_string()
g.logo = None
if config.has_section('logo'):
g.logo = {}
for key, value in config.items('logo'):
g.logo[key] = value.decode('utf-8')
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
render_template('unauthorized.html'), 401,
{'WWW-Authenticate': 'Basic realm="' + realm + '"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
username = None
if authentication_header and auth is not None:
username = auth.username
if username_header and 'x-remote-user' in request.headers:
username = request.headers['x-remote-user']
if username is not None:
user = UserTools.get_user(username)
g.user = user
if user:
g.username = username
g.user_role = user['role']
return f(*args, **kwargs)
return authenticate()
return decorated
@app.route('/')
@app.route('/index.html')
@requires_auth
def index():
return render_template('index.html')
@app.route('/logout.html')
def logout():
auth = request.authorization
logout_user = request.args.get('user')
if auth is not None and logout_user == auth.username:
return authenticate()
else:
response = Response(
'<p>You should be redirected to URL: '
'<a href="%s">%s</a>.' %
('/index.html', '/index.html'), 302, mimetype='text/html')
response.autocorrect_location_header = False
response.headers['Location'] = '/index.html'
return response
@app.route('/LICENSE.md')
def license():
with codecs.open('LICENSE.md', mode='r', encoding='utf-8') as fd:
return Response(markdown.markdown(fd.read()), 200, mimetype='text/html')
@app.route('/Datenspezifikation.md')
def data_specification():
with codecs.open('Datenspezifikation.md', mode="r", encoding="utf-8") as fd:
return Response(markdown.markdown(fd.read()), 200, mimetype='text/html')
@app.route('/gpl.txt')
def gpl():
return send_file('gpl.txt', 'text/plain')
@app.route('/students_view_<student_ident>.html')
@requires_auth
def students_view(student_ident):
if not UserTools.has_right('students_data', g.user_role):
return Response('No Permission', 403)
return render_template('students_view/student_details.html')
@app.route('/api/get_current_students_data')
@requires_auth
def handle_students_view_api_request():
if not UserTools.has_right('students_data', g.user_role):
return Response('No Permission', 403)
if not request.headers['referer']:
return Response('No Referer', 403)
m = re.search('students_view_(.+?)\\.html', request.headers['referer'])
if m is None:
return Response('No Student ident found', 404)
g.settings = DB.Settings.load_dict([
'hide_finished_ident_data',
'student_ident_string'
])
ident = m.group(1)
if not g.settings['student_ident_string']:
ident = int(ident)
g.student = DB.Student.find_one({'_id': ident})
if g.student is None:
return Response('No Student found with ident ' + ident, 404)
return StudentsWebAPI.GetData.handle()
@app.route('/<filename>.html')
@requires_auth
def html(filename):
if re.match('^[-a-zA-Z0-9_]{1,30}$', filename):
return render_template(filename + '.html')
else:
return Response('Illegal file name', 400)
@app.route('/api/<endpoint>', methods=['POST', 'GET'])
@requires_auth
def handle_api_request(endpoint):
if not hasattr(WebAPI, endpoint):
abort(404)
endpoint = getattr(WebAPI, endpoint)
if not hasattr(endpoint, 'handle'):
abort(501)
get_temp = request.args.get('temp', default='false') == 'true'
if get_temp:
DB.enable_temp_data()
try:
response = endpoint.handle()
if get_temp:
DB.disable_temp_data()
except:
if get_temp:
DB.disable_temp_data()
raise
return response
if __name__ == '__main__':
print 'Starting Webserver on ', 'http://' + host + ':' + str(port) + '/'
app.run(host=host, port=port, debug=debug, use_reloader=True)