Files
cursor-byok/server/src/local_app/account.rs
T
leookun 44e2d8057a feat: initialize server structure and database schema
- Added initial server setup with Cargo.toml defining dependencies and project structure.
- Created build.rs for generating protobuf bindings and validating wire contracts.
- Established database schema with initial migration files for conversations, messages, and runs.
- Introduced tools and prompts for Cursor functionality, enhancing user interaction capabilities.
2026-08-30 01:33:39 +08:00

104 lines
3.5 KiB
Rust

//! Integrates local Cursor account state.
use std::path::{Path, PathBuf};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde_json::json;
use sqlx::{Connection, Row, SqliteConnection};
use crate::{Error, Result};
const EMAIL: &str = "cursor@ai.com";
const SIGN_UP_TYPE: &str = "Google";
const SUBJECT: &str = "cursor-local-user";
const MEMBERSHIP_TYPE: &str = "ultra";
const SUBSCRIPTION_STATUS: &str = "active";
pub async fn inject_if_missing() -> Result<()> {
inject_if_missing_at(&state_db_path()?).await
}
fn state_db_path() -> Result<PathBuf> {
let home = dirs::home_dir()
.ok_or_else(|| Error::Config("cannot resolve user home directory".into()))?;
match std::env::consts::OS {
"macos" => {
Ok(home.join("Library/Application Support/Cursor/User/globalStorage/state.vscdb"))
}
"windows" => Ok(std::env::var_os("APPDATA")
.map(PathBuf::from)
.unwrap_or_else(|| home.join("AppData/Roaming"))
.join("Cursor/User/globalStorage/state.vscdb")),
"linux" => Ok(std::env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home.join(".config"))
.join("Cursor/User/globalStorage/state.vscdb")),
platform => Err(Error::Config(format!(
"Cursor account injection is unsupported on {platform}"
))),
}
}
async fn inject_if_missing_at(path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await?;
}
let options = sqlx::sqlite::SqliteConnectOptions::new()
.filename(path)
.create_if_missing(true);
let mut connection = SqliteConnection::connect_with(&options).await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB)",
)
.execute(&mut connection)
.await?;
let account = sqlx::query("SELECT CAST(value AS TEXT) AS value FROM ItemTable WHERE key = ?")
.bind("cursorAuth/accessToken")
.fetch_optional(&mut connection)
.await?;
if account.is_some_and(|row| {
row.try_get::<String, _>("value")
.is_ok_and(|value| !value.trim().is_empty())
}) {
return Ok(());
}
let token = local_token()?;
let values = [
("cursorAuth/accessToken", token.as_str()),
("cursorAuth/refreshToken", token.as_str()),
("cursorAuth/cachedEmail", EMAIL),
("cursorAuth/cachedSignUpType", SIGN_UP_TYPE),
("cursorAuth/stripeMembershipType", MEMBERSHIP_TYPE),
("cursorAuth/stripeSubscriptionStatus", SUBSCRIPTION_STATUS),
];
let mut transaction = connection.begin().await?;
for (key, value) in values {
sqlx::query("INSERT OR REPLACE INTO ItemTable(key, value) VALUES(?, ?)")
.bind(key)
.bind(value)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
tracing::info!(
email = EMAIL,
subject = SUBJECT,
"injected local Cursor account"
);
Ok(())
}
fn local_token() -> Result<String> {
let header = URL_SAFE_NO_PAD.encode(br#"{"alg":"HS256","typ":"JWT"}"#);
let payload = URL_SAFE_NO_PAD.encode(serde_json::to_vec(&json!({
"sub": SUBJECT,
"email": EMAIL,
"type": "session",
"iss": "cursor-client",
"scope": "openid profile email",
"exp": 4070908800_u64
}))?);
Ok(format!("{header}.{payload}.{SUBJECT}"))
}