fix: 扫码不再覆盖已有密码,登录按地址限速
This commit is contained in:
+18
-2
@@ -19,6 +19,7 @@ from admin.session import admin_session_active, clear_admin_session, write_admin
|
||||
from admin.settings import ensure_admin_seed, jigsaw_plan, load_admin_identity, load_config
|
||||
from euniv.db import get_db, init_db
|
||||
from euniv.models import Account
|
||||
from euniv.login_limit import allow as login_allowed
|
||||
from euniv.service import (
|
||||
fetch_exam_detail,
|
||||
fetch_home,
|
||||
@@ -32,6 +33,7 @@ from euniv.service import (
|
||||
revoke_credential,
|
||||
start_credential_refresh,
|
||||
start_qr_login,
|
||||
start_pending_cleaner,
|
||||
submit_jigsaw,
|
||||
)
|
||||
from euniv.session import clear_session, write_session
|
||||
@@ -88,6 +90,7 @@ async def lifespan(_app: FastAPI):
|
||||
|
||||
study_worker = start_autostudy()
|
||||
stop_pool = start_proxy_pool()
|
||||
start_pending_cleaner()
|
||||
yield
|
||||
if study_worker is not None:
|
||||
study_worker.stop()
|
||||
@@ -97,7 +100,14 @@ async def lifespan(_app: FastAPI):
|
||||
stop_refresh.set()
|
||||
|
||||
|
||||
app = FastAPI(title="EunivTools", lifespan=lifespan)
|
||||
_openapi = os.environ.get("EUNIV_OPENAPI") == "1"
|
||||
app = FastAPI(
|
||||
title="EunivTools",
|
||||
lifespan=lifespan,
|
||||
docs_url="/docs" if _openapi else None,
|
||||
redoc_url="/redoc" if _openapi else None,
|
||||
openapi_url="/openapi.json" if _openapi else None,
|
||||
)
|
||||
app.include_router(public_router)
|
||||
app.include_router(admin_router)
|
||||
|
||||
@@ -169,6 +179,10 @@ def _client_host(request: Request) -> str:
|
||||
@app.post("/api/login")
|
||||
def login(body: LoginBody, request: Request, response: Response, db: Session = Depends(get_db)) -> dict:
|
||||
account = body.employee_id.strip()
|
||||
if not account:
|
||||
raise HTTPException(status_code=400, detail="请填写工号")
|
||||
if not login_allowed(_client_host(request)):
|
||||
raise HTTPException(status_code=429, detail="登录过于频繁,请稍后再试")
|
||||
if identity.matches(account):
|
||||
source = _client_host(request)
|
||||
if too_many(source):
|
||||
@@ -200,7 +214,9 @@ def login_jigsaw(body: JigsawBody, response: Response, db: Session = Depends(get
|
||||
|
||||
|
||||
@app.post("/api/login/qr")
|
||||
def login_qr() -> dict:
|
||||
def login_qr(request: Request) -> dict:
|
||||
if not login_allowed(_client_host(request)):
|
||||
raise HTTPException(status_code=429, detail="登录过于频繁,请稍后再试")
|
||||
try:
|
||||
return start_qr_login()
|
||||
except RuntimeError as exc:
|
||||
|
||||
Reference in New Issue
Block a user