Add admin endpoints and finalize push_update.py
This commit is contained in:
parent
92c1750e1b
commit
cd35f9e02d
35
api/main.py
35
api/main.py
|
|
@ -22,6 +22,7 @@ DB_USER = "verimund_user"
|
||||||
DB_PASS = os.getenv("DB_PASS", "Shlevison2k17")
|
DB_PASS = os.getenv("DB_PASS", "Shlevison2k17")
|
||||||
JWT_SECRET = os.getenv("JWT_SECRET", "")
|
JWT_SECRET = os.getenv("JWT_SECRET", "")
|
||||||
HMAC_SECRET = os.getenv("HMAC_SECRET", "")
|
HMAC_SECRET = os.getenv("HMAC_SECRET", "")
|
||||||
|
ADMIN_KEY = os.getenv("ADMIN_KEY", "")
|
||||||
JWT_ALGORITHM = "HS256"
|
JWT_ALGORITHM = "HS256"
|
||||||
JWT_EXPIRE_HOURS = 24
|
JWT_EXPIRE_HOURS = 24
|
||||||
|
|
||||||
|
|
@ -54,6 +55,11 @@ def verify_jwt(authorization: Optional[str] = Header(None)):
|
||||||
raise HTTPException(status_code=401, detail="Invalid token")
|
raise HTTPException(status_code=401, detail="Invalid token")
|
||||||
|
|
||||||
|
|
||||||
|
def verify_admin(authorization: Optional[str] = Header(None)):
|
||||||
|
if not authorization or authorization != f"Bearer {ADMIN_KEY}":
|
||||||
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
|
|
||||||
class AuthRequest(BaseModel):
|
class AuthRequest(BaseModel):
|
||||||
license_key: str
|
license_key: str
|
||||||
hwid: str
|
hwid: str
|
||||||
|
|
@ -61,6 +67,12 @@ class AuthRequest(BaseModel):
|
||||||
signature: str
|
signature: str
|
||||||
|
|
||||||
|
|
||||||
|
class RegisterVersionRequest(BaseModel):
|
||||||
|
version: str
|
||||||
|
channel: str
|
||||||
|
release_notes: str = ""
|
||||||
|
|
||||||
|
|
||||||
@app.post("/auth")
|
@app.post("/auth")
|
||||||
def authenticate(req: AuthRequest, db=Depends(get_db)):
|
def authenticate(req: AuthRequest, db=Depends(get_db)):
|
||||||
try:
|
try:
|
||||||
|
|
@ -127,18 +139,37 @@ def get_sector_stats(payload=Depends(verify_jwt), db=Depends(get_db)):
|
||||||
def check_update(payload=Depends(verify_jwt), db=Depends(get_db)):
|
def check_update(payload=Depends(verify_jwt), db=Depends(get_db)):
|
||||||
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
channel = payload.get("channel", "stable")
|
channel = payload.get("channel", "stable")
|
||||||
cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY created_at DESC LIMIT 1", (channel,))
|
cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY released_at DESC LIMIT 1", (channel,))
|
||||||
version = cur.fetchone()
|
version = cur.fetchone()
|
||||||
if not version:
|
if not version:
|
||||||
raise HTTPException(status_code=404, detail="No version found")
|
raise HTTPException(status_code=404, detail="No version found")
|
||||||
return {"version": version["version"], "channel": channel, "release_notes": version.get("release_notes", "")}
|
return {"version": version["version"], "channel": channel, "release_notes": version.get("release_notes", "")}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/admin/versions")
|
||||||
|
def get_versions(_=Depends(verify_admin), db=Depends(get_db)):
|
||||||
|
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
|
cur.execute("SELECT * FROM app_versions ORDER BY released_at DESC LIMIT 1")
|
||||||
|
version = cur.fetchone()
|
||||||
|
return {"version": version["version"] if version else "1.0.0"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/admin/register-version")
|
||||||
|
def register_version(req: RegisterVersionRequest, _=Depends(verify_admin), db=Depends(get_db)):
|
||||||
|
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO app_versions (version, channel, release_notes, released_at) VALUES (%s, %s, %s, %s)",
|
||||||
|
(req.version, req.channel, req.release_notes, datetime.now(timezone.utc))
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
return {"status": "ok", "version": req.version, "channel": req.channel}
|
||||||
|
|
||||||
|
|
||||||
@app.post("/download")
|
@app.post("/download")
|
||||||
def download_exe(payload=Depends(verify_jwt), db=Depends(get_db)):
|
def download_exe(payload=Depends(verify_jwt), db=Depends(get_db)):
|
||||||
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
channel = payload.get("channel", "stable")
|
channel = payload.get("channel", "stable")
|
||||||
cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY created_at DESC LIMIT 1", (channel,))
|
cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY released_at DESC LIMIT 1", (channel,))
|
||||||
version = cur.fetchone()
|
version = cur.fetchone()
|
||||||
if not version:
|
if not version:
|
||||||
raise HTTPException(status_code=404, detail="No version found")
|
raise HTTPException(status_code=404, detail="No version found")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue