-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
481 lines (381 loc) · 13.5 KB
/
server.py
File metadata and controls
481 lines (381 loc) · 13.5 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
from datetime import timedelta
from functools import wraps
from os import environ
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_jwt_extended import (
JWTManager,
create_access_token,
get_jwt_identity,
verify_jwt_in_request,
get_jwt,
)
from flask_migrate import Migrate
from flask_socketio import SocketIO, join_room, emit
from flask_sqlalchemy import SQLAlchemy
from marshmallow import Schema, fields, post_dump
from sqlalchemy import func, and_, or_
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config["SECRET_KEY"] = (
environ.get("SECRET_KEY")
or "0ac1faff41cfe21eda735af6dfe29e9727a75dbbd88f82c9f66115cd44ca6399"
)
app.config["JWT_SECRET_KEY"] = (
environ.get("JWT_SECRET_KEY")
or "cfad2708c23ab3bb4da83f489eb3bb2a3f12066350b20e8862157cca33b9334a"
)
# definitely don't try this in a real world application.
# I used this to ease the stress of having to think about security and focus on other features.
# Please! You are better off using session which is simpler and battle tested.
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(days=365)
app.config["SQLALCHEMY_DATABASE_URI"] = (
environ.get("DATABASE_URL") or "sqlite:///chatapp.db"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
cors = CORS(app)
db = SQLAlchemy(app)
jwt = JWTManager(app)
migrate = Migrate(app, db)
socketio = SocketIO(app, async_mode="eventlet", logger=True)
users = []
users_conversations = db.Table(
"users_conversations",
db.Column("user_id", db.Integer, db.ForeignKey("users.id")),
db.Column(
"conversation_id",
db.Integer,
db.ForeignKey("conversations.id"),
),
)
def admin_only(f):
@wraps(f)
def decorator(*args, **kwargs):
verify_jwt_in_request()
username = get_jwt()["sub"]
if username != "admin":
# checks if it isn't a socketio connection
if not hasattr(request, "sid"):
return jsonify(status="error", msg="Only admins allowed!"), 403
return socketio.disconnect()
return f(*args, **kwargs)
return decorator
def not_banned(f):
@wraps(f)
def decorator(*args, **kwargs):
verify_jwt_in_request()
username = get_jwt()["sub"]
user = User.query.filter_by(username=username).first()
if user.is_banned:
# checks if it isn't a socketio connection
if not hasattr(request, "sid"):
return (
jsonify(status="error", msg="Sorry, your account has been banned!"),
403,
)
return socketio.disconnect()
return f(*args, **kwargs)
return decorator
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
password_hash = db.Column(db.String(200), nullable=False, unique=True)
ip_address = db.Column(db.String(16), nullable=False)
is_banned = db.Column(db.Boolean, default=False)
messages = db.relationship("Message", backref="sender", lazy="dynamic")
date_joined = db.Column(db.DateTime, default=func.now())
def __init__(self, username, ip, password):
self.username = username
self.ip_address = ip
self.password_hash = generate_password_hash(password)
class UserSerializer(Schema):
id = fields.Int()
username = fields.Str()
password_hash = fields.Str(load_only=True)
ip_address = fields.Str()
is_banned = fields.Bool()
date_joined = fields.DateTime()
class Message(db.Model):
__tablename__ = "messages"
id = db.Column(db.Integer, primary_key=True)
sender_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
conversation_id = db.Column(
db.Integer, db.ForeignKey("conversations.id"), nullable=False
)
content = db.Column(db.LargeBinary(), nullable=False)
timestamp = db.Column(db.DateTime, default=func.now())
class MessageSerializer(Schema):
id = fields.Int()
content = fields.Str()
timestamp = fields.DateTime()
@post_dump(pass_original=True)
def add_sender(self, data, original_data, **kwargs):
sender = UserSerializer().dump(original_data.sender)
data["sender"] = sender
return data
class Conversation(db.Model):
__tablename__ = "conversations"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False, unique=True)
is_private = db.Column(db.Boolean, default=False)
created_on = db.Column(db.DateTime, default=func.now())
participants = db.relationship(
"User",
secondary=users_conversations,
backref=db.backref("conversations", lazy="dynamic"),
lazy="dynamic",
)
messages = db.relationship("Message", backref="conversation", lazy="dynamic")
class ConversationSerializer(Schema):
id = fields.Int()
name = fields.Str()
is_private = fields.Bool()
created_on = fields.DateTime()
participants = fields.Nested(UserSerializer(many=True))
messages = fields.Nested(MessageSerializer(many=True))
@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
identity = jwt_data["sub"]
return User.query.filter_by(username=identity).first()
@app.route("/")
def index():
return jsonify({"message": "Welcome to Chatapp"}), 200
@app.route("/registration", methods=["POST"])
def registration():
req_data = request.get_json(force=True)
if not req_data["username"] or not req_data["password"]:
return jsonify(status="error", message="Invalid"), 400
user = User.query.filter_by(username=req_data["username"]).first()
if user:
return jsonify(status="error", message="Username already in use"), 400
new_user = User(
username=req_data["username"],
password=req_data["password"],
ip=request.remote_addr,
)
db.session.add(new_user)
db.session.commit()
return (
jsonify(
{
"status": "success",
"message": "Your account has been created successfully!",
}
),
201,
)
@app.route("/login", methods=["POST"])
def login():
req_data = request.get_json(force=True)
if not req_data["username"] or not req_data["password"]:
return jsonify({"status": "error", "message": "Invalid"}), 400
user = User.query.filter_by(username=req_data["username"]).first()
if not user or not check_password_hash(user.password_hash, req_data["password"]):
return jsonify(status="error", message="Invalid credentials"), 400
if user.is_banned:
return (
jsonify(status="error", message="Sorry, your account has been banned!"),
403,
)
access_token = create_access_token(identity=user.username)
return (
jsonify(
status="success",
username=user.username,
access_token=access_token,
message="Logged in successfully!",
),
200,
)
@app.route("/channels")
@not_banned
def channels():
all_channels = Conversation.query.filter_by(is_private=False).all()
schema = ConversationSerializer(many=True)
result = schema.dump(all_channels)
return jsonify(status="success", data=result), 200
@app.route("/channels/<name>")
@not_banned
def channel(name):
_channel = Conversation.query.filter(
and_(Conversation.name == name, Conversation.is_private == False)
).first()
user = User.query.filter_by(username=get_jwt_identity()).first()
schema = ConversationSerializer()
if not _channel:
new_channel = Conversation(name=name)
user = User.query.filter_by(username=get_jwt_identity()).first()
new_channel.participants.append(user)
db.session.add(new_channel)
db.session.commit()
result = schema.dump(new_channel)
return jsonify(status="success", data=result), 200
if not _channel.participants.filter(User.id == user.id).count() > 0:
_channel.participants.append(user)
db.session.commit()
result = schema.dump(_channel)
return jsonify(status="success", data=result), 200
@app.route("/privates")
@not_banned
def privates():
user = User.query.filter_by(username=get_jwt_identity()).first()
private_chats = user.conversations.filter_by(is_private=True).all()
schema = ConversationSerializer(many=True)
data = schema.dump(private_chats)
return jsonify(status="success", data=data), 200
@app.route("/privates/<username>")
@not_banned
def private(username):
user = User.query.filter_by(username=username).first()
if not user:
return jsonify(status="error", msg="User not found!"), 404
current_user = User.query.filter_by(username=get_jwt_identity()).first()
private_chat = Conversation.query.filter(
Conversation.is_private == True,
or_(
Conversation.name == f"{current_user.username}-{user.username}",
Conversation.name == f"{user.username}-{current_user.username}",
),
).first()
schema = ConversationSerializer()
if private_chat:
data = schema.dump(private_chat)
return jsonify(status="success", data=data), 200
new_private_chat = Conversation(
name=f"{user.username}-{current_user.username}", is_private=True
)
new_private_chat.participants.extend([user, current_user])
db.session.add(new_private_chat)
db.session.commit()
data = schema.dump(new_private_chat)
return jsonify(status="success", data=data), 200
@app.route("/ban/username/<username>")
@admin_only
def ban_username(username):
user = User.query.filter_by(username=username).first()
if not user:
return (
jsonify(status="error", msg="No user is associated with that username!"),
404,
)
if user.username == "admin":
return (
jsonify(status="error", msg="You cannot ban an admin!"),
400,
)
user.is_banned = True
db.session.commit()
return jsonify(status="success", msg=f"{username} banned!"), 200
@app.route("/ban/ip/<ip_addr>")
@admin_only
def ban_ip_addr(ip_addr):
_users = User.query.filter_by(ip_address=ip_addr).all()
if not _users:
return (
jsonify(status="error", msg="IP Address not associated with any user!"),
404,
)
for user in _users:
if user.username == "admin":
continue
user.is_banned = True
db.session.commit()
return jsonify(status="success", msg=f"{ip_addr} banned!"), 200
@app.route("/stats")
@admin_only
def stats():
_users = User.query.all()
schema = UserSerializer(many=True)
data = schema.dump(_users)
return jsonify(status="success", data=data)
@app.route("/search")
@admin_only
def search_channels():
query = request.args.get("q")
_channel = Conversation.query.filter(
Conversation.is_private == False, Conversation.name == query
).first()
if not _channel:
return jsonify(status="error", msg="Channel not found!"), 404
schema = ConversationSerializer()
data = schema.dump(_channel)
return jsonify(status="success", data=data), 200
@socketio.on("connect")
@not_banned
def connect():
print(f"Connecting: {get_jwt_identity()}, {request.sid}")
users.append({"sid": request.sid, "user_id": get_jwt_identity()})
print(users)
@socketio.on("disconnect")
def disconnect():
print(f"Disconnecting: {request.sid}")
global users
users = list(filter(lambda user: user.get("sid") != request.sid, users))
print(users)
@socketio.on("join-channel")
@not_banned
def join_channel(data):
room = data["channel_name"]
username = data["username"]
join_room(room=room)
emit(
"joined-channel",
{"msg": f"{username} just joined the room."},
room=room,
include_self=False,
)
@socketio.on("join-private-chat")
@not_banned
def join_private_chat(data):
room = data["chat_name"]
username = data["username"]
join_room(room=room)
emit(
"joined-private-chat",
{"msg": f"{username} is now online."},
room=room,
include_self=False,
)
@socketio.on("new-private-message")
@not_banned
def new_private_message(data):
room = data["chat_name"]
sender = data["from"]
message = data["msg"]
user = User.query.filter_by(username=sender).first()
conversation = Conversation.query.filter(
and_(Conversation.name == room, Conversation.is_private == True)
).first()
new_message = Message(
sender_id=user.id, conversation_id=conversation.id, content=message
)
db.session.add(new_message)
db.session.commit()
data = MessageSerializer().dump(new_message)
emit("private-messages", data, room=room)
@socketio.on("new-channel-message")
@not_banned
def new_channel_message(data):
room = data["channel"]
sender = data["from"]
message = data["msg"]
user = User.query.filter_by(username=sender).first()
conversation = Conversation.query.filter(
and_(Conversation.name == room, Conversation.is_private == False)
).first()
new_message = Message(
sender_id=user.id, conversation_id=conversation.id, content=message
)
db.session.add(new_message)
db.session.commit()
data = MessageSerializer().dump(new_message)
emit("channel-messages", data, room=room)
@app.cli.command("run:socketio")
def run_socketio():
"""
Run the eventlet server.
"""
import eventlet
eventlet.monkey_patch()
socketio.run(app)