From da68c8ee8dc55150bc5c7038fae393519cb83d8c Mon Sep 17 00:00:00 2001 From: steev Date: Mon, 4 Dec 2023 14:26:12 +0100 Subject: [PATCH] added db connection to auth server and added usermanagement code --- Auth Server/.env | 8 +++-- Auth Server/database.py | 20 ++++++++++++ Auth Server/index.py | 29 ++++++++++++++--- User Management/.env | 3 ++ User Management/index.py | 70 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 Auth Server/database.py create mode 100644 User Management/.env create mode 100644 User Management/index.py diff --git a/Auth Server/.env b/Auth Server/.env index ffe3ac9..9715ca6 100644 --- a/Auth Server/.env +++ b/Auth Server/.env @@ -1,3 +1,7 @@ HOST="127.0.0.1" -PORT=54321 -ENV="DEV" \ No newline at end of file +PORT=54322 +ENV="DEV" +DBHOST="" +DBUNAME="" +DBPASSWORD="" +DBNAME="" \ No newline at end of file diff --git a/Auth Server/database.py b/Auth Server/database.py new file mode 100644 index 0000000..a43d5a8 --- /dev/null +++ b/Auth Server/database.py @@ -0,0 +1,20 @@ +import psycopg2 + +class Database: + __conn:psycopg2.connect + __cursor:psycopg2.cursor + + def __init__(self, database:str, host:str, user:str, password:str, port:str): + self.__conn = psycopg2.connect(database=database, host=host, user=user, password=password, port=port) + self.__cursor = self.__conn.cursor() + + def getConnection(self) -> psycopg2.connect: + return self.__conn + + def fetchall(self, query:str): + self.__cursor.execute(query) + return self.__cursor.fetchall() + + def fetchall(self, query:str): + self.__cursor.execute(query) + return self.__cursor.fetchone() \ No newline at end of file diff --git a/Auth Server/index.py b/Auth Server/index.py index 6985d17..5966ec6 100644 --- a/Auth Server/index.py +++ b/Auth Server/index.py @@ -29,20 +29,41 @@ def handle_connection(socket:socket, address): # decide which event should be performed if message_json["event"] == "login": # encode message and respond to client - # TODO: Handle login + # TODO: connect to databasse + # TODO: request user data + # TODO: validate user data + # TODO: create session + # TODO: return session to client response = f"" socket.sendall(response.encode()) elif message_json["event"] == "register": # encode message and respond to client - # TODO: Handle registration + # TODO: connect to databasse + # TODO: request user data + # TODO: validate user data + # TODO: create session + # TODO: return session to client response = f"" socket.sendall(response.encode()) elif message_json["event"] == "logout": # encode message and respond to client - # TODO: Handle registration + # TODO: connect to databasse + # TODO: request user data + # TODO: validate user data + # TODO: create session + # TODO: return session to client response = f"" socket.sendall(response.encode()) - + elif message_json["event"] == "sessionrefresh": + # encode message and respond to client + # TODO: connect to databasse + # TODO: request user data + # TODO: validate user data + # TODO: create session + # TODO: return session to client + response = f"" + socket.sendall(response.encode()) + # connection is not required anymore and gets closed socket.close() print(f"connection closed for {address}") diff --git a/User Management/.env b/User Management/.env new file mode 100644 index 0000000..ffe3ac9 --- /dev/null +++ b/User Management/.env @@ -0,0 +1,3 @@ +HOST="127.0.0.1" +PORT=54321 +ENV="DEV" \ No newline at end of file diff --git a/User Management/index.py b/User Management/index.py new file mode 100644 index 0000000..bfb9484 --- /dev/null +++ b/User Management/index.py @@ -0,0 +1,70 @@ +import socket +import threading +import os +import json +from dotenv import load_dotenv + +load_dotenv() + +# retrieves host data from environment +HOST = os.getenv("HOST") +PORT = os.getenv("PORT") + +# TODO: setup tcp service for authorization +def handle_connection(socket:socket, address): + # states that a connection has been established + print(f"Connected with {address}") + + # Communication with client + while True: + data = socket.recv(1024) + if not data: + break + + # decode message for handling + message = data.decode() + message_json = json.loads(message) + print(f"received message from {address}: {message}") + + # decide which event should be performed + if message_json["event"] == "login": + # encode message and respond to client + # TODO: Handle login + # TODO: request user information based on input + # TODO: validate informateion given is valid + # TODO: create session id for user + # TODO: send session information to user + response = f"" + socket.sendall(response.encode()) + elif message_json["event"] == "register": + # encode message and respond to client + # TODO: Handle registration + response = f"" + socket.sendall(response.encode()) + elif message_json["event"] == "logout": + # encode message and respond to client + # TODO: Handle registration + response = f"" + socket.sendall(response.encode()) + + # connection is not required anymore and gets closed + socket.close() + print(f"connection closed for {address}") + +# create tcp socket +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# make socket bind connection info and listen for connections +socket.bind(HOST, PORT) +socket.listen() +print(f"authorization server online on {HOST}:{PORT}") + +# server loop forwards connection to handle_connection +while True: + # accept incomming connection + # TODO: validate this connection is a valid game connection + client_socket, client_address = server_socket.accept() + + # create network thread for connection + client_thread = threading.Thread(target=handle_connection, args=(client_socket, client_address)) + client_thread.start() \ No newline at end of file