dev@locazia: closer to finish autorization
This commit is contained in:
parent
0d83eaeb91
commit
a0489a7383
|
|
@ -11,13 +11,17 @@ app.register_middleware(attach_user_to_request, "request")
|
||||||
app.register_middleware(close_db_session, "response")
|
app.register_middleware(close_db_session, "response")
|
||||||
|
|
||||||
from app.api.routes._index import s_index
|
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.auth import s_api_v1_auth_twa
|
||||||
from app.api.routes.tonconnect import s_api_tonconnect_manifest
|
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.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
|
from app.api.routes.custodial import s_api_v1_custodial_upload_content
|
||||||
|
|
||||||
app.add_route(s_index, "/")
|
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_tonconnect_manifest, "/api/tonconnect-manifest.json")
|
||||||
|
|
||||||
app.add_route(s_api_v1_auth_twa, "/api/v1/auth.twa", methods=["POST", "OPTIONS"])
|
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_post, "/api/v1/storage", methods=["POST", "OPTIONS"])
|
||||||
app.add_route(s_api_v1_storage_get, "/api/v1/storage/<file_hash>", methods=["GET", "OPTIONS"])
|
app.add_route(s_api_v1_storage_get, "/api/v1/storage/<file_hash>", 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"])
|
app.add_route(s_api_v1_custodial_upload_content, "/api/v1/custodial.uploadContent", methods=["POST", "OPTIONS"])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,8 @@ async def try_authorization(request):
|
||||||
|
|
||||||
|
|
||||||
async def attach_user_to_request(request):
|
async def attach_user_to_request(request):
|
||||||
|
request.ctx.user = None
|
||||||
|
request.ctx.user_key = None
|
||||||
request.ctx.db_session = Session()
|
request.ctx.db_session = Session()
|
||||||
await try_authorization(request)
|
await try_authorization(request)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,3 @@ from sanic import response
|
||||||
async def s_index(request):
|
async def s_index(request):
|
||||||
return response.text("OK")
|
return response.text("OK")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
})
|
||||||
|
|
@ -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())
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
from sanic import response
|
from sanic import response
|
||||||
from app.core._config import TELEGRAM_API_KEY
|
from app.core._config import TELEGRAM_API_KEY
|
||||||
from app.core.models.user import User
|
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 aiogram.utils.web_app import safe_parse_webapp_init_data
|
||||||
from datetime import datetime
|
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()
|
known_user = request.ctx.db_session.query(User).filter(User.telegram_id == twa_data.user.id).first()
|
||||||
assert known_user, "User not created"
|
assert known_user, "User not created"
|
||||||
|
|
||||||
|
|
||||||
new_user_key = await known_user.create_api_token_v1(request.ctx.db_session, "USER_API_V1")
|
new_user_key = await known_user.create_api_token_v1(request.ctx.db_session, "USER_API_V1")
|
||||||
return response.json({
|
return response.json({
|
||||||
'user': known_user.json_format(),
|
'user': known_user.json_format(),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from app.core.models.keys import KnownKey
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from base58 import b58encode, b58decode
|
from base58 import b58encode, b58decode
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
from app.core.logger import make_log
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,6 +58,7 @@ class AuthenticationMixin:
|
||||||
db_session.commit()
|
db_session.commit()
|
||||||
new_key = db_session.query(KnownKey).filter(KnownKey.seed_hash == new_key.seed_hash).first()
|
new_key = db_session.query(KnownKey).filter(KnownKey.seed_hash == new_key.seed_hash).first()
|
||||||
assert new_key, "Key not created"
|
assert new_key, "Key not created"
|
||||||
|
make_log(f"[Auth] User {user_id} created new {token_type} key {new_key.id}")
|
||||||
return {
|
return {
|
||||||
"key": new_key,
|
"key": new_key,
|
||||||
"auth_v1_token": new_key.seed
|
"auth_v1_token": new_key.seed
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue