cursor_auth_manager.py 1.9 KB

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