75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
from fastapi import FastAPI, Depends, HTTPException, Body
|
||
from fastapi.security import OAuth2PasswordBearer
|
||
from .models import UserCreate, UserLogin, ValidateRequest
|
||
from .auth import AuthService
|
||
from .database import users_collection
|
||
import os
|
||
from typing import Union
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
app = FastAPI()
|
||
auth_service = AuthService()
|
||
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=["*"], # Разрешить все домены
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
@app.get("/")
|
||
def api_root():
|
||
return {
|
||
"meta": {
|
||
"serverName": "Your Auth Server",
|
||
"implementationName": "FastAPI",
|
||
"implementationVersion": "1.0.0",
|
||
"links": {
|
||
"homepage": "https://your-server.com"
|
||
}
|
||
}
|
||
}
|
||
|
||
# Эндпоинты Mojang-like API
|
||
@app.post("/auth/register")
|
||
async def register(user: UserCreate):
|
||
return await auth_service.register(user)
|
||
|
||
@app.post("/auth/authenticate")
|
||
async def authenticate(credentials: UserLogin):
|
||
return await auth_service.login(credentials)
|
||
|
||
@app.post("/auth/validate")
|
||
async def validate_token(request: ValidateRequest):
|
||
is_valid = await auth_service.validate(request.accessToken, request.clientToken)
|
||
return {"valid": is_valid}
|
||
|
||
@app.post("/auth/refresh")
|
||
async def refresh_token(access_token: str, client_token: str):
|
||
result = await auth_service.refresh(access_token, client_token)
|
||
if not result:
|
||
raise HTTPException(status_code=401, detail="Invalid tokens")
|
||
return result
|
||
|
||
# Эндпоинт для проверки скинов (Minecraft использует его)
|
||
@app.get("/session/hasJoined")
|
||
async def has_joined(username: str, serverId: str):
|
||
user = await users_collection.find_one({"username": username})
|
||
if not user:
|
||
raise HTTPException(status_code=404, detail="User not found")
|
||
|
||
return {
|
||
"id": user["uuid"],
|
||
"name": username,
|
||
"properties": [
|
||
{
|
||
"name": "textures",
|
||
"value": "base64_encoded_skin_data", # Здесь можно добавить скины
|
||
}
|
||
],
|
||
}
|
||
|
||
if __name__ == "__main__":
|
||
import uvicorn
|
||
uvicorn.run(app, host="0.0.0.0", port=8000)
|