summaryrefslogtreecommitdiffstats
path: root/atuin-server
diff options
context:
space:
mode:
authorConrad Ludgate <conradludgate@gmail.com>2021-05-09 21:17:24 +0100
committerGitHub <noreply@github.com>2021-05-09 21:17:24 +0100
commitde2e34ac500c17e80fe4dcf2ed1c08add8998fa3 (patch)
treeaca08817ace6e0bd6a7dfc21615b2ed8b62f3a05 /atuin-server
parente43e5ce74a85d87a625295b9b089a1b5b8e26fab (diff)
some changes :shrug: (#83)
* make everything a cow * fmt + clippy
Diffstat (limited to 'atuin-server')
-rw-r--r--atuin-server/src/database.rs36
-rw-r--r--atuin-server/src/handlers/history.rs20
-rw-r--r--atuin-server/src/handlers/user.rs36
-rw-r--r--atuin-server/src/lib.rs2
-rw-r--r--atuin-server/src/models.rs18
5 files changed, 65 insertions, 47 deletions
diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs
index 4a3828d0..7de2a6f2 100644
--- a/atuin-server/src/database.rs
+++ b/atuin-server/src/database.rs
@@ -13,9 +13,9 @@ pub trait Database {
async fn get_session_user(&self, token: &str) -> Result<User>;
async fn add_session(&self, session: &NewSession) -> Result<()>;
- async fn get_user(&self, username: String) -> Result<User>;
+ async fn get_user(&self, username: &str) -> Result<User>;
async fn get_user_session(&self, u: &User) -> Result<Session>;
- async fn add_user(&self, user: NewUser) -> Result<i64>;
+ async fn add_user(&self, user: &NewUser) -> Result<i64>;
async fn count_history(&self, user: &User) -> Result<i64>;
async fn list_history(
@@ -23,7 +23,7 @@ pub trait Database {
user: &User,
created_since: chrono::NaiveDateTime,
since: chrono::NaiveDateTime,
- host: String,
+ host: &str,
) -> Result<Vec<History>>;
async fn add_history(&self, history: &[NewHistory]) -> Result<()>;
}
@@ -62,7 +62,7 @@ impl Database for Postgres {
}
}
- async fn get_user(&self, username: String) -> Result<User> {
+ async fn get_user(&self, username: &str) -> Result<User> {
let res: Option<User> =
sqlx::query_as::<_, User>("select * from users where username = $1")
.bind(username)
@@ -111,7 +111,7 @@ impl Database for Postgres {
user: &User,
created_since: chrono::NaiveDateTime,
since: chrono::NaiveDateTime,
- host: String,
+ host: &str,
) -> Result<Vec<History>> {
let res = sqlx::query_as::<_, History>(
"select * from history
@@ -137,6 +137,10 @@ impl Database for Postgres {
let mut tx = self.pool.begin().await?;
for i in history {
+ let client_id: &str = &i.client_id;
+ let hostname: &str = &i.hostname;
+ let data: &str = &i.data;
+
sqlx::query(
"insert into history
(client_id, user_id, hostname, timestamp, data)
@@ -144,11 +148,11 @@ impl Database for Postgres {
on conflict do nothing
",
)
- .bind(i.client_id)
+ .bind(client_id)
.bind(i.user_id)
- .bind(i.hostname)
+ .bind(hostname)
.bind(i.timestamp)
- .bind(i.data)
+ .bind(data)
.execute(&mut tx)
.await?;
}
@@ -158,16 +162,20 @@ impl Database for Postgres {
Ok(())
}
- async fn add_user(&self, user: NewUser) -> Result<i64> {
+ async fn add_user(&self, user: &NewUser) -> Result<i64> {
+ let email: &str = &user.email;
+ let username: &str = &user.username;
+ let password: &str = &user.password;
+
let res: (i64,) = sqlx::query_as(
"insert into users
(username, email, password)
values($1, $2, $3)
returning id",
)
- .bind(user.username.as_str())
- .bind(user.email.as_str())
- .bind(user.password)
+ .bind(username)
+ .bind(email)
+ .bind(password)
.fetch_one(&self.pool)
.await?;
@@ -175,13 +183,15 @@ impl Database for Postgres {
}
async fn add_session(&self, session: &NewSession) -> Result<()> {
+ let token: &str = &session.token;
+
sqlx::query(
"insert into sessions
(user_id, token)
values($1, $2)",
)
.bind(session.user_id)
- .bind(session.token)
+ .bind(token)
.execute(&self.pool)
.await?;
diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs
index 18852b58..06715381 100644
--- a/atuin-server/src/handlers/history.rs
+++ b/atuin-server/src/handlers/history.rs
@@ -6,7 +6,7 @@ use atuin_common::api::*;
pub async fn count(
user: User,
db: impl Database + Clone + Send + Sync,
-) -> JSONResult<ErrorResponseStatus> {
+) -> JSONResult<ErrorResponseStatus<'static>> {
db.count_history(&user).await.map_or(
reply_error(
ErrorResponse::reply("failed to query history count")
@@ -17,16 +17,16 @@ pub async fn count(
}
pub async fn list(
- req: SyncHistoryRequest,
+ req: SyncHistoryRequest<'_>,
user: User,
db: impl Database + Clone + Send + Sync,
-) -> JSONResult<ErrorResponseStatus> {
+) -> JSONResult<ErrorResponseStatus<'static>> {
let history = db
.list_history(
&user,
req.sync_ts.naive_utc(),
req.history_ts.naive_utc(),
- req.host,
+ &req.host,
)
.await;
@@ -54,20 +54,20 @@ pub async fn list(
}
pub async fn add(
- req: Vec<AddHistoryRequest>,
+ req: Vec<AddHistoryRequest<'_, String>>,
user: User,
db: impl Database + Clone + Send + Sync,
-) -> ReplyResult<impl Reply, ErrorResponseStatus> {
+) -> ReplyResult<impl Reply, ErrorResponseStatus<'_>> {
debug!("request to add {} history items", req.len());
let history: Vec<NewHistory> = req
- .iter()
+ .into_iter()
.map(|h| NewHistory {
- client_id: h.id.as_str(),
+ client_id: h.id,
user_id: user.id,
- hostname: h.hostname.as_str(),
+ hostname: h.hostname,
timestamp: h.timestamp.naive_utc(),
- data: h.data.as_str(),
+ data: h.data.into(),
})
.collect();
diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs
index ed779168..8144adab 100644
--- a/atuin-server/src/handlers/user.rs
+++ b/atuin-server/src/handlers/user.rs
@@ -1,3 +1,5 @@
+use std::borrow::Borrow;
+
use atuin_common::api::*;
use atuin_common::utils::hash_secret;
use sodiumoxide::crypto::pwhash::argon2id13;
@@ -23,10 +25,10 @@ pub fn verify_str(secret: &str, verify: &str) -> bool {
}
pub async fn get(
- username: String,
+ username: impl AsRef<str>,
db: impl Database + Clone + Send + Sync,
-) -> JSONResult<ErrorResponseStatus> {
- let user = match db.get_user(username).await {
+) -> JSONResult<ErrorResponseStatus<'static>> {
+ let user = match db.get_user(username.as_ref()).await {
Ok(user) => user,
Err(e) => {
debug!("user not found: {}", e);
@@ -37,15 +39,15 @@ pub async fn get(
};
reply_json(UserResponse {
- username: user.username,
+ username: user.username.into(),
})
}
pub async fn register(
- register: RegisterRequest,
+ register: RegisterRequest<'_>,
settings: Settings,
db: impl Database + Clone + Send + Sync,
-) -> JSONResult<ErrorResponseStatus> {
+) -> JSONResult<ErrorResponseStatus<'static>> {
if !settings.open_registration {
return reply_error(
ErrorResponse::reply("this server is not open for registrations")
@@ -53,15 +55,15 @@ pub async fn register(
);
}
- let hashed = hash_secret(register.password.as_str());
+ let hashed = hash_secret(&register.password);
let new_user = NewUser {
email: register.email,
username: register.username,
- password: hashed,
+ password: hashed.into(),
};
- let user_id = match db.add_user(new_user).await {
+ let user_id = match db.add_user(&new_user).await {
Ok(id) => id,
Err(e) => {
error!("failed to add user: {}", e);
@@ -75,11 +77,13 @@ pub async fn register(
let new_session = NewSession {
user_id,
- token: token.as_str(),
+ token: (&token).into(),
};
match db.add_session(&new_session).await {
- Ok(_) => reply_json(RegisterResponse { session: token }),
+ Ok(_) => reply_json(RegisterResponse {
+ session: token.into(),
+ }),
Err(e) => {
error!("failed to add session: {}", e);
reply_error(
@@ -91,10 +95,10 @@ pub async fn register(
}
pub async fn login(
- login: LoginRequest,
+ login: LoginRequest<'_>,
db: impl Database + Clone + Send + Sync,
-) -> JSONResult<ErrorResponseStatus> {
- let user = match db.get_user(login.username.clone()).await {
+) -> JSONResult<ErrorResponseStatus<'_>> {
+ let user = match db.get_user(login.username.borrow()).await {
Ok(u) => u,
Err(e) => {
error!("failed to get user {}: {}", login.username.clone(), e);
@@ -116,7 +120,7 @@ pub async fn login(
}
};
- let verified = verify_str(user.password.as_str(), login.password.as_str());
+ let verified = verify_str(user.password.as_str(), login.password.borrow());
if !verified {
return reply_error(
@@ -125,6 +129,6 @@ pub async fn login(
}
reply_json(LoginResponse {
- session: session.token,
+ session: session.token.into(),
})
}
diff --git a/atuin-server/src/lib.rs b/atuin-server/src/lib.rs
index 36b6ffa7..e4858811 100644
--- a/atuin-server/src/lib.rs
+++ b/atuin-server/src/lib.rs
@@ -1,3 +1,5 @@
+#![forbid(unsafe_code)]
+
use std::net::IpAddr;
use eyre::Result;
diff --git a/atuin-server/src/models.rs b/atuin-server/src/models.rs
index fbf1897e..d493153a 100644
--- a/atuin-server/src/models.rs
+++ b/atuin-server/src/models.rs
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
use chrono::prelude::*;
#[derive(sqlx::FromRow)]
@@ -14,12 +16,12 @@ pub struct History {
}
pub struct NewHistory<'a> {
- pub client_id: &'a str,
+ pub client_id: Cow<'a, str>,
pub user_id: i64,
- pub hostname: &'a str,
+ pub hostname: Cow<'a, str>,
pub timestamp: chrono::NaiveDateTime,
- pub data: &'a str,
+ pub data: Cow<'a, str>,
}
#[derive(sqlx::FromRow)]
@@ -37,13 +39,13 @@ pub struct Session {
pub token: String,
}
-pub struct NewUser {
- pub username: String,
- pub email: String,
- pub password: String,
+pub struct NewUser<'a> {
+ pub username: Cow<'a, str>,
+ pub email: Cow<'a, str>,
+ pub password: Cow<'a, str>,
}
pub struct NewSession<'a> {
pub user_id: i64,
- pub token: &'a str,
+ pub token: Cow<'a, str>,
}