Přidáno:

tlačítko Přihlásit přes Gitea na login stránku
zachovaný lokální login jako nouzový admin fallback
GET /portal/auth/gitea/login
GET /portal/auth/gitea/callback
OAuth state přes session a ověření přes secrets.compare_digest
výměna code za token a načtení /api/v1/user
mapování Gitea uživatele do users bez ORM
nové Gitea účty dostanou role = developer, existující role se zachová
disabled user is_active = 0 se nepřihlásí
audit eventy auth.gitea.login.started, auth.gitea.login.success, auth.gitea.login.failed
bezpečné chybové hlášky bez detailů a bez zobrazení client secretu
This commit is contained in:
JiriUhlir
2026-06-01 13:53:06 +02:00
parent 6aa5e69ee4
commit 786d055f6b
2 changed files with 216 additions and 3 deletions
+59
View File
@@ -35,6 +35,65 @@ def get_user_by_username(username: str) -> dict[str, Any] | None:
return dict(row) if row else None
def find_or_create_oauth_user(username: str, display_name: str, email: str) -> dict[str, Any]:
username = username.strip()
display_name = (display_name or username).strip() or username
email = (email or "").strip()
con = get_connection()
row = con.execute(
"""
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
FROM users
WHERE username = ?
""",
(username,),
).fetchone()
if row:
con.execute(
"""
UPDATE users
SET display_name = ?,
email = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(display_name, email, row["id"]),
)
else:
con.execute(
"""
INSERT INTO users (
username,
display_name,
email,
password_hash,
role,
is_active,
created_at,
updated_at
)
VALUES (?, ?, ?, '', 'developer', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
""",
(username, display_name, email),
)
con.commit()
user_id = row["id"] if row else con.execute("SELECT last_insert_rowid() AS id").fetchone()["id"]
user = con.execute(
"""
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
FROM users
WHERE id = ?
""",
(user_id,),
).fetchone()
con.close()
return dict(user)
def get_user_by_id(user_id: int) -> dict[str, Any] | None:
con = get_connection()