refactor (#327)
This commit is contained in:
parent
a9d1ece0cb
commit
02c70deecb
21 changed files with 72 additions and 86 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -86,7 +86,6 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"pretty_env_logger",
|
"pretty_env_logger",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tabwriter",
|
"tabwriter",
|
||||||
"termion",
|
"termion",
|
||||||
|
@ -119,7 +118,6 @@ dependencies = [
|
||||||
"rmp-serde",
|
"rmp-serde",
|
||||||
"rust-crypto",
|
"rust-crypto",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"shellexpand",
|
"shellexpand",
|
||||||
"sodiumoxide",
|
"sodiumoxide",
|
||||||
|
@ -134,14 +132,9 @@ dependencies = [
|
||||||
name = "atuin-common"
|
name = "atuin-common"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
|
||||||
"chrono",
|
"chrono",
|
||||||
"http",
|
|
||||||
"rust-crypto",
|
"rust-crypto",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
|
||||||
"serde_json",
|
|
||||||
"sodiumoxide",
|
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -162,7 +155,6 @@ dependencies = [
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"rust-crypto",
|
"rust-crypto",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sodiumoxide",
|
"sodiumoxide",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
@ -1068,9 +1060,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ipnet"
|
name = "ipnet"
|
||||||
version = "2.4.0"
|
version = "2.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "35e70ee094dc02fd9c13fdad4940090f22dbd6ac7c9e7094a46cf0232a50bc7c"
|
checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
|
|
|
@ -40,8 +40,7 @@ chrono = { version = "0.4", features = ["serde"] }
|
||||||
eyre = "0.6"
|
eyre = "0.6"
|
||||||
directories = "4"
|
directories = "4"
|
||||||
indicatif = "0.16.2"
|
indicatif = "0.16.2"
|
||||||
serde_derive = "1.0.125"
|
serde = { version = "1.0.126", features = ["derive"] }
|
||||||
serde = "1.0.126"
|
|
||||||
serde_json = "1.0.75"
|
serde_json = "1.0.75"
|
||||||
tui = "0.16"
|
tui = "0.16"
|
||||||
termion = "1.5"
|
termion = "1.5"
|
||||||
|
|
|
@ -21,8 +21,7 @@ uuid = { version = "0.8", features = ["v4"] }
|
||||||
whoami = "1.1.2"
|
whoami = "1.1.2"
|
||||||
chrono-english = "0.1.4"
|
chrono-english = "0.1.4"
|
||||||
config = "0.13"
|
config = "0.13"
|
||||||
serde_derive = "1.0.125"
|
serde = { version = "1.0.126", features = ["derive"] }
|
||||||
serde = "1.0.126"
|
|
||||||
serde_json = "1.0.75"
|
serde_json = "1.0.75"
|
||||||
rmp-serde = "1.0.0"
|
rmp-serde = "1.0.0"
|
||||||
sodiumoxide = "0.2.6"
|
sodiumoxide = "0.2.6"
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// to decrypt
|
// to decrypt
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::env;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
||||||
use atuin_common::utils::uuid_v4;
|
use atuin_common::utils::uuid_v4;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
// Any new fields MUST be Optional<>!
|
// Any new fields MUST be Optional<>!
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, sqlx::FromRow)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, sqlx::FromRow)]
|
||||||
|
|
|
@ -8,6 +8,7 @@ use atuin_common::utils::uuid_v4;
|
||||||
use chrono::{TimeZone, Utc};
|
use chrono::{TimeZone, Utc};
|
||||||
use directories::UserDirs;
|
use directories::UserDirs;
|
||||||
use eyre::{eyre, Result};
|
use eyre::{eyre, Result};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::{count_lines, Importer};
|
use super::{count_lines, Importer};
|
||||||
use crate::history::History;
|
use crate::history::History;
|
||||||
|
|
|
@ -3,9 +3,6 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
pub mod api_client;
|
pub mod api_client;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod encryption;
|
pub mod encryption;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use fs_err::{create_dir_all, File};
|
use fs_err::{create_dir_all, File};
|
||||||
|
use serde::Deserialize;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,6 @@ repository = "https://github.com/ellie/atuin"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rust-crypto = "^0.2"
|
rust-crypto = "^0.2"
|
||||||
sodiumoxide = "0.2.6"
|
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
serde_derive = "1.0.125"
|
serde = { version = "1.0.126", features = ["derive"] }
|
||||||
serde = "1.0.126"
|
|
||||||
serde_json = "1.0.75"
|
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
axum = "0.5"
|
|
||||||
http = "0.2"
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use axum::{response::IntoResponse, Json};
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct UserResponse {
|
pub struct UserResponse {
|
||||||
|
@ -56,34 +53,3 @@ pub struct SyncHistoryRequest {
|
||||||
pub struct SyncHistoryResponse {
|
pub struct SyncHistoryResponse {
|
||||||
pub history: Vec<String>,
|
pub history: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub struct ErrorResponse<'a> {
|
|
||||||
pub reason: Cow<'a, str>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IntoResponse for ErrorResponseStatus<'a> {
|
|
||||||
fn into_response(self) -> axum::response::Response {
|
|
||||||
(self.status, Json(self.error)).into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ErrorResponseStatus<'a> {
|
|
||||||
pub error: ErrorResponse<'a>,
|
|
||||||
pub status: http::StatusCode,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> ErrorResponse<'a> {
|
|
||||||
pub fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> {
|
|
||||||
ErrorResponseStatus {
|
|
||||||
error: self,
|
|
||||||
status,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reply(reason: &'a str) -> ErrorResponse {
|
|
||||||
Self {
|
|
||||||
reason: reason.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
// Calendar data
|
// Calendar data
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub enum TimePeriod {
|
pub enum TimePeriod {
|
||||||
YEAR,
|
YEAR,
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
|
@ -1,26 +1,11 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use crypto::digest::Digest;
|
|
||||||
use crypto::sha2::Sha256;
|
|
||||||
use sodiumoxide::crypto::pwhash::argon2id13;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn hash_secret(secret: &str) -> String {
|
|
||||||
sodiumoxide::init().unwrap();
|
|
||||||
let hash = argon2id13::pwhash(
|
|
||||||
secret.as_bytes(),
|
|
||||||
argon2id13::OPSLIMIT_INTERACTIVE,
|
|
||||||
argon2id13::MEMLIMIT_INTERACTIVE,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let texthash = std::str::from_utf8(&hash.0).unwrap().to_string();
|
|
||||||
|
|
||||||
// postgres hates null chars. don't do that to postgres
|
|
||||||
texthash.trim_end_matches('\u{0}').to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash_str(string: &str) -> String {
|
pub fn hash_str(string: &str) -> String {
|
||||||
|
use crypto::digest::Digest;
|
||||||
|
use crypto::sha2::Sha256;
|
||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
hasher.input_str(string);
|
hasher.input_str(string);
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,7 @@ eyre = "0.6"
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
whoami = "1.1.2"
|
whoami = "1.1.2"
|
||||||
config = "0.13"
|
config = "0.13"
|
||||||
serde_derive = "1.0.125"
|
serde = { version = "1.0.126", features = ["derive"] }
|
||||||
serde = "1.0.126"
|
|
||||||
serde_json = "1.0.75"
|
serde_json = "1.0.75"
|
||||||
sodiumoxide = "0.2.6"
|
sodiumoxide = "0.2.6"
|
||||||
base64 = "0.13.0"
|
base64 = "0.13.0"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// Calendar data
|
// Calendar data
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub enum TimePeriod {
|
pub enum TimePeriod {
|
||||||
YEAR,
|
YEAR,
|
||||||
MONTH,
|
MONTH,
|
||||||
|
|
|
@ -10,6 +10,8 @@ use atuin_common::api::*;
|
||||||
|
|
||||||
use crate::calendar::{TimePeriod, TimePeriodInfo};
|
use crate::calendar::{TimePeriod, TimePeriodInfo};
|
||||||
|
|
||||||
|
use super::{ErrorResponse, ErrorResponseStatus};
|
||||||
|
|
||||||
#[instrument(skip_all, fields(user.id = user.id))]
|
#[instrument(skip_all, fields(user.id = user.id))]
|
||||||
pub async fn count(
|
pub async fn count(
|
||||||
user: User,
|
user: User,
|
||||||
|
|
|
@ -1,6 +1,41 @@
|
||||||
|
use axum::{response::IntoResponse, Json};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub async fn index() -> &'static str {
|
pub async fn index() -> &'static str {
|
||||||
"\"Through the fathomless deeps of space swims the star turtle Great A\u{2019}Tuin, bearing on its back the four giant elephants who carry on their shoulders the mass of the Discworld.\"\n\t-- Sir Terry Pratchett"
|
"\"Through the fathomless deeps of space swims the star turtle Great A\u{2019}Tuin, bearing on its back the four giant elephants who carry on their shoulders the mass of the Discworld.\"\n\t-- Sir Terry Pratchett"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct ErrorResponse<'a> {
|
||||||
|
pub reason: Cow<'a, str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoResponse for ErrorResponseStatus<'a> {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
(self.status, Json(self.error)).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ErrorResponseStatus<'a> {
|
||||||
|
pub error: ErrorResponse<'a>,
|
||||||
|
pub status: http::StatusCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ErrorResponse<'a> {
|
||||||
|
pub fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> {
|
||||||
|
ErrorResponseStatus {
|
||||||
|
error: self,
|
||||||
|
status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reply(reason: &'a str) -> ErrorResponse {
|
||||||
|
Self {
|
||||||
|
reason: reason.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
use atuin_common::api::*;
|
use atuin_common::api::*;
|
||||||
use atuin_common::utils::hash_secret;
|
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::{Extension, Json};
|
use axum::{Extension, Json};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
@ -13,6 +12,8 @@ use crate::database::{Database, Postgres};
|
||||||
use crate::models::{NewSession, NewUser};
|
use crate::models::{NewSession, NewUser};
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
||||||
|
use super::{ErrorResponse, ErrorResponseStatus};
|
||||||
|
|
||||||
pub fn verify_str(secret: &str, verify: &str) -> bool {
|
pub fn verify_str(secret: &str, verify: &str) -> bool {
|
||||||
sodiumoxide::init().unwrap();
|
sodiumoxide::init().unwrap();
|
||||||
|
|
||||||
|
@ -139,3 +140,17 @@ pub async fn login(
|
||||||
session: session.token,
|
session: session.token,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hash_secret(secret: &str) -> String {
|
||||||
|
sodiumoxide::init().unwrap();
|
||||||
|
let hash = argon2id13::pwhash(
|
||||||
|
secret.as_bytes(),
|
||||||
|
argon2id13::OPSLIMIT_INTERACTIVE,
|
||||||
|
argon2id13::MEMLIMIT_INTERACTIVE,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let texthash = std::str::from_utf8(&hash.0).unwrap().to_string();
|
||||||
|
|
||||||
|
// postgres hates null chars. don't do that to postgres
|
||||||
|
texthash.trim_end_matches('\u{0}').to_string()
|
||||||
|
}
|
||||||
|
|
|
@ -8,9 +8,6 @@ use eyre::{Context, Result};
|
||||||
|
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod calendar;
|
pub mod calendar;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use fs_err::{create_dir_all, File};
|
use fs_err::{create_dir_all, File};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub enum Cmd {
|
||||||
pub fn print_list(h: &[History], human: bool, cmd_only: bool) {
|
pub fn print_list(h: &[History], human: bool, cmd_only: bool) {
|
||||||
let mut writer = TabWriter::new(std::io::stdout()).padding(2);
|
let mut writer = TabWriter::new(std::io::stdout()).padding(2);
|
||||||
|
|
||||||
let lines = h.iter().map(|h| {
|
let lines = h.iter().rev().map(|h| {
|
||||||
if human {
|
if human {
|
||||||
let duration = humantime::format_duration(Duration::from_nanos(std::cmp::max(
|
let duration = humantime::format_duration(Duration::from_nanos(std::cmp::max(
|
||||||
h.duration, 0,
|
h.duration, 0,
|
||||||
|
@ -82,7 +82,7 @@ pub fn print_list(h: &[History], human: bool, cmd_only: bool) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for i in lines.rev() {
|
for i in lines {
|
||||||
writer
|
writer
|
||||||
.write_all(i.as_bytes())
|
.write_all(i.as_bytes())
|
||||||
.expect("failed to write to tab writer");
|
.expect("failed to write to tab writer");
|
||||||
|
|
Loading…
Reference in a new issue