Skip to content

Commit a154ca9

Browse files
committed
fixes in init.sql and added clear of session when the token is rejected
1 parent b7e3869 commit a154ca9

4 files changed

Lines changed: 46 additions & 25 deletions

File tree

database/init.sql

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
CREATE TABLE IF NOT EXISTS Participants (
22
pid SERIAL PRIMARY KEY,
3-
first_name VARCHAR(50) NOT NULL,
4-
last_name VARCHAR(50) NOT NULL,
3+
first_name TEXT NOT NULL,
4+
last_name TEXT NOT NULL,
55
age INT NOT NULL,
6-
gender VARCHAR(15) NOT NULL,
7-
phone VARCHAR(15) NOT NULL,
8-
email VARCHAR(100) UNIQUE NOT NULL
6+
gender TEXT NOT NULL,
7+
phone TEXT NOT NULL,
8+
email TEXT UNIQUE NOT NULL
99
);
1010

1111
CREATE TABLE IF NOT EXISTS Experiments (
@@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS Experiments (
1919
CREATE TABLE IF NOT EXISTS ExperimentsFeedback (
2020
exp_id INT,
2121
pid INT,
22-
question VARCHAR(100),
23-
answer VARCHAR(300) NOT NULL,
22+
question TEXT,
23+
answer TEXT NOT NULL,
2424
PRIMARY KEY (exp_id, pid, question),
2525
FOREIGN KEY (pid) REFERENCES Participants(pid),
2626
FOREIGN KEY (exp_id) REFERENCES Experiments(exp_id)
@@ -30,19 +30,19 @@ CREATE TABLE IF NOT EXISTS Sessions (
3030
session_id SERIAL PRIMARY KEY,
3131
exp_id INT,
3232
duration INT NOT NULL,
33-
session_type VARCHAR(50) NOT NULL,
33+
session_type TEXT NOT NULL,
3434
session_order INT NOT NULL,
3535
tolerance INT NOT NULL,
3636
window_length INT NOT NULL,
37-
state VARCHAR(50) NOT NULL,
37+
state TEXT NOT NULL,
3838
FOREIGN KEY (exp_id) REFERENCES Experiments(exp_id)
3939
);
4040

4141
CREATE TABLE IF NOT EXISTS SessionsFeedback (
4242
session_id INT,
4343
pid INT,
44-
question VARCHAR(100),
45-
answer VARCHAR(300) NOT NULL,
44+
question TEXT,
45+
answer TEXT NOT NULL,
4646
PRIMARY KEY (session_id, pid, question),
4747
FOREIGN KEY (pid) REFERENCES Participants(pid),
4848
FOREIGN KEY (session_id) REFERENCES Sessions(session_id)
@@ -51,15 +51,15 @@ CREATE TABLE IF NOT EXISTS SessionsFeedback (
5151
CREATE TABLE IF NOT EXISTS SessionEvents (
5252
event_id SERIAL PRIMARY KEY,
5353
session_id INT,
54-
type VARCHAR(50) NOT NULL,
55-
subtype VARCHAR(50) NOT NULL,
54+
type TEXT NOT NULL,
55+
subtype TEXT NOT NULL,
5656
timestamp INT NOT NULL,
57-
actor VARCHAR(100) NOT NULL,
57+
actor TEXT NOT NULL,
5858
data TEXT,
5959
FOREIGN KEY (session_id) REFERENCES Sessions(session_id)
6060
);
6161

6262
CREATE TABLE IF NOT EXISTS Credentials (
63-
user_id VARCHAR(50) PRIMARY KEY,
64-
password VARCHAR(50) NOT NULL
63+
user_id TEXT PRIMARY KEY,
64+
password TEXT NOT NULL
6565
)

game_server/src/main/kotlin/com/imsproject/gameserver/business/auth/CredentialsController.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@ class CredentialsController(private val credentialsDAO: CredentialsDAO) {
3535
}
3636

3737
fun getAllUserIds(): List<String>{
38-
return credentials.keys().toList().filter { it != "admin" }
38+
val allCredentials = credentialsDAO.selectAll()
39+
allCredentials.forEach { credentials[it.userId] = it.password }
40+
return credentials.keys.toList().filter { it != "admin" }
3941
}
4042

4143
fun remove(userId: String) : Boolean {
4244
if(userId == "admin") {
4345
throw IllegalArgumentException("Cannot delete admin user")
4446
}
45-
return credentials.remove(userId) != null
47+
if(!contains(userId)) {
48+
return false
49+
}
50+
credentials.remove(userId)
51+
credentialsDAO.delete(CredentialsPK(userId))
52+
return true
4653
}
4754
}

game_server/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ database.port=5432
1010
database.name=ims-db
1111
database.driver-class-name=org.postgresql.Driver
1212

13-
running.local=false
13+
running.local=true

manager/src/managers/__init__.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import base64
55
from .logger import Logger
66

7-
RUNNING_LOCAL = False
7+
RUNNING_LOCAL = True
88

99
if RUNNING_LOCAL:
1010
URL = "http://localhost:8080"
@@ -28,22 +28,36 @@ def post_auth(url, json, headers=None, timeout=2.0):
2828
final_headers = auth_headers()
2929
if headers:
3030
final_headers.update(headers)
31-
return requests.post(url, json=json, headers=final_headers, timeout=timeout)
31+
post = requests.post(url, json=json, headers=final_headers, timeout=timeout)
32+
if post.status_code == 401:
33+
response = server_response(post)
34+
if not response.get_success() and response.get_message() == "Invalid Bearer token":
35+
session.pop('token')
36+
session.pop('username')
37+
38+
return post
3239

3340
def get_auth(url, headers=None, timeout=2.0):
3441
final_headers = auth_headers()
3542
if headers:
3643
final_headers.update(headers)
37-
return requests.get(url, headers=final_headers, timeout=timeout)
44+
get = requests.get(url, headers=final_headers, timeout=timeout)
45+
if get.status_code == 401:
46+
response = server_response(get)
47+
if not response.get_success() and response.get_message() == "Invalid Bearer token":
48+
session.pop('token')
49+
session.pop('username')
50+
51+
return get
3852

3953
def authenticate_basic(username: str, password: str):
4054
try:
4155
token = base64.b64encode(f"{username}:{password}".encode()).decode()
4256
headers = {"Authorization": f"Basic {token}"}
43-
Logger.log_debug(f"Sending auth request to {URL + 'auth'} with headers: {headers}")
44-
res = requests.get(URL + "login", headers=headers)
57+
# Logger.log_debug(f"Sending auth request to {URL + 'auth'} with headers: {headers}")
58+
res = requests.get(URL + "/login", headers=headers)
4559

46-
Logger.log_debug(f"Auth status: {res.status_code}, response: {res.text}")
60+
# Logger.log_debug(f"Auth status: {res.status_code}, response: {res.text}")
4761
return server_response(res)
4862
except Exception as e:
4963
Logger.log_error(f"Authentication error: {e}")

0 commit comments

Comments
 (0)