refactoring

This commit is contained in:
2025-07-17 03:45:44 +05:00
parent b79b0ae69f
commit 733977f56e
25 changed files with 300 additions and 363 deletions

0
app/api/__init__.py Normal file
View File

12
app/api/capes.py Normal file
View File

@ -0,0 +1,12 @@
from fastapi import APIRouter, UploadFile, File
from app.services.cape import CapeService
router = APIRouter(tags=["Capes"])
@router.post("/user/{username}/cape")
async def set_cape(username: str, cape_file: UploadFile = File(...)):
return await CapeService().set_cape(username, cape_file)
@router.delete("/user/{username}/cape")
async def remove_cape(username: str):
return await CapeService().remove_cape(username)

18
app/api/meta.py Normal file
View File

@ -0,0 +1,18 @@
from fastapi import APIRouter
router = APIRouter(tags=["Meta"])
@router.get("/")
def api_root():
return {
"meta": {
"serverName": "Your Auth Server",
"implementationName": "FastAPI",
"implementationVersion": "1.0.0",
"links": {
"homepage": "https://your-server.com"
},
},
"skinDomains": ["147.78.65.214"],
"capeDomains": ["147.78.65.214"]
}

12
app/api/skins.py Normal file
View File

@ -0,0 +1,12 @@
from fastapi import APIRouter, UploadFile, File, Form
from app.services.skin import SkinService
router = APIRouter(tags=["Skins"])
@router.post("/user/{username}/skin")
async def set_skin(username: str, skin_file: UploadFile = File(...), skin_model: str = Form("classic")):
return await SkinService().set_skin(username, skin_file, skin_model)
@router.delete("/user/{username}/skin")
async def remove_skin(username: str):
return await SkinService().remove_skin(username)

47
app/api/users.py Normal file
View File

@ -0,0 +1,47 @@
from fastapi import APIRouter, HTTPException, Body, Response
from app.models.user import UserCreate, UserLogin
from app.models.request import ValidateRequest
from app.services.auth import AuthService
router = APIRouter(
tags=["Users"]
)
@router.post("/auth/register")
async def register(user: UserCreate):
"""Регистрация нового пользователя"""
return await AuthService().register(user)
@router.post("/auth/authenticate")
async def authenticate(credentials: UserLogin):
"""Аутентификация пользователя"""
return await AuthService().login(credentials)
@router.post("/auth/validate")
async def validate_token(request: ValidateRequest):
is_valid = await AuthService().validate(request.accessToken, request.clientToken)
return {"valid": is_valid}
@router.post("/auth/refresh")
async def refresh_token(access_token: str, client_token: str):
result = await AuthService().refresh(access_token, client_token)
if not result:
raise HTTPException(status_code=401, detail="Invalid tokens")
return result
@router.get("/sessionserver/session/minecraft/profile/{uuid}")
async def get_minecraft_profile(uuid: str, unsigned: bool = False):
return await AuthService().get_minecraft_profile(uuid)
@router.post("/sessionserver/session/minecraft/join")
async def join_server(request_data: dict = Body(...)):
try:
await AuthService().join_server(request_data)
return Response(status_code=204)
except Exception as e:
print("Error in join_server:", str(e))
raise
@router.get("/sessionserver/session/minecraft/hasJoined")
async def has_joined(username: str, serverId: str):
return await AuthService().has_joined(username, serverId)

0
app/core/__init__.py Normal file
View File

11
app/core/config.py Normal file
View File

@ -0,0 +1,11 @@
from dotenv import load_dotenv
import os
from pathlib import Path
load_dotenv(dotenv_path=Path(__file__).parent.parent.parent / ".env")
FILES_URL = os.getenv("FILES_URL")
MONGO_URI = os.getenv("MONGO_URI")
SECRET_KEY = os.getenv("SECRET_KEY")
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 1440 # 24 часа

0
app/db/__init__.py Normal file
View File

8
app/db/database.py Normal file
View File

@ -0,0 +1,8 @@
from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import MONGO_URI
client = AsyncIOMotorClient(MONGO_URI)
db = client["minecraft_auth"]
users_collection = db["users"]
sessions_collection = db["sessions"]

0
app/models/__init__.py Normal file
View File

4
app/models/cape.py Normal file
View File

@ -0,0 +1,4 @@
from pydantic import BaseModel
class CapeUpdate(BaseModel):
cape_url: str

5
app/models/request.py Normal file
View File

@ -0,0 +1,5 @@
from pydantic import BaseModel
class ValidateRequest(BaseModel):
accessToken: str # camelCase
clientToken: str

5
app/models/skin.py Normal file
View File

@ -0,0 +1,5 @@
from pydantic import BaseModel
from typing import Optional
class SkinUpdate(BaseModel):
skin_model: Optional[str] = "classic"

29
app/models/user.py Normal file
View File

@ -0,0 +1,29 @@
from pydantic import BaseModel, EmailStr
from datetime import datetime
from typing import Optional
class UserCreate(BaseModel):
username: str
email: EmailStr
password: str
class UserLogin(BaseModel):
username: str
password: str
class UserInDB(BaseModel):
username: str
email: EmailStr
hashed_password: str
uuid: str
skin_url: Optional[str] = None
skin_model: Optional[str] = "classic"
cloak_url: Optional[str] = None
is_active: bool = True
created_at: datetime = datetime.utcnow()
class Session(BaseModel):
access_token: str
client_token: str
user_uuid: str
expires_at: datetime

0
app/services/__init__.py Normal file
View File

209
app/services/auth.py Normal file
View File

@ -0,0 +1,209 @@
import base64
import json
from fastapi import HTTPException, UploadFile
from fastapi.responses import JSONResponse
from app.models.user import UserLogin, UserInDB, UserCreate, Session
from app.utils.misc import (
verify_password,
get_password_hash,
create_access_token,
decode_token,
)
from ..db.database import users_collection, sessions_collection
import uuid
from datetime import datetime, timedelta
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
from dotenv import load_dotenv
import os
from pathlib import Path
env_path = Path(__file__).parent.parent / ".env"
load_dotenv(dotenv_path=env_path)
FILES_URL = os.getenv("FILES_URL")
class AuthService:
async def register(self, user: UserCreate):
# Проверяем, существует ли пользователь
if await users_collection.find_one({"username": user.username}):
raise HTTPException(status_code=400, detail="Username already taken")
# Хешируем пароль
hashed_password = get_password_hash(user.password)
# Создаём UUID для Minecraft
user_uuid = str(uuid.uuid4())
# Сохраняем в MongoDB
new_user = UserInDB(
username=user.username,
email=user.email,
hashed_password=hashed_password,
uuid=user_uuid,
)
await users_collection.insert_one(new_user.dict())
return {"status": "success", "uuid": user_uuid}
async def login(self, credentials: UserLogin):
# Ищем пользователя
user = await users_collection.find_one({"username": credentials.username})
if not user or not verify_password(credentials.password, user["hashed_password"]):
raise HTTPException(status_code=401, detail="Invalid credentials")
# Генерируем токены
access_token = create_access_token({"sub": user["username"], "uuid": user["uuid"]})
client_token = str(uuid.uuid4())
# Сохраняем сессию
session = Session(
access_token=access_token,
client_token=client_token,
user_uuid=user["uuid"],
expires_at=datetime.utcnow() + timedelta(minutes=1440),
)
await sessions_collection.insert_one(session.dict())
return {
"accessToken": access_token,
"clientToken": client_token,
"selectedProfile": {
"id": user["uuid"],
"name": user["username"],
},
}
async def validate(self, access_token: str, client_token: str):
print(f"Searching for access_toke and client_token: '{access_token}', '{client_token}")
session = await sessions_collection.find_one({
"access_token": access_token,
"client_token": client_token,
})
print("Session from DB:", session)
if not session or datetime.utcnow() > session["expires_at"]:
return False
return True
async def refresh(self, access_token: str, client_token: str):
if not await self.validate(access_token, client_token):
return None
# Обновляем токен
new_access_token = create_access_token({"sub": "user", "uuid": "user_uuid"})
await sessions_collection.update_one(
{"access_token": access_token},
{"$set": {"access_token": new_access_token}},
)
return {"accessToken": new_access_token, "clientToken": client_token}
async def get_minecraft_profile(self, uuid: str):
# Преобразуем UUID без дефисов в формат с дефисами (если нужно)
if '-' not in uuid:
formatted_uuid = f"{uuid[:8]}-{uuid[8:12]}-{uuid[12:16]}-{uuid[16:20]}-{uuid[20:]}"
else:
formatted_uuid = uuid
user = await users_collection.find_one({"uuid": formatted_uuid}) # Ищем по UUID с дефисами
if not user:
raise HTTPException(status_code=404, detail="User not found")
textures = {
"timestamp": int(datetime.now().timestamp() * 1000),
"profileId": user["uuid"], # UUID с дефисами
"profileName": user["username"],
"textures": {}
}
if user.get("skin_url"):
textures["textures"]["SKIN"] = {
"url": user["skin_url"],
"metadata": {"model": user.get("skin_model", "classic")}
}
if user.get("cloak_url"):
textures["textures"]["CAPE"] = {"url": user["cloak_url"]}
textures_json = json.dumps(textures).encode()
base64_textures = base64.b64encode(textures_json).decode()
# Подписываем текстуры
with open("private_key.pem", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None
)
signature = private_key.sign(
textures_json,
padding.PKCS1v15(),
hashes.SHA1()
)
return JSONResponse({
"id": user["uuid"].replace("-", ""), # Уберите дефисы
"name": user["username"],
"properties": [{
"name": "textures",
"value": base64_textures,
"signature": base64.b64encode(signature).decode()
}]
})
async def join_server(self, request_data: dict):
access_token = request_data.get("accessToken")
selected_profile = request_data.get("selectedProfile") # UUID без дефисов
server_id = request_data.get("serverId")
if not all([access_token, selected_profile, server_id]):
raise HTTPException(status_code=400, detail="Missing required parameters")
decoded_token = decode_token(access_token)
if not decoded_token:
raise HTTPException(status_code=401, detail="Invalid access token")
token_uuid = decoded_token.get("uuid", "").replace("-", "")
if token_uuid != selected_profile:
raise HTTPException(status_code=403, detail="Token doesn't match selected profile")
# Сохраняем server_id в сессию
await sessions_collection.update_one(
{"user_uuid": decoded_token["uuid"]}, # UUID с дефисами
{"$set": {"server_id": server_id}},
upsert=True
)
return True
async def has_joined(self, username: str, server_id: str):
user = await users_collection.find_one({"username": username})
if not user:
raise HTTPException(status_code=404, detail="User not found")
# Ищем сессию с этим server_id
session = await sessions_collection.find_one({
"user_uuid": user["uuid"], # UUID с дефисами
"server_id": server_id
})
if not session:
raise HTTPException(status_code=403, detail="Not joined this server")
textures = {}
if user.get("skin_url"):
textures["SKIN"] = {"url": user["skin_url"]}
if user.get("cloak_url"):
textures["CAPE"] = {"url": user["cloak_url"]}
textures_value = base64.b64encode(json.dumps({
"timestamp": int(datetime.now().timestamp()),
"profileId": user["uuid"].replace("-", ""), # UUID без дефисов
"profileName": username,
"textures": textures
}).encode()).decode()
return {
"id": user["uuid"].replace("-", ""), # UUID без дефисов
"name": username,
"properties": [{
"name": "textures",
"value": textures_value
}] if textures else []
}

70
app/services/cape.py Normal file
View File

@ -0,0 +1,70 @@
from app.db.database import users_collection
from app.core.config import FILES_URL
from fastapi import HTTPException, UploadFile
from datetime import datetime
class CapeService:
async def set_cape(self, username: str, cape_file: UploadFile):
"""Установка или замена плаща через загрузку файла (PNG или GIF)"""
if not cape_file.content_type.startswith('image/'):
raise HTTPException(status_code=400, detail="File must be an image")
# Определяем расширение
ext = None
if cape_file.content_type == "image/png":
ext = "png"
elif cape_file.content_type == "image/gif":
ext = "gif"
else:
raise HTTPException(status_code=400, detail="Only PNG and GIF capes are supported")
max_size = 2 * 1024 * 1024 # 2MB
contents = await cape_file.read()
if len(contents) > max_size:
raise HTTPException(status_code=400, detail="File too large (max 2MB)")
# Удаляем старый плащ, если есть
user = await users_collection.find_one({"username": username})
if user and user.get("cloak_url"):
from urllib.parse import urlparse
import os
old_url = user["cloak_url"]
old_filename = os.path.basename(urlparse(old_url).path)
old_path = os.path.join("app/static/capes", old_filename)
if os.path.exists(old_path):
try:
os.remove(old_path)
except Exception:
pass
# Создаем папку для плащей, если ее нет
from pathlib import Path
cape_dir = Path("app/static/capes")
cape_dir.mkdir(parents=True, exist_ok=True)
cape_filename = f"{username}_{int(datetime.now().timestamp())}.{ext}"
cape_path = cape_dir / cape_filename
with open(cape_path, "wb") as f:
f.write(contents)
result = await users_collection.update_one(
{"username": username},
{"$set": {
"cloak_url": f"{FILES_URL}/capes/{cape_filename}"
}}
)
if result.modified_count == 0:
raise HTTPException(status_code=404, detail="User not found")
return {"status": "success"}
async def remove_cape(self, username: str):
"""Удаление плаща"""
result = await users_collection.update_one(
{"username": username},
{"$unset": {"cloak_url": ""}}
)
if result.modified_count == 0:
raise HTTPException(status_code=404, detail="User not found")
return {"status": "success"}

82
app/services/skin.py Normal file
View File

@ -0,0 +1,82 @@
from fastapi import HTTPException, UploadFile
from datetime import datetime
from app.db.database import users_collection
from app.core.config import FILES_URL
class SkinService:
async def set_skin(self, username: str, skin_file: UploadFile, skin_model: str = "classic"):
"""Установка или замена скина через загрузку файла"""
# Проверяем тип файла
if not skin_file.content_type.startswith('image/'):
raise HTTPException(status_code=400, detail="File must be an image")
# Проверяем размер файла (максимум 2MB)
max_size = 2 * 1024 * 1024 # 2MB
contents = await skin_file.read()
if len(contents) > max_size:
raise HTTPException(status_code=400, detail="File too large (max 2MB)")
# Удаляем старый скин, если есть
user = await users_collection.find_one({"username": username})
if user and user.get("skin_url"):
from urllib.parse import urlparse
import os
old_url = user["skin_url"]
# Получаем имя файла из url
old_filename = os.path.basename(urlparse(old_url).path)
old_path = os.path.join("app/static/skins", old_filename)
print(f"Trying to delete old skin at: {old_path}")
if os.path.exists(old_path):
try:
os.remove(old_path)
except Exception:
pass
# Создаем папку для скинов, если ее нет
from pathlib import Path
skin_dir = Path("app/static/skins")
skin_dir.mkdir(parents=True, exist_ok=True)
# Генерируем имя файла
skin_filename = f"{username}_{int(datetime.now().timestamp())}.png"
skin_path = skin_dir / skin_filename
# Сохраняем файл
with open(skin_path, "wb") as f:
f.write(contents)
# Обновляем запись пользователя
result = await users_collection.update_one(
{"username": username},
{"$set": {
"skin_url": f"{FILES_URL}/skins/{skin_filename}",
"skin_model": skin_model
}}
)
if result.modified_count == 0:
raise HTTPException(status_code=404, detail="User not found")
return {"status": "success"}
async def remove_skin(self, username: str):
"""Удаление скина"""
user = await users_collection.find_one({"username": username})
if not user:
raise HTTPException(status_code=404, detail="User not found")
# Удаляем файл скина, если он существует
if user.get("skin_url") and user["skin_url"].startswith("/skins/"):
import os
try:
os.remove(f"skins/{user['skin_url'].split('/')[-1]}")
except:
pass
result = await users_collection.update_one(
{"username": username},
{"$unset": {
"skin_url": "",
"skin_model": ""
}}
)
return {"status": "success"}

0
app/utils/__init__.py Normal file
View File

24
app/utils/misc.py Normal file
View File

@ -0,0 +1,24 @@
from jose import jwt, JWTError
from passlib.context import CryptContext
from datetime import datetime, timedelta
from app.core.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def create_access_token(data: dict) -> str:
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
def decode_token(token: str):
try:
return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
except JWTError:
return None