forked from mtusi2026/mtusi2026lab7
Перевести пароли на Argon2id и читать секреты через AppRole.
Лимит входа общий для реплик, размер тела ограничен, исходящие клиенты проверяют TLS, если задан CA. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+21
-1
@@ -10,7 +10,8 @@ from starlette.responses import JSONResponse, Response
|
||||
from app import attachments, cache, config, db, direct, images, jobs, rediscli, s3, sessions, store
|
||||
from app.cursor_codec import decode_cursor, encode_cursor
|
||||
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
|
||||
from app.passwords import dummy_hash, hash_password, verify_password
|
||||
from app.passwords import dummy_hash, hash_password, needs_rehash, verify_password
|
||||
from app.ratelimit import reserve_login
|
||||
from app.validation import (
|
||||
parse_json_body,
|
||||
require_exact_keys,
|
||||
@@ -87,6 +88,15 @@ async def current_user(request: Request) -> Response:
|
||||
return _json(user, request.state.request_id)
|
||||
|
||||
|
||||
def _client_ip(request: Request) -> str:
|
||||
real = request.headers.get("x-real-ip") or ""
|
||||
if real.strip():
|
||||
return real.split(",")[0].strip()
|
||||
if request.client is not None:
|
||||
return request.client.host
|
||||
return "unknown"
|
||||
|
||||
|
||||
async def create_session(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
header = request.headers.get("Authorization") or ""
|
||||
@@ -100,10 +110,20 @@ async def create_session(request: Request) -> Response:
|
||||
username, sep, password = decoded.partition(":")
|
||||
if not sep:
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
retry_after = await reserve_login(username, _client_ip(request))
|
||||
if retry_after is not None:
|
||||
return error_response(
|
||||
429,
|
||||
"too many login attempts",
|
||||
rid,
|
||||
{"Retry-After": str(retry_after)},
|
||||
)
|
||||
row = await store.get_user_by_username(username)
|
||||
password_hash = row["password_hash"] if row else dummy_hash()
|
||||
if row is None or not verify_password(password_hash, password):
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
if needs_rehash(password_hash):
|
||||
await store.update_password_hash(row["id"], hash_password(password))
|
||||
token, expires_at = await sessions.create(row["id"])
|
||||
return _json(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user