Skip to content
Snippets Groups Projects
Commit 53ce20ef authored by Anton's avatar Anton
Browse files

trying to fix web socket disconnect

parent 2927d49c
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -51,13 +51,35 @@ def create_response(success, message, data=None): ...@@ -51,13 +51,35 @@ def create_response(success, message, data=None):
def generate_token(): def generate_token():
return secrets.token_urlsafe(32) return secrets.token_urlsafe(32)
def validate_ws(data):
user_identifier = data["email"]
hashed_data = data["hashed_email"]
token = database_helper.get_token(user_identifier)
if token:
token = token[0]
print("ID", user_identifier)
print("token", token)
data_str_plus_token = user_identifier + token
print("data_str_plus_token", data_str_plus_token)
reconstructed_hashed_data = hashlib.sha512(
data_str_plus_token.encode("utf-8")
).hexdigest()
print("hashed_user_id", hashed_data)
print("reconstructed", reconstructed_hashed_data)
if hashed_data == reconstructed_hashed_data:
print("Ws auth successfull")
return True
else:
print("Ws hash missmatch")
print("Ws auth failed")
return False
def validate_request(): def validate_request():
data = request.args.to_dict() if request.method == "GET" else request.form.to_dict() data = request.args.to_dict() if request.method == "GET" else request.form.to_dict()
user_identifier = data.get("user_identifier") user_identifier = data.get("user_identifier")
hashed_data = request.headers["Authorization"] hashed_data = request.headers["Authorization"]
print("normal req hashed data", hashed_data)
token = database_helper.get_token(user_identifier) token = database_helper.get_token(user_identifier)
print("token", token)
if token: if token:
token = token[0] token = token[0]
data_str = "" data_str = ""
...@@ -340,6 +362,9 @@ def web_socket(): ...@@ -340,6 +362,9 @@ def web_socket():
ws = request.environ["wsgi.websocket"] ws = request.environ["wsgi.websocket"]
obj = ws.receive() obj = ws.receive()
data = json.loads(obj) data = json.loads(obj)
if not validate_ws(data):
print("SHOULD RETURN")
return ""
try: try:
active_sockets[data["email"]] = ws active_sockets[data["email"]] = ws
......
...@@ -456,6 +456,7 @@ function xmlRequest(url, callback, params, token = null, requestType) { ...@@ -456,6 +456,7 @@ function xmlRequest(url, callback, params, token = null, requestType) {
if (token) { if (token) {
let hashed_params = hash_params(params, token); let hashed_params = hash_params(params, token);
console.log("REQUEST", hashed_params)
xhttp.setRequestHeader("Authorization", hashed_params); xhttp.setRequestHeader("Authorization", hashed_params);
} }
...@@ -490,10 +491,14 @@ function connectWebSocket() { ...@@ -490,10 +491,14 @@ function connectWebSocket() {
ws.onopen = function () { ws.onopen = function () {
let email = localStorage.getItem("email"); let email = localStorage.getItem("email");
let userData = { email: email }; let token = localStorage.getItem("token");
hashed_email = CryptoJS.SHA512(email + token).toString(CryptoJS.enc.Hex);;
console.log("hashed_email", hashed_email)
let userData = { email: email, hashed_email: hashed_email};
ws.send(JSON.stringify(userData)); ws.send(JSON.stringify(userData));
console.log("Web socket opened"); console.log("Web socket opened");
// ping neccessary when using Heroku because of defualt timout on idle connections
let clock = setInterval(function () { let clock = setInterval(function () {
console.log("Ping server"); console.log("Ping server");
ws.send("ping"); ws.send("ping");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment