Skip to content

Commit

Permalink
feat: Frontend convenient signup
Browse files Browse the repository at this point in the history
  • Loading branch information
WizzyGeek committed Jan 12, 2024
1 parent 015556b commit 3ca52b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/pwncore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"user_already_in_team": 20,
"user_not_in_team": 21,
"insufficient_coins": 22,
"users_not_found": 23,
}


Expand Down
30 changes: 27 additions & 3 deletions src/pwncore/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import BaseModel
from tortoise.transactions import atomic

from pwncore.models import Team
from pwncore.models import Team, User
from pwncore.config import config

# Metadata at the top for instant accessibility
Expand All @@ -26,16 +26,40 @@ class AuthBody(BaseModel):
password: str


class SignupBody(BaseModel):
name: str
password: str
tags: set[str]


def normalise_tag(tag: str):
return tag.strip().casefold()


@atomic()
@router.post("/signup")
async def signup_team(team: AuthBody, response: Response):
async def signup_team(team: SignupBody, response: Response):
team.name = team.name.strip()
members = set(map(normalise_tag, team.tags))

try:
if await Team.exists(name=team.name):
response.status_code = 406
return {"msg_code": config.msg_codes["team_exists"]}

await Team.create(name=team.name, secret_hash=bcrypt.hash(team.password))
q = await User.filter(tag__in=members)
if len(q) != len(members):
response.status_code = 404
return {"msg_code": config.msg_codes["users_not_found"], "tags": list(members - set(map(lambda h: h.tag, q)))}
in_teams = list(filter(lambda h: h.team is not None, q))
if in_teams:
response.status_code = 401
return {"msg_code": config.msg_codes["user_already_in_team"], "tags": list(in_teams)}

newteam = await Team.create(name=team.name, secret_hash=bcrypt.hash(team.password))
for user in q:
user.team = newteam
if q: await User.bulk_update(q, fields=["team"])
except Exception:
response.status_code = 500
return {"msg_code": config.msg_codes["db_error"]}
Expand Down

0 comments on commit 3ca52b7

Please sign in to comment.