diff options
| author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-22 19:24:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-22 19:24:38 +0100 |
| commit | 02c70deecba955c1b01f661ed7a709038e90addc (patch) | |
| tree | fc6881b607120cdb97991e17142b2e3159bff759 /atuin-server/src/handlers | |
| parent | Added docker-compose.yml (#325) (diff) | |
| download | atuin-02c70deecba955c1b01f661ed7a709038e90addc.zip | |
refactor (#327)
Diffstat (limited to 'atuin-server/src/handlers')
| -rw-r--r-- | atuin-server/src/handlers/history.rs | 2 | ||||
| -rw-r--r-- | atuin-server/src/handlers/mod.rs | 35 | ||||
| -rw-r--r-- | atuin-server/src/handlers/user.rs | 17 |
3 files changed, 53 insertions, 1 deletions
diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs index b7112526..aca9ecc6 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -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, diff --git a/atuin-server/src/handlers/mod.rs b/atuin-server/src/handlers/mod.rs index 83c2d0c3..9e6659e2 100644 --- a/atuin-server/src/handlers/mod.rs +++ b/atuin-server/src/handlers/mod.rs @@ -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(), + } + } +} diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index a9a48fdc..862f228c 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -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() +} |
