diff --git a/app/api/__init__.py b/app/api/__init__.py index 7a57d3a..349fbfd 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -11,13 +11,17 @@ app.register_middleware(attach_user_to_request, "request") app.register_middleware(close_db_session, "response") from app.api.routes._index import s_index +from app.api.routes._system import s_api_system_version from app.api.routes.auth import s_api_v1_auth_twa from app.api.routes.tonconnect import s_api_tonconnect_manifest from app.api.routes.node_storage import s_api_v1_storage_post, s_api_v1_storage_get +from app.api.routes.account import s_api_v1_account_get from app.api.routes.custodial import s_api_v1_custodial_upload_content app.add_route(s_index, "/") +app.add_route(s_api_system_version, "/api/system.version", methods=["GET", "OPTIONS"]) + app.add_route(s_api_tonconnect_manifest, "/api/tonconnect-manifest.json") app.add_route(s_api_v1_auth_twa, "/api/v1/auth.twa", methods=["POST", "OPTIONS"]) @@ -25,6 +29,8 @@ app.add_route(s_api_v1_auth_twa, "/api/v1/auth.twa", methods=["POST", "OPTIONS"] app.add_route(s_api_v1_storage_post, "/api/v1/storage", methods=["POST", "OPTIONS"]) app.add_route(s_api_v1_storage_get, "/api/v1/storage/", methods=["GET", "OPTIONS"]) +app.add_route(s_api_v1_account_get, "/api/v1/account", methods=["GET", "OPTIONS"]) + app.add_route(s_api_v1_custodial_upload_content, "/api/v1/custodial.uploadContent", methods=["POST", "OPTIONS"]) diff --git a/app/api/middleware.py b/app/api/middleware.py index 5046d8c..b305b73 100644 --- a/app/api/middleware.py +++ b/app/api/middleware.py @@ -55,6 +55,8 @@ async def try_authorization(request): async def attach_user_to_request(request): + request.ctx.user = None + request.ctx.user_key = None request.ctx.db_session = Session() await try_authorization(request) diff --git a/app/api/routes/_index.py b/app/api/routes/_index.py index 47aaee6..a36b6dc 100644 --- a/app/api/routes/_index.py +++ b/app/api/routes/_index.py @@ -4,4 +4,3 @@ from sanic import response async def s_index(request): return response.text("OK") - diff --git a/app/api/routes/_system.py b/app/api/routes/_system.py new file mode 100644 index 0000000..8373b67 --- /dev/null +++ b/app/api/routes/_system.py @@ -0,0 +1,16 @@ +from sanic import response +import subprocess + + +def get_git_info(): + branch_name = subprocess.check_output(["git", "branch", "--show-current"]).decode('utf-8').strip() + commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode('utf-8').strip() + return branch_name, commit_hash + + +async def s_api_system_version(request): + branch_name, commit_hash = get_git_info() + return response.json({ + "codebase_hash": commit_hash, + "codebase_branch": branch_name, + }) diff --git a/app/api/routes/account.py b/app/api/routes/account.py new file mode 100644 index 0000000..24c6124 --- /dev/null +++ b/app/api/routes/account.py @@ -0,0 +1,8 @@ +from sanic import response + + +async def s_api_v1_account_get(request): + if not request.ctx.user: + return response.json({"error": "User not found"}, status=400) + + return response.json(request.ctx.user.json_format()) diff --git a/app/api/routes/auth.py b/app/api/routes/auth.py index 5d1ef45..36a9ac0 100644 --- a/app/api/routes/auth.py +++ b/app/api/routes/auth.py @@ -1,7 +1,6 @@ from sanic import response from app.core._config import TELEGRAM_API_KEY from app.core.models.user import User -from app.core.logger import make_log from aiogram.utils.web_app import safe_parse_webapp_init_data from datetime import datetime @@ -37,7 +36,6 @@ async def s_api_v1_auth_twa(request): known_user = request.ctx.db_session.query(User).filter(User.telegram_id == twa_data.user.id).first() assert known_user, "User not created" - new_user_key = await known_user.create_api_token_v1(request.ctx.db_session, "USER_API_V1") return response.json({ 'user': known_user.json_format(), diff --git a/app/core/auth_v1.py b/app/core/auth_v1.py index f47d2d1..ed587d7 100644 --- a/app/core/auth_v1.py +++ b/app/core/auth_v1.py @@ -2,6 +2,7 @@ from app.core.models.keys import KnownKey from datetime import datetime from base58 import b58encode, b58decode from hashlib import sha256 +from app.core.logger import make_log import os @@ -57,6 +58,7 @@ class AuthenticationMixin: db_session.commit() new_key = db_session.query(KnownKey).filter(KnownKey.seed_hash == new_key.seed_hash).first() assert new_key, "Key not created" + make_log(f"[Auth] User {user_id} created new {token_type} key {new_key.id}") return { "key": new_key, "auth_v1_token": new_key.seed