def user_auth(data): logger.warning(f'User {data["userId"]} attempting auth') decode_response = dbUser.decode_auth_token(data["authToken"]) ip = request.remote_addr join_room(ip) if data["userId"] == decode_response: logger.warning('AUTHENTICATED :D') user = dbUser.query.filter(dbUser.id == decode_response).first() if user: for chat in user.chats: join_room(chat.room_name) user.last_ip = ip db.session.add(user) db.session.commit() emit('user_connected', { 'message': 'connected', 'chats': [chat.resp_dict(exceptID=decode_response) for chat in user.chats], }, room=ip) else: emit('reject', {'message': 'Please log back in!'}, room=ip) logger.warning('NOT AUTHENTICATED >:(') return False
def check_auth(request): auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] decode_response = User.decode_auth_token(auth_token) if isinstance(decode_response, str): return False else: return decode_response else: return False
def post(self): # def blacklist_token(auth_token): # blacklisted_token = BlacklistToken(token=auth_token) # try: # db.session.add(blacklisted_token) # db.session.commit() # return { # 'status': 'success', # 'message': 'Successfully logged out' # }, 200 # except Exception as e: # logger.error(e) # return { # 'status': 'fail', # 'message': e # } auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] resp = User.decode_auth_token(auth_token) if isinstance(resp, str): return { 'status': 'success', 'message': resp }, 201 # todo changed from 401 else: # return blacklist_token(auth_token) blacklisted_token = BlacklistToken(token=auth_token) try: db.session.add(blacklisted_token) db.session.commit() return { 'status': 'success', 'message': 'Successfully logged out' }, 200 except Exception as e: logger.error(e) return {'status': 'fail', 'message': e} else: # return blacklist_token(auth_token) return { 'status': 'success', 'message': 'Invalid auth provided' }, 201 # todo changed from 401
def authenticate(*args, **kwargs): auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] decode_response = User.decode_auth_token(auth_token) if isinstance(decode_response, str): return { 'status': 'fail', 'message': f" decode error: {decode_response}" }, 401 else: try: return func(*args, **kwargs) except Exception as e: logger.error(e) return { 'status': 'fail', 'message': f'An error has occurred: {e}' }, 401 else: return {'status': 'fail', 'message': 'Invalid auth provided'}, 401
def post(self): auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] resp = User.decode_auth_token(auth_token) if isinstance(resp, str): return {'status': 'fail', 'message': resp}, 401 else: data = request.json logger.info(f"Comment post data[{type(data)}]: {data}") if data is None: return {'status': 'fail', 'message': 'No data passed'}, 400 else: try: post_id = data['postId'] author_id = data['authorId'] comment = data['comment'] post = dbPost.query.filter( dbPost.id == post_id).first() new_comment = dbComment(post_id=post_id, author_id=author_id, body=comment) post.comments.append(new_comment) db.session.add(new_comment) db.session.add(post) db.session.commit() return { 'status': 'success', 'message': 'comment submitted', }, 200 except Exception as e: logger.error(e) return { 'status': 'fail', 'message': 'An error has occurred', }, 401 else: return {'status': 'fail', 'message': 'Invalid auth provided'}, 401