This commit is contained in:
Conrad Ludgate
2022-04-22 19:24:38 +01:00
committed by GitHub
parent a9d1ece0cb
commit 02c70deecb
21 changed files with 72 additions and 86 deletions

View File

@@ -17,8 +17,7 @@ eyre = "0.6"
uuid = { version = "0.8", features = ["v4"] }
whoami = "1.1.2"
config = "0.13"
serde_derive = "1.0.125"
serde = "1.0.126"
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.75"
sodiumoxide = "0.2.6"
base64 = "0.13.0"

View File

@@ -1,5 +1,7 @@
// Calendar data
use serde::{Deserialize, Serialize};
pub enum TimePeriod {
YEAR,
MONTH,

View File

@@ -10,6 +10,8 @@ use atuin_common::api::*;
use crate::calendar::{TimePeriod, TimePeriodInfo};
use super::{ErrorResponse, ErrorResponseStatus};
#[instrument(skip_all, fields(user.id = user.id))]
pub async fn count(
user: User,

View File

@@ -1,6 +1,41 @@
use axum::{response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
pub mod history;
pub mod user;
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"
}
#[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(),
}
}
}

View File

@@ -1,7 +1,6 @@
use std::borrow::Borrow;
use atuin_common::api::*;
use atuin_common::utils::hash_secret;
use axum::extract::Path;
use axum::{Extension, Json};
use http::StatusCode;
@@ -13,6 +12,8 @@ use crate::database::{Database, Postgres};
use crate::models::{NewSession, NewUser};
use crate::settings::Settings;
use super::{ErrorResponse, ErrorResponseStatus};
pub fn verify_str(secret: &str, verify: &str) -> bool {
sodiumoxide::init().unwrap();
@@ -139,3 +140,17 @@ pub async fn login(
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()
}

View File

@@ -8,9 +8,6 @@ use eyre::{Context, Result};
use crate::settings::Settings;
#[macro_use]
extern crate serde_derive;
pub mod auth;
pub mod calendar;
pub mod database;

View File

@@ -1,4 +1,5 @@
use fs_err::{create_dir_all, File};
use serde::{Deserialize, Serialize};
use std::io::prelude::*;
use std::path::PathBuf;