数据看板
This commit is contained in:
@@ -161,6 +161,57 @@ def get_user_by_session_token(token: str | None) -> dict[str, Any] | None:
|
||||
}
|
||||
|
||||
|
||||
def create_agent_access_token(session_token: str | None) -> tuple[str, datetime] | None:
|
||||
"""为 Link AI 生成独立的短期 Bearer Token。"""
|
||||
if not session_token:
|
||||
return None
|
||||
session_hash = _sha256(session_token)
|
||||
current = now_iso()
|
||||
token = secrets.token_urlsafe(36)
|
||||
expires_at_dt = datetime.now().astimezone() + timedelta(days=Config.SESSION_EXPIRE_DAYS)
|
||||
expires_at = expires_at_dt.isoformat(timespec="seconds")
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"""SELECT user_id FROM auth_sessions
|
||||
WHERE session_hash=? AND revoked_at IS NULL AND expires_at>?""",
|
||||
(session_hash, current),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
conn.execute(
|
||||
"""INSERT INTO ai_access_tokens
|
||||
(token_hash,user_id,expires_at,created_at,last_seen_at)
|
||||
VALUES (?,?,?,?,?)""",
|
||||
(_sha256(token), row["user_id"], expires_at, current, current),
|
||||
)
|
||||
return token, expires_at_dt
|
||||
|
||||
|
||||
def get_user_by_agent_token(token: str | None) -> dict[str, Any] | None:
|
||||
if not token:
|
||||
return None
|
||||
current = now_iso()
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"""SELECT t.id AS token_id,u.* FROM ai_access_tokens t
|
||||
JOIN auth_users u ON u.id=t.user_id
|
||||
WHERE t.token_hash=? AND t.revoked_at IS NULL AND t.expires_at>?""",
|
||||
(_sha256(token), current),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
conn.execute("UPDATE ai_access_tokens SET last_seen_at=? WHERE id=?", (current, row["token_id"]))
|
||||
return {
|
||||
"id": str(row["ding_user_id"]),
|
||||
"name": row["name"],
|
||||
"account": row["mobile"] or row["email"] or row["ding_user_id"],
|
||||
"avatar": row["avatar"],
|
||||
"union_id": row["union_id"],
|
||||
"open_id": row["open_id"],
|
||||
"raw": json_loads(row["raw_json"]) if row["raw_json"] else {},
|
||||
}
|
||||
|
||||
|
||||
def revoke_session(token: str | None) -> None:
|
||||
if not token:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user