feat: мульти-пользовательская авторизация через invite + email

- SQLite: таблицы users (email, password_hash, role, verified) + invites
- auth.py: register_user, verify_email, hash_password + legacy env user
- email_service.py: SMTP Timeweb (smtp.timeweb.ru:465) — код подтверждения
- API: POST /api/register, /api/verify-email, /api/admin/invite
- UI: /register?invite=TOKEN — 3-шаговая регистрация
- Admin: кнопка создать приглашение, список invites + users
- Обратная совместимость: env user (smeta) = admin

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-27 15:44:43 +10:00
co-authored by Claude Opus 4.6
parent 144a592e5e
commit 0469dd85e7
7 changed files with 523 additions and 19 deletions
+109
View File
@@ -18,6 +18,27 @@ CREATE TABLE IF NOT EXISTS messages (
timestamp REAL NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_session ON messages(username, tab_id);
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
verified INTEGER NOT NULL DEFAULT 0,
verify_code TEXT,
verify_expires REAL,
created_at REAL NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE TABLE IF NOT EXISTS invites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT UNIQUE NOT NULL,
created_by TEXT NOT NULL,
used_by TEXT,
created_at REAL NOT NULL,
expires_at REAL NOT NULL
);
"""
@@ -53,6 +74,94 @@ def get_history(username: str, tab_id: str, limit: int = 100) -> list[dict]:
return [dict(r) for r in reversed(rows)]
# === Users ===
def create_user(email: str, password_hash: str, role: str = "user") -> int:
with _conn() as conn:
cur = conn.execute(
"INSERT INTO users (email, password_hash, role, created_at) VALUES (?, ?, ?, ?)",
(email.lower().strip(), password_hash, role, time.time()),
)
return cur.lastrowid
def get_user_by_email(email: str) -> dict | None:
with _conn() as conn:
row = conn.execute(
"SELECT * FROM users WHERE email = ?", (email.lower().strip(),)
).fetchone()
return dict(row) if row else None
def set_verify_code(email: str, code: str, expires_in: int = 600):
with _conn() as conn:
conn.execute(
"UPDATE users SET verify_code = ?, verify_expires = ? WHERE email = ?",
(code, time.time() + expires_in, email.lower().strip()),
)
def verify_user(email: str, code: str) -> bool:
with _conn() as conn:
user = conn.execute(
"SELECT verify_code, verify_expires FROM users WHERE email = ?",
(email.lower().strip(),),
).fetchone()
if not user or user["verify_code"] != code:
return False
if user["verify_expires"] and time.time() > user["verify_expires"]:
return False
conn.execute(
"UPDATE users SET verified = 1, verify_code = NULL WHERE email = ?",
(email.lower().strip(),),
)
return True
def list_users() -> list[dict]:
with _conn() as conn:
rows = conn.execute(
"SELECT id, email, role, verified, created_at FROM users ORDER BY created_at DESC"
).fetchall()
return [dict(r) for r in rows]
# === Invites ===
def create_invite(token: str, created_by: str, expires_hours: int = 72) -> int:
with _conn() as conn:
cur = conn.execute(
"INSERT INTO invites (token, created_by, created_at, expires_at) VALUES (?, ?, ?, ?)",
(token, created_by, time.time(), time.time() + expires_hours * 3600),
)
return cur.lastrowid
def use_invite(token: str, used_by: str) -> bool:
with _conn() as conn:
invite = conn.execute(
"SELECT * FROM invites WHERE token = ? AND used_by IS NULL",
(token,),
).fetchone()
if not invite:
return False
if time.time() > invite["expires_at"]:
return False
conn.execute(
"UPDATE invites SET used_by = ? WHERE token = ?",
(used_by, token),
)
return True
def list_invites() -> list[dict]:
with _conn() as conn:
rows = conn.execute(
"SELECT token, created_by, used_by, created_at, expires_at FROM invites ORDER BY created_at DESC LIMIT 50"
).fetchall()
return [dict(r) for r in rows]
def list_sessions(username: str) -> list[dict]:
"""List all chat sessions for a user with last message preview."""
with _conn() as conn: