use anyhow::anyhow;
use chrono::{DateTime, Utc};
use ecdsa::signature::Signer;
use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
use serde::{Deserialize, Serialize};
use super::{
account_data::AccountDataForServer, account_token::AccountToken, machine_id::MachineUid,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IgnoreSession {
pub account_data: AccountDataForServer,
pub time_stamp: DateTime<Utc>,
pub signature: Signature,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogoutAllRequest {
pub account_token: AccountToken,
pub ignore_session: Option<IgnoreSession>,
}
pub fn logout_all(
account_token_hex: String,
hw_id: MachineUid,
key: &SigningKey,
pub_key: VerifyingKey,
) -> anyhow::Result<LogoutAllRequest> {
let account_token = hex::decode(account_token_hex)?;
let time_stamp = chrono::Utc::now();
let time_str = time_stamp.to_string();
let signature = key.sign(time_str.as_bytes());
Ok(LogoutAllRequest {
account_token: account_token
.try_into()
.map_err(|_| anyhow!("Invalid account token."))?,
ignore_session: Some(IgnoreSession {
account_data: AccountDataForServer {
public_key: pub_key,
hw_id,
},
signature,
time_stamp,
}),
})
}