diff --git a/site/module_livechat/listError.txt b/site/module_livechat/listError.txt index c12a44b..4c34cb0 100644 --- a/site/module_livechat/listError.txt +++ b/site/module_livechat/listError.txt @@ -6,4 +6,6 @@ - 9002 : Invalid json - 9003 : Invalid path - 9004 : Invalid type -- 9005 : Invalid request \ No newline at end of file +- 9005 : Invalid request +- 9006 : Invalid login type +- 9007 : Invalid username or password diff --git a/site/module_livechat/server/Class/User.py b/site/module_livechat/server/Class/User.py new file mode 100644 index 0000000..1d1f304 --- /dev/null +++ b/site/module_livechat/server/Class/User.py @@ -0,0 +1,82 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# User.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: edbernar +#+ +:+ +#+ # +# By: edbernar +#+ +:+ +#+ # +# By: edbernar {"type" : "login", "content" : {"username": "". "token": "", "id": 0}} + userList = [ { "username": "user1", "token": "123456", + "mail": "aa@aa.fr", + "password": "ABC123", "id": 1 }, { "username": "user2", "token": "789123", + "mail": "bb@bb.fr", + "password": "DEF456", "id": 2 }, { "username": "user3", "token": "456789", + "mail": "cc@cc,fr", + "password": "GHI789", "id": 3 } ] +async def loginByToken(websocket, content): + # |TOM| Requete pour savoir si le token est valide + for user in userList: + if (user["token"] == content["token"]): + jsonVar = {"type": "login", "content": {"username": user["username"], "token": user["token"], "id": user["id"]}} + await websocket.send(json.dumps(jsonVar)) + return + jsonVar = {"type": "error", "content": "Invalid token", "code": 9001} + await websocket.send(json.dumps(jsonVar)) + +async def loginByPass(websocket, content): + # |TOM| Requete pour savoir si le mail et le mot de passe sont valides + # et créer un token s'il n'existe pas + for user in userList: + if (user["username"] == content["username"] and user["password"] == content["password"]): + jsonVar = {"type": "login", "content": {"username": user["username"], "token": user["token"], "id": user["id"]}} + await websocket.send(json.dumps(jsonVar)) + return + jsonVar = {"type": "error", "content": "Invalid username or password", "code": 9007} + await websocket.send(json.dumps(jsonVar)) + +async def verifyToken42(token42): + url = "https://api.intra.42.fr/v2/me" + headers = { + "Authorization": f"Bearer {token42}" + } + response = requests.get(url, headers=headers) + # |Eddy| Regarder ce que renvoie la requete quand elle est valide pour savoir qui rechercher + # dans la base de données + return (response.status_code == 200) + +async def loginBy42(websocket, content): + # |TOM| Requete pour récuperer les informations de l'utilisateur selon l'intra de la personne + # et créer un token s'il n'existe pas + for user in userList: + if (await verifyToken42(content["token42"])): + jsonVar = {"type": "login", "content": {"username": user["username"], "token": user["token"], "id": user["id"]}} + await websocket.send(json.dumps(jsonVar)) + jsonVar = {"type": "error", "content": "Invalid 42 token", "code": 9008} + await websocket.send(json.dumps(jsonVar)) + async def login(websocket, content): - print(content) \ No newline at end of file + # |TOM| Faire 3 types de requêtes: + # - byToken: Récupérer les informations de l'utilisateur en fonction de son token + # - byPass: Récupérer les informations de l'utilisateur en fonction de mail et de son mot de passe + # - by42: Récupérer les informations de l'utilisateur en fonction de son token42 (qui sera different du token) + + try: + if (content["type"] == "byToken"): + await loginByToken(websocket, content) + elif (content["type"] == "byPass"): + await loginByPass(websocket, content) + elif (content["type"] == "by42"): + await loginBy42(websocket, content) + else: + await sendError(websocket, "Invalid login type", 9006) + except: + await sendError(websocket, "Invalid request", 9005) \ No newline at end of file diff --git a/site/module_livechat/site/main.js b/site/module_livechat/site/main.js index cd706c0..c8837a0 100644 --- a/site/module_livechat/site/main.js +++ b/site/module_livechat/site/main.js @@ -3,16 +3,15 @@ /* ::: :::::::: */ /* main.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: edbernar +#+ +:+ +#+ */ +/* By: edbernar { liveChat(); @@ -51,28 +50,25 @@ async function showListUserMessage() { const divMessageListChatHome = document.getElementById("messageListChatHome"); let divUser; - socket.send(JSON.stringify({ - type: 'get_private_list_user', - token: token, - content: {} - })); - + sendRequest("get_private_list_user", {}); await waitForUserList(); - console.log(userList); divMessageListChatHome.style.height = "100%"; divMessageListChatHome.style.paddingBottom = "10px"; divMessageListChatHome.innerHTML = ''; divMessageListChatHome.scrollTop = 0; - userList.forEach(element => { - divMessageListChatHome.innerHTML += ` -
-
- + if (JSON.stringify(userList) !== "{}") + { + userList.forEach(element => { + divMessageListChatHome.innerHTML += ` +
+
+ +
+

${element.name}

-

${element.name}

-
- `; - }); + `; + }); + } divMessageListChatHome.innerHTML += "

New conversation +

"; divUser = document.getElementsByClassName("user"); for (let i = 0; i < divUser.length; i++) { diff --git a/site/module_livechat/site/typeResponse/typePrivateListUser.js b/site/module_livechat/site/typeResponse/typePrivateListUser.js index 6366c21..e8e4df1 100644 --- a/site/module_livechat/site/typeResponse/typePrivateListUser.js +++ b/site/module_livechat/site/typeResponse/typePrivateListUser.js @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* typePrivateListUser.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: edbernar +#+ +:+ +#+ */ +/* By: edbernar { - if (userListAvailable) { + if (userListAvailable) resolve(); - } else { + else userListResolve = resolve; - } }); } function typePrivateListUser(list) { userList = list; userListAvailable = true; - console.log(userListResolve) - if (userListResolve) { + if (userListResolve) + { userListResolve(); userListResolve = null; } diff --git a/site/module_livechat/site/websocket.js b/site/module_livechat/site/websocket.js index 32e9cea..5826aab 100644 --- a/site/module_livechat/site/websocket.js +++ b/site/module_livechat/site/websocket.js @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* websocket.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: edbernar +#+ +:+ +#+ */ +/* By: edbernar { console.log('Connected'); + if (token) + sendRequest("login", {"type": "byToken", "token": token}); }; socket.onmessage = (event) => { @@ -48,4 +50,18 @@ socket.onclose = () => { console.log('Disconnected'); }; -export { socket, token}; \ No newline at end of file +function sendRequest(type, content) { + let coc = null; + + if (content instanceof Object) + coc = JSON.stringify(content); + else + coc = content; + socket.send(JSON.stringify({ + type: type, + token: token, + content: content + })); +} + +export { socket, token, sendRequest }; \ No newline at end of file