Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Frontend convenient signup #17

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 38 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,51 @@ 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:
# Mypy kinda not working
user.team = newteam # type: ignore[assignment]
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