2021-04-21 11:13:51 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
use chrono::Utc;
|
2022-04-21 01:05:57 -06:00
|
|
|
use eyre::{bail, Result};
|
2021-04-21 11:13:51 -06:00
|
|
|
use reqwest::header::{HeaderMap, AUTHORIZATION, USER_AGENT};
|
|
|
|
use reqwest::{StatusCode, Url};
|
2021-04-20 10:07:11 -06:00
|
|
|
use sodiumoxide::crypto::secretbox;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
use atuin_common::api::{
|
2021-05-09 14:17:24 -06:00
|
|
|
AddHistoryRequest, CountResponse, LoginRequest, LoginResponse, RegisterResponse,
|
|
|
|
SyncHistoryResponse,
|
2021-04-21 11:13:51 -06:00
|
|
|
};
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2021-05-09 15:31:11 -06:00
|
|
|
use crate::encryption::{decode_key, decrypt};
|
2021-04-20 14:53:07 -06:00
|
|
|
use crate::history::History;
|
2022-04-25 00:13:30 -06:00
|
|
|
use crate::sync::hash_str;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
static APP_USER_AGENT: &str = concat!("atuin/", env!("CARGO_PKG_VERSION"),);
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
// TODO: remove all references to the encryption key from this
|
|
|
|
// It should be handled *elsewhere*
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
pub struct Client<'a> {
|
2021-04-20 10:07:11 -06:00
|
|
|
sync_addr: &'a str,
|
|
|
|
key: secretbox::Key,
|
|
|
|
client: reqwest::Client,
|
2021-04-13 12:14:07 -06:00
|
|
|
}
|
|
|
|
|
2021-12-08 06:37:49 -07:00
|
|
|
pub async fn register(
|
2021-04-21 11:13:51 -06:00
|
|
|
address: &str,
|
|
|
|
username: &str,
|
|
|
|
email: &str,
|
|
|
|
password: &str,
|
2022-04-12 16:06:19 -06:00
|
|
|
) -> Result<RegisterResponse> {
|
2021-04-21 11:13:51 -06:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert("username", username);
|
|
|
|
map.insert("email", email);
|
|
|
|
map.insert("password", password);
|
|
|
|
|
|
|
|
let url = format!("{}/user/{}", address, username);
|
2022-04-22 14:14:23 -06:00
|
|
|
let resp = reqwest::get(url).await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
if resp.status().is_success() {
|
2022-04-21 01:05:57 -06:00
|
|
|
bail!("username already in use");
|
2021-04-21 11:13:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let url = format!("{}/register", address);
|
2021-12-08 06:37:49 -07:00
|
|
|
let client = reqwest::Client::new();
|
2021-04-21 11:13:51 -06:00
|
|
|
let resp = client
|
|
|
|
.post(url)
|
2021-05-09 14:17:24 -06:00
|
|
|
.header(USER_AGENT, APP_USER_AGENT)
|
2021-04-21 11:13:51 -06:00
|
|
|
.json(&map)
|
2021-12-08 06:37:49 -07:00
|
|
|
.send()
|
|
|
|
.await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
if !resp.status().is_success() {
|
2022-04-21 01:05:57 -06:00
|
|
|
bail!("failed to register user");
|
2021-04-21 11:13:51 -06:00
|
|
|
}
|
|
|
|
|
2021-12-08 06:37:49 -07:00
|
|
|
let session = resp.json::<RegisterResponse>().await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
Ok(session)
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> {
|
2021-04-21 11:13:51 -06:00
|
|
|
let url = format!("{}/login", address);
|
2021-12-08 06:37:49 -07:00
|
|
|
let client = reqwest::Client::new();
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
let resp = client
|
|
|
|
.post(url)
|
2021-05-09 14:17:24 -06:00
|
|
|
.header(USER_AGENT, APP_USER_AGENT)
|
|
|
|
.json(&req)
|
2021-12-08 06:37:49 -07:00
|
|
|
.send()
|
|
|
|
.await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
if resp.status() != reqwest::StatusCode::OK {
|
2022-04-21 01:05:57 -06:00
|
|
|
bail!("invalid login details");
|
2021-04-21 11:13:51 -06:00
|
|
|
}
|
|
|
|
|
2021-12-08 06:37:49 -07:00
|
|
|
let session = resp.json::<LoginResponse>().await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
Ok(session)
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
impl<'a> Client<'a> {
|
2021-05-09 14:17:24 -06:00
|
|
|
pub fn new(sync_addr: &'a str, session_token: &'a str, key: String) -> Result<Self> {
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(AUTHORIZATION, format!("Token {}", session_token).parse()?);
|
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
Ok(Client {
|
2021-04-20 10:07:11 -06:00
|
|
|
sync_addr,
|
2021-04-21 11:13:51 -06:00
|
|
|
key: decode_key(key)?,
|
2021-05-09 14:17:24 -06:00
|
|
|
client: reqwest::Client::builder()
|
|
|
|
.user_agent(APP_USER_AGENT)
|
|
|
|
.default_headers(headers)
|
|
|
|
.build()?,
|
2021-04-21 11:13:51 -06:00
|
|
|
})
|
2021-04-13 12:14:07 -06:00
|
|
|
}
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
pub async fn count(&self) -> Result<i64> {
|
|
|
|
let url = format!("{}/sync/count", self.sync_addr);
|
|
|
|
let url = Url::parse(url.as_str())?;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
let resp = self.client.get(url).send().await?;
|
2021-04-21 11:13:51 -06:00
|
|
|
|
|
|
|
if resp.status() != StatusCode::OK {
|
2022-04-21 01:05:57 -06:00
|
|
|
bail!("failed to get count (are you logged in?)");
|
2021-04-21 11:13:51 -06:00
|
|
|
}
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
let count = resp.json::<CountResponse>().await?;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
|
|
|
Ok(count.count)
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
pub async fn get_history(
|
2021-04-13 12:14:07 -06:00
|
|
|
&self,
|
|
|
|
sync_ts: chrono::DateTime<Utc>,
|
|
|
|
history_ts: chrono::DateTime<Utc>,
|
|
|
|
host: Option<String>,
|
|
|
|
) -> Result<Vec<History>> {
|
|
|
|
let host = match host {
|
|
|
|
None => hash_str(&format!("{}:{}", whoami::hostname(), whoami::username())),
|
|
|
|
Some(h) => h,
|
|
|
|
};
|
|
|
|
|
|
|
|
let url = format!(
|
|
|
|
"{}/sync/history?sync_ts={}&history_ts={}&host={}",
|
2021-04-20 10:07:11 -06:00
|
|
|
self.sync_addr,
|
|
|
|
urlencoding::encode(sync_ts.to_rfc3339().as_str()),
|
|
|
|
urlencoding::encode(history_ts.to_rfc3339().as_str()),
|
2021-04-13 12:14:07 -06:00
|
|
|
host,
|
|
|
|
);
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
let resp = self.client.get(url).send().await?;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
let history = resp.json::<SyncHistoryResponse>().await?;
|
2021-04-13 12:14:07 -06:00
|
|
|
let history = history
|
|
|
|
.history
|
|
|
|
.iter()
|
|
|
|
.map(|h| serde_json::from_str(h).expect("invalid base64"))
|
2021-04-20 10:07:11 -06:00
|
|
|
.map(|h| decrypt(&h, &self.key).expect("failed to decrypt history! check your key"))
|
2021-04-13 12:14:07 -06:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(history)
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
pub async fn post_history(&self, history: &[AddHistoryRequest]) -> Result<()> {
|
2021-04-20 10:07:11 -06:00
|
|
|
let url = format!("{}/history", self.sync_addr);
|
|
|
|
let url = Url::parse(url.as_str())?;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
self.client.post(url).json(history).send().await?;
|
2021-04-13 12:14:07 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|