Websocket server
- Added function to create account with request "create_account" - Can create new account (but it s temporary because no db) - Doing 42 connection
This commit is contained in:
74
websocket-server/typeRequets/createAccount.py
Normal file
74
websocket-server/typeRequets/createAccount.py
Normal file
@ -0,0 +1,74 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# createAccount.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/09 08:08:00 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/09 08:52:38 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
from typeRequets.login import userList
|
||||
import random
|
||||
import re
|
||||
|
||||
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).+$'
|
||||
|
||||
# {'username': 'Kumita', 'mail': 'eddydhj@gmail.com', 'password': '3b19482535d1ab2f4e3c629c4e3e5e2d6af0a5f5280be190726a4c3be518a475'}
|
||||
|
||||
|
||||
async def createAccount(userClass, content):
|
||||
try:
|
||||
content["mail"] = content["mail"].lower()
|
||||
if (content["mail"].find('@') == -1 or content["mail"].find('.') == -1):
|
||||
await userClass.sendError("Invalid mail", 9006)
|
||||
return
|
||||
if (content["username"].find(' ') != -1):
|
||||
await userClass.sendError("Username must not contain spaces", 9007)
|
||||
return
|
||||
if (len(content["username"]) < 3):
|
||||
await userClass.sendError("Username must be at least 3 characters long", 9008)
|
||||
return
|
||||
if (len(content["username"]) > 20):
|
||||
await userClass.sendError("Username must be at most 20 characters long", 9009)
|
||||
return
|
||||
if (content["username"].find(' ') != -1):
|
||||
await userClass.sendError("Username must not contain spaces", 9011)
|
||||
return
|
||||
if (content["username"].isalnum() == False):
|
||||
await userClass.sendError("Username must contain only letters and numbers", 9012)
|
||||
return
|
||||
if (len(content["password"]) < 8):
|
||||
await userClass.sendError("Password must be at least 8 characters long", 9013)
|
||||
return
|
||||
if (bool(re.match(pattern, content["password"]))):
|
||||
await userClass.sendError("Password must contain at least one lowercase letter, one uppercase letter and one special character", 9014)
|
||||
return
|
||||
if (content["password"].find(content["username"]) != -1):
|
||||
await userClass.sendError("Password must not contain the username", 9015)
|
||||
return
|
||||
if (content["mail"] in userList):
|
||||
await userClass.sendError("Mail already used", 9016)
|
||||
return
|
||||
if (content["username"] in userList):
|
||||
await userClass.sendError("Username already used", 9017)
|
||||
return
|
||||
content["token"] = generateToken()
|
||||
while (True):
|
||||
content["id"] = random.randint(1000000, 9999999)
|
||||
if (content["id"] not in userList):
|
||||
break
|
||||
userList.append(content)
|
||||
await userClass.send({"type": "create_account", "content": "Account created"})
|
||||
except Exception as e:
|
||||
await userClass.sendError("Error create account", 9005, e)
|
||||
|
||||
def generateToken():
|
||||
list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
token = ""
|
||||
|
||||
for i in range(0, 35):
|
||||
token += list[random.randint(0, len(list) - 1)]
|
||||
return token
|
@ -3,15 +3,17 @@
|
||||
# ::: :::::::: #
|
||||
# login.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/08 22:31:18 by edbernar ### ########.fr #
|
||||
# Updated: 2024/08/09 09:41:55 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
from typeRequets.login42.login42 import main42login
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
# Les requêtes de login peuvent être de 3 types:
|
||||
# <-- {"type" : "login", "content" : {"type": "byToken", "token": "123456"}}
|
||||
@ -54,7 +56,7 @@ userList = [
|
||||
"token": "poiuygfvbdsv5c21vcxvcxhgbjqnkmds546",
|
||||
"mail": "ddddd",
|
||||
"password": "ddddd",
|
||||
"id": 2371234
|
||||
"id": 6423457
|
||||
}
|
||||
]
|
||||
|
||||
@ -84,26 +86,15 @@ async def loginByPass(userClass, content):
|
||||
return
|
||||
await userClass.send({"type": "error", "content": "Invalid username or password", "code": 9007})
|
||||
|
||||
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(userClass, content):
|
||||
# |TOM| Requete pour récuperer les informations de l'utilisateur selon l'intra de la personne
|
||||
# et créer un token si celui-ci 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 userClass.send(json.dumps(jsonVar))
|
||||
return
|
||||
jsonVar = {"type": "error", "content": "Invalid 42 token", "code": 9008}
|
||||
await userClass.send(json.dumps(jsonVar))
|
||||
try:
|
||||
main42login(content)
|
||||
except Exception as e:
|
||||
await userClass.sendError("Invalid 42 token", 9008, e)
|
||||
|
||||
async def login(userClass, content):
|
||||
# |TOM| Faire 3 types de requêtes:
|
||||
|
50
websocket-server/typeRequets/login42/login42.py
Normal file
50
websocket-server/typeRequets/login42/login42.py
Normal file
@ -0,0 +1,50 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# login42.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/09 09:32:17 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/09 10:03:54 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
UID42 = os.environ.get("uid")
|
||||
SECRET42 = os.environ.get("secret")
|
||||
TOKENURL = 'https://api.intra.42.fr/oauth/token'
|
||||
INFOURL = 'https://api.intra.42.fr/v2/me'
|
||||
|
||||
access_token = ""
|
||||
|
||||
def main42login(content):
|
||||
global access_token
|
||||
|
||||
print(UID42)
|
||||
print(SECRET42)
|
||||
data = {
|
||||
'grant_type': 'client_credentials',
|
||||
'client_id': UID42,
|
||||
'client_secret': SECRET42,
|
||||
}
|
||||
response = requests.post(TOKENURL, data=data)
|
||||
access_token = response.json()["access_token"]
|
||||
data = {
|
||||
'grant_type': 'authorization_code',
|
||||
'client_id': UID42,
|
||||
'client_secret': SECRET42,
|
||||
'code': content["token"],
|
||||
'redirect_uri': 'http://localhost:3000',
|
||||
}
|
||||
response = requests.get('https://api.intra.42.fr/v2/me', headers={'Authorization': 'Bearer ' + access_token})
|
||||
|
||||
if (response.status_code != 200):
|
||||
raise Exception("")
|
||||
|
||||
response = response.json()
|
||||
print(response)
|
||||
|
Reference in New Issue
Block a user