diff --git a/Lab_3/Twidder/server.py b/Lab_3/Twidder/server.py index 48c063659d13f34b29c6c693819187d1edbf3ee0..e9bd0bafd9754b385a909ac740b47001668ae5db 100644 --- a/Lab_3/Twidder/server.py +++ b/Lab_3/Twidder/server.py @@ -28,8 +28,7 @@ app = Flask(__name__, static_url_path = '/static')#in case flask does not recogn sock = Sock(app) app.debug = True -session = {'token':'email'} -signedInUser = {'email':'wsObj'} +session = {'token': ("email", "wsObj")} @app.route('/') def root(): @@ -73,34 +72,34 @@ def echo(socket): if not socket: return - # Making sure message format is OK and store email & token in string data = socket.receive() try: myEmail = json.loads(data)["email"] except: - signedInUser[email].close() + # session[token?][1].close() return try: myToken = json.loads(data)["token"] except: - signedInUser[email].close() + # session[token?][1].close() return - print(signedInUser) + print(session) # sign out if I am logged in somewhere else - for email in list(signedInUser.keys()): - if email == myEmail: + for token in list(session.keys()): + if session[token][0] == myEmail and session[token][1] != "": print("You got kicked out") - signedInUser[email].send(json.dumps({"action" : "signOut"})) - signedInUser[email].close() - signedInUser.pop(email) - print(signedInUser) + session[token][1].send(json.dumps({"action" : "signOut"})) + session[token][1].close() + session.pop(token) + + print(session) # Put socket in global dict so server knows my connection is open - signedInUser[myEmail] = socket - print(signedInUser) + session[myToken] = (myEmail, socket) + print(session) socket.send(json.dumps({"action" : "signIn"})) @@ -130,7 +129,7 @@ def sign_in(): # Generate a random token token = str(uuid.uuid4()) - session[token] = email + session[token] = (email, "") # return the token in the Authorization header response = make_response(jsonify({})) #"Server inserted user data into database" @@ -197,15 +196,14 @@ def sign_out(): if tmp[0]: return jsonify({}), tmp[1] - print(signedInUser) + # Close my socket - myEmail = session[token] + print(session) try: - signedInUser[myEmail].close() - signedInUser.pop(myEmail) + session[token][1].close() except: pass # samma sak som ingenting - print(signedInUser) + print(session) # set user to not logged in session.pop(token) @@ -239,7 +237,7 @@ def change_password(): new_password = tmp[2] # Extracting theemail of the current user - email = session[token] + email = session[token][0] # Validation of the old password and attemption to change it to the new one if old_password == database_helper.find_user(email)[1]: #checks if old_password is correct @@ -263,7 +261,7 @@ def get_user_data_by_token(): return jsonify({}), 401 # "User not signed in or invalid access token" # Extracting the email of the current user - email = session[token] + email = session[token][0] return get_user_data_by_email(email) @@ -304,7 +302,7 @@ def get_user_messages_by_token(): return jsonify({}), tmp[1] # Extracting the email of the current user - email = session[token] + email = session[token][0] return get_user_messages_by_email(email) @app.route("/myServer/getMessagesByEmail/<req_email>", methods=['GET']) @@ -353,7 +351,7 @@ def post_message(): return jsonify({}), tmp[1] # Extracting the email of the current user - my_email = session[token] + my_email = session[token][0] # Find out & check email we are posting to tmp = input_has_error('email')