-
Notifications
You must be signed in to change notification settings - Fork 157
refactor: split domains #706
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
Draft
7mochi
wants to merge
3
commits into
master
Choose a base branch
from
refactor-domains
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from fastapi import APIRouter | ||
|
|
||
| from . import users | ||
|
|
||
| bancho_router = APIRouter(tags=["Bancho"]) | ||
|
|
||
| bancho_router.include_router(users.router) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import copy | ||
| import hashlib | ||
| import random | ||
| import secrets | ||
| from collections import defaultdict | ||
| from collections.abc import Awaitable | ||
| from collections.abc import Callable | ||
| from collections.abc import Mapping | ||
| from enum import IntEnum | ||
| from enum import unique | ||
| from functools import cache | ||
| from pathlib import Path as SystemPath | ||
| from typing import Any | ||
| from typing import Literal | ||
| from urllib.parse import unquote | ||
| from urllib.parse import unquote_plus | ||
|
|
||
| import bcrypt | ||
| from fastapi import status | ||
| from fastapi.datastructures import FormData | ||
| from fastapi.datastructures import UploadFile | ||
| from fastapi.exceptions import HTTPException | ||
| from fastapi.param_functions import Depends | ||
| from fastapi.param_functions import File | ||
| from fastapi.param_functions import Form | ||
| from fastapi.param_functions import Header | ||
| from fastapi.param_functions import Path | ||
| from fastapi.param_functions import Query | ||
| from fastapi.requests import Request | ||
| from fastapi.responses import FileResponse | ||
| from fastapi.responses import ORJSONResponse | ||
| from fastapi.responses import RedirectResponse | ||
| from fastapi.responses import Response | ||
| from fastapi.routing import APIRouter | ||
| from starlette.datastructures import UploadFile as StarletteUploadFile | ||
|
|
||
| import app.packets | ||
| import app.settings | ||
| import app.state | ||
| import app.utils | ||
| from app import encryption | ||
| from app._typing import UNSET | ||
| from app.api.web.authentication import authenticate_player_session | ||
| from app.constants import regexes | ||
| from app.constants.clientflags import LastFMFlags | ||
| from app.constants.gamemodes import GameMode | ||
| from app.constants.mods import Mods | ||
| from app.constants.privileges import Privileges | ||
| from app.logging import Ansi | ||
| from app.logging import log | ||
| from app.objects import models | ||
| from app.objects.beatmap import Beatmap | ||
| from app.objects.beatmap import RankedStatus | ||
| from app.objects.beatmap import ensure_osu_file_is_available | ||
| from app.objects.player import Player | ||
| from app.objects.score import Grade | ||
| from app.objects.score import Score | ||
| from app.objects.score import SubmissionStatus | ||
| from app.repositories import clans as clans_repo | ||
| from app.repositories import comments as comments_repo | ||
| from app.repositories import favourites as favourites_repo | ||
| from app.repositories import mail as mail_repo | ||
| from app.repositories import maps as maps_repo | ||
| from app.repositories import ratings as ratings_repo | ||
| from app.repositories import scores as scores_repo | ||
| from app.repositories import stats as stats_repo | ||
| from app.repositories import users as users_repo | ||
| from app.repositories.achievements import Achievement | ||
| from app.usecases import achievements as achievements_usecases | ||
| from app.usecases import user_achievements as user_achievements_usecases | ||
| from app.utils import escape_enum | ||
| from app.utils import pymysql_encode | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| INGAME_REGISTRATION_DISALLOWED_ERROR = { | ||
| "form_error": { | ||
| "user": { | ||
| "password": [ | ||
| "In-game registration is disabled. Please register on the website.", | ||
| ], | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @router.post("/users") | ||
| async def register_account( | ||
| request: Request, | ||
| username: str = Form(..., alias="user[username]"), | ||
| email: str = Form(..., alias="user[user_email]"), | ||
| pw_plaintext: str = Form(..., alias="user[password]"), | ||
| check: int = Form(...), | ||
| # XXX: require/validate these headers; they are used later | ||
| # on in the registration process for resolving geolocation | ||
| forwarded_ip: str = Header(..., alias="X-Forwarded-For"), | ||
| real_ip: str = Header(..., alias="X-Real-IP"), | ||
| ) -> Response: | ||
| if not all((username, email, pw_plaintext)): | ||
| return Response( | ||
| content=b"Missing required params", | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| # Disable in-game registration if enabled | ||
| if app.settings.DISALLOW_INGAME_REGISTRATION: | ||
| return ORJSONResponse( | ||
| content=INGAME_REGISTRATION_DISALLOWED_ERROR, | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| # ensure all args passed | ||
| # are safe for registration. | ||
| errors: Mapping[str, list[str]] = defaultdict(list) | ||
|
|
||
| # Usernames must: | ||
| # - be within 2-15 characters in length | ||
| # - not contain both ' ' and '_', one is fine | ||
| # - not be in the config's `disallowed_names` list | ||
| # - not already be taken by another player | ||
| if not regexes.USERNAME.match(username): | ||
| errors["username"].append("Must be 2-15 characters in length.") | ||
|
|
||
| if "_" in username and " " in username: | ||
| errors["username"].append('May contain "_" and " ", but not both.') | ||
|
|
||
| if username in app.settings.DISALLOWED_NAMES: | ||
| errors["username"].append("Disallowed username; pick another.") | ||
|
|
||
| if "username" not in errors: | ||
| if await users_repo.fetch_one(name=username): | ||
| errors["username"].append("Username already taken by another player.") | ||
|
|
||
| # Emails must: | ||
| # - match the regex `^[^@\s]{1,200}@[^@\s\.]{1,30}\.[^@\.\s]{1,24}$` | ||
| # - not already be taken by another player | ||
| if not regexes.EMAIL.match(email): | ||
| errors["user_email"].append("Invalid email syntax.") | ||
| else: | ||
| if await users_repo.fetch_one(email=email): | ||
| errors["user_email"].append("Email already taken by another player.") | ||
|
|
||
| # Passwords must: | ||
| # - be within 8-32 characters in length | ||
| # - have more than 3 unique characters | ||
| # - not be in the config's `disallowed_passwords` list | ||
| if not 8 <= len(pw_plaintext) <= 32: | ||
| errors["password"].append("Must be 8-32 characters in length.") | ||
|
|
||
| if len(set(pw_plaintext)) <= 3: | ||
| errors["password"].append("Must have more than 3 unique characters.") | ||
|
|
||
| if pw_plaintext.lower() in app.settings.DISALLOWED_PASSWORDS: | ||
| errors["password"].append("That password was deemed too simple.") | ||
|
|
||
| if errors: | ||
| # we have errors to send back, send them back delimited by newlines. | ||
| errors = {k: ["\n".join(v)] for k, v in errors.items()} | ||
| errors_full = {"form_error": {"user": errors}} | ||
| return ORJSONResponse( | ||
| content=errors_full, | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| if check == 0: | ||
| # the client isn't just checking values, | ||
| # they want to register the account now. | ||
| # make the md5 & bcrypt the md5 for sql. | ||
| pw_md5 = hashlib.md5(pw_plaintext.encode()).hexdigest().encode() | ||
| pw_bcrypt = bcrypt.hashpw(pw_md5, bcrypt.gensalt()) | ||
| app.state.cache.bcrypt[pw_bcrypt] = pw_md5 # cache result for login | ||
|
|
||
| ip = app.state.services.ip_resolver.get_ip(request.headers) | ||
|
|
||
| geoloc = await app.state.services.fetch_geoloc(ip, request.headers) | ||
| country = geoloc["country"]["acronym"] if geoloc is not None else "XX" | ||
|
|
||
| async with app.state.services.database.transaction(): | ||
| # add to `users` table. | ||
| player = await users_repo.create( | ||
| name=username, | ||
| email=email, | ||
| pw_bcrypt=pw_bcrypt, | ||
| country=country, | ||
| ) | ||
|
|
||
| # add to `stats` table. | ||
| await stats_repo.create_all_modes(player_id=player["id"]) | ||
|
|
||
| if app.state.services.datadog: | ||
| app.state.services.datadog.increment("bancho.registrations") # type: ignore[no-untyped-call] | ||
|
|
||
| log(f"<{username} ({player['id']})> has registered!", Ansi.LGREEN) | ||
|
|
||
| return Response(content=b"ok") # success |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,3 @@ | |
|
|
||
| from . import cho | ||
| from . import map | ||
| from . import osu | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where were the contents of cho.py migrated to? that's the part i'd expect to have under bancho/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what I migrated was osu.py, cho.py is still pending, in bancho/users.py currently there is only the register_account method that was in osu.py, should it be in web/instead of bancho/?