Add: multi token management
This commit is contained in:
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use thiserror::Error;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -106,13 +107,41 @@ impl Default for SpriteConfig {
|
||||
pub struct ApiConfig {
|
||||
pub port: u16,
|
||||
pub auth_token: String,
|
||||
pub auth_tokens: Vec<ApiTokenEntry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(default)]
|
||||
pub struct ApiTokenEntry {
|
||||
pub id: String,
|
||||
pub label: String,
|
||||
pub token: String,
|
||||
pub created_at_ms: u64,
|
||||
}
|
||||
|
||||
impl Default for ApiTokenEntry {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
label: "default".to_string(),
|
||||
token: Uuid::new_v4().to_string(),
|
||||
created_at_ms: now_unix_ms(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ApiConfig {
|
||||
fn default() -> Self {
|
||||
let token = Uuid::new_v4().to_string();
|
||||
Self {
|
||||
port: 32_145,
|
||||
auth_token: Uuid::new_v4().to_string(),
|
||||
auth_token: token.clone(),
|
||||
auth_tokens: vec![ApiTokenEntry {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
label: "default".to_string(),
|
||||
token,
|
||||
created_at_ms: now_unix_ms(),
|
||||
}],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,6 +235,13 @@ pub fn save(path: &Path, config: &AppConfig) -> Result<(), ConfigError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn now_unix_ms() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|v| v.as_millis() as u64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{load_or_create_at, save, AppConfig};
|
||||
|
||||
Reference in New Issue
Block a user