Add: multi token management

This commit is contained in:
DaZuo0122
2026-02-15 12:20:00 +08:00
parent f20ed1fd9d
commit 832fbda04d
8 changed files with 713 additions and 16 deletions

View File

@@ -90,6 +90,14 @@ struct UiSpritePackOption {
pack_id_or_path: String,
}
#[derive(Debug, Clone, serde::Serialize)]
struct UiApiToken {
id: String,
label: String,
token: String,
created_at_ms: u64,
}
#[derive(Debug, Error)]
enum AppError {
#[error("{0}")]
@@ -243,6 +251,60 @@ fn list_sprite_packs(state: tauri::State<'_, AppState>) -> Result<Vec<UiSpritePa
Ok(packs)
}
#[tauri::command]
fn list_api_tokens(state: tauri::State<'_, AppState>) -> Result<Vec<UiApiToken>, String> {
let entries = state
.runtime_core
.list_api_tokens()
.map_err(|err| err.to_string())?;
Ok(entries.into_iter().map(to_ui_api_token).collect())
}
#[tauri::command]
fn create_api_token(
state: tauri::State<'_, AppState>,
label: Option<String>,
) -> Result<UiApiToken, String> {
let entry = state
.runtime_core
.create_api_token(label)
.map_err(|err| err.to_string())?;
Ok(to_ui_api_token(entry))
}
#[tauri::command]
fn rename_api_token(
state: tauri::State<'_, AppState>,
id: String,
label: String,
) -> Result<Vec<UiApiToken>, String> {
state
.runtime_core
.rename_api_token(&id, &label)
.map_err(|err| err.to_string())?;
let entries = state
.runtime_core
.list_api_tokens()
.map_err(|err| err.to_string())?;
Ok(entries.into_iter().map(to_ui_api_token).collect())
}
#[tauri::command]
fn revoke_api_token(
state: tauri::State<'_, AppState>,
id: String,
) -> Result<Vec<UiApiToken>, String> {
state
.runtime_core
.revoke_api_token(&id)
.map_err(|err| err.to_string())?;
let entries = state
.runtime_core
.list_api_tokens()
.map_err(|err| err.to_string())?;
Ok(entries.into_iter().map(to_ui_api_token).collect())
}
#[tauri::command]
fn set_sprite_pack(
app_handle: tauri::AppHandle,
@@ -382,6 +444,10 @@ fn main() -> Result<(), AppError> {
tauri_animation_slowdown_factor,
set_tauri_animation_slowdown_factor,
list_sprite_packs,
list_api_tokens,
create_api_token,
rename_api_token,
revoke_api_token,
set_sprite_pack,
set_scale,
set_visibility,
@@ -502,6 +568,15 @@ fn to_ui_snapshot(snapshot: &FrontendStateSnapshot) -> UiSnapshot {
}
}
fn to_ui_api_token(entry: sprimo_config::ApiTokenEntry) -> UiApiToken {
UiApiToken {
id: entry.id,
label: entry.label,
token: entry.token,
created_at_ms: entry.created_at_ms,
}
}
fn sprite_pack_root(runtime_core: &RuntimeCore) -> Result<std::path::PathBuf, String> {
let sprite_packs_dir = runtime_core
.config()