-
Notifications
You must be signed in to change notification settings - Fork 0
/
flack.py
280 lines (229 loc) · 8.01 KB
/
flack.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Flack sequence form session with Miguel Grinberb - Pycon 2016
#!/usr/bin/env python
import os
import threading
import time
from flask import Flask, render_template, request, abort, jsonify, g
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
from flask_bootstrap import Bootstrap
from utils import timestamp, url_for
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = '51f52814-0071-11e6-a247-000ec6c2372c'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'DATABASE_URL', 'sqlite:///' + os.path.join(basedir, 'db.sqlite'))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Flask extensions
db = SQLAlchemy(app)
Bootstrap(app)
# Authentication objects for username/password auth, token auth, and a
# token optional auth that is used for open endpoints.
basic_auth = HTTPBasicAuth()
token_auth = HTTPTokenAuth('Bearer')
token_optional_auth = HTTPTokenAuth('Bearer')
# We use a list to calculate requests per second
request_stats = []
# Import models so that they are registered with SQLAlchemy
from models import User, Message
@basic_auth.verify_password
def verify_password(nickname, password):
"""Password verification callback."""
user = User.query.filter_by(nickname=nickname).first()
if user is None or not user.verify_password(password):
return False
user.ping()
db.session.add(user)
db.session.commit()
g.current_user = user
return True
@basic_auth.error_handler
def password_error():
"""Return a 401 error to the client."""
# To avoid login prompts in the browser, use the "Bearer" realm.
return (jsonify({'error': 'authentication required'}), 401,
{'WWW-Authenticate': 'Bearer realm="Authentication Required"'})
@token_auth.verify_token
def verify_token(token):
"""Token verification callback."""
user = User.query.filter_by(token=token).first()
if user is None:
return False
user.ping()
db.session.add(user)
db.session.commit()
g.current_user = user
return True
@token_auth.error_handler
def token_error():
"""Return a 401 error to the client."""
return (jsonify({'error': 'authentication required'}), 401,
{'WWW-Authenticate': 'Bearer realm="Authentication Required"'})
@token_optional_auth.verify_token
def verify_optional_token(token):
"""Alternative token authentication that allows anonymous logins."""
if token == '':
# no token provided, mark the logged in users as None and continue
g.current_user = None
return True
# but if a token was provided, make sure it is valid
return verify_token(token)
@app.before_first_request
def before_first_request():
"""Start a background thread that looks for users that leave."""
def find_offline_users():
while True:
User.find_offline_users()
db.session.remove()
time.sleep(5)
if not app.config['TESTING']:
thread = threading.Thread(target=find_offline_users)
thread.start()
@app.before_request
def before_request():
"""Update requests per second stats."""
t = timestamp()
while len(request_stats) > 0 and request_stats[0] < t - 15:
del request_stats[0]
request_stats.append(t)
@app.route('/')
def index():
"""Serve client-side application."""
return render_template('index.html')
@app.route('/api/users', methods=['POST'])
def new_user():
"""
Register a new user.
This endpoint is publicly available.
"""
user = User.create(request.get_json() or {})
if User.query.filter_by(nickname=user.nickname).first() is not None:
abort(400)
db.session.add(user)
db.session.commit()
r = jsonify(user.to_dict())
r.status_code = 201
r.headers['Location'] = url_for('get_user', id=user.id)
return r
@app.route('/api/users', methods=['GET'])
@token_optional_auth.login_required
def get_users():
"""
Return list of users.
This endpoint is publicly available, but if the client has a token it
should send it, as that indicates to the server that the user is online.
"""
users = User.query.order_by(User.updated_at.asc(), User.nickname.asc())
if request.args.get('online'):
users = users.filter_by(online=(request.args.get('online') != '0'))
if request.args.get('updated_since'):
users = users.filter(
User.updated_at > int(request.args.get('updated_since')))
return jsonify({'users': [user.to_dict() for user in users.all()]})
@app.route('/api/users/<id>', methods=['GET'])
@token_optional_auth.login_required
def get_user(id):
"""
Return a user.
This endpoint is publicly available, but if the client has a token it
should send it, as that indicates to the server that the user is online.
"""
return jsonify(User.query.get_or_404(id).to_dict())
@app.route('/api/users/<id>', methods=['PUT'])
@token_auth.login_required
def edit_user(id):
"""
Modify an existing user.
This endpoint is requires a valid user token.
Note: users are only allowed to modify themselves.
"""
user = User.query.get_or_404(id)
if user != g.current_user:
abort(403)
user.from_dict(request.get_json() or {})
db.session.add(user)
db.session.commit()
return '', 204
@app.route('/api/tokens', methods=['POST'])
@basic_auth.login_required
def new_token():
"""
Request a user token.
This endpoint is requires basic auth with nickname and password.
"""
if g.current_user.token is None:
g.current_user.generate_token()
db.session.add(g.current_user)
db.session.commit()
return jsonify({'token': g.current_user.token})
@app.route('/api/tokens', methods=['DELETE'])
@token_auth.login_required
def revoke_token():
"""
Revoke a user token.
This endpoint is requires a valid user token.
"""
g.current_user.token = None
db.session.add(g.current_user)
db.session.commit()
return '', 204
@app.route('/api/messages', methods=['POST'])
@token_auth.login_required
def new_message():
"""
Post a new message.
This endpoint is requires a valid user token.
"""
msg = Message.create(request.get_json() or {})
db.session.add(msg)
db.session.commit()
r = jsonify(msg.to_dict())
r.status_code = 201
r.headers['Location'] = url_for('get_message', id=msg.id)
return r
@app.route('/api/messages', methods=['GET'])
@token_optional_auth.login_required
def get_messages():
"""
Return list of messages.
This endpoint is publicly available, but if the client has a token it
should send it, as that indicates to the server that the user is online.
"""
since = int(request.args.get('updated_since', '0'))
day_ago = timestamp() - 24 * 60 * 60
if since < day_ago:
# do not return more than a day worth of messages
since = day_ago
msgs = Message.query.filter(Message.updated_at > since).order_by(
Message.updated_at)
return jsonify({'messages': [msg.to_dict() for msg in msgs.all()]})
@app.route('/api/messages/<id>', methods=['GET'])
@token_optional_auth.login_required
def get_message(id):
"""
Return a message.
This endpoint is publicly available, but if the client has a token it
should send it, as that indicates to the server that the user is online.
"""
return jsonify(Message.query.get_or_404(id).to_dict())
@app.route('/api/messages/<id>', methods=['PUT'])
@token_auth.login_required
def edit_message(id):
"""
Modify an existing message.
This endpoint is requires a valid user token.
Note: users are only allowed to modify their own messages.
"""
msg = Message.query.get_or_404(id)
if msg.user != g.current_user:
abort(403)
msg.from_dict(request.get_json() or {})
db.session.add(msg)
db.session.commit()
return '', 204
@app.route('/stats', methods=['GET'])
def get_stats():
return jsonify({'requests_per_second': len(request_stats) / 15})
if __name__ == '__main__':
db.create_all()
app.run(host='0.0.0.0', debug=True)