cursor_auth_manager.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import sqlite3
  2. import os
  3. class CursorAuthManager:
  4. """Cursor认证信息管理器"""
  5. def __init__(self):
  6. # 判断操作系统
  7. if os.name == "nt": # Windows
  8. self.db_path = os.path.join(
  9. os.getenv("APPDATA"), "Cursor", "User", "globalStorage", "state.vscdb"
  10. )
  11. else: # macOS
  12. self.db_path = os.path.expanduser(
  13. "~/Library/Application Support/Cursor/User/globalStorage/state.vscdb"
  14. )
  15. def update_auth(self, email=None, access_token=None, refresh_token=None):
  16. """
  17. 更新Cursor的认证信息
  18. :param email: 新的邮箱地址
  19. :param access_token: 新的访问令牌
  20. :param refresh_token: 新的刷新令牌
  21. :return: bool 是否成功更新
  22. """
  23. updates = []
  24. if email is not None:
  25. updates.append(("cursorAuth/cachedEmail", email))
  26. if access_token is not None:
  27. updates.append(("cursorAuth/accessToken", access_token))
  28. if refresh_token is not None:
  29. updates.append(("cursorAuth/refreshToken", refresh_token))
  30. if not updates:
  31. print("没有提供任何要更新的值")
  32. return False
  33. conn = None
  34. try:
  35. conn = sqlite3.connect(self.db_path)
  36. cursor = conn.cursor()
  37. for key, value in updates:
  38. query = "UPDATE itemTable SET value = ? WHERE key = ?"
  39. cursor.execute(query, (value, key))
  40. if cursor.rowcount > 0:
  41. print(f"成功更新 {key.split('/')[-1]}")
  42. else:
  43. print(f"未找到 {key.split('/')[-1]} 或值未变化")
  44. conn.commit()
  45. return True
  46. except sqlite3.Error as e:
  47. print("数据库错误:", str(e))
  48. return False
  49. except Exception as e:
  50. print("发生错误:", str(e))
  51. return False
  52. finally:
  53. if conn:
  54. conn.close()