summaryrefslogtreecommitdiffstats
path: root/src/auth/credentials.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth/credentials.rs')
-rw-r--r--src/auth/credentials.rs35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/auth/credentials.rs b/src/auth/credentials.rs
index 7f92048..88253b3 100644
--- a/src/auth/credentials.rs
+++ b/src/auth/credentials.rs
@@ -5,18 +5,22 @@ use argon2::{
use axum::{
extract::{Path, State},
http::StatusCode,
+ Json,
};
-use axum_extra::{
- headers::{authorization::Basic, Authorization},
- routing::Resource,
- TypedHeader,
-};
+use axum_extra::routing::Resource;
+use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::state::AppState;
use super::{error::Error, AccessClaims, RefreshClaims};
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Credential {
+ pub password: String,
+}
+
pub fn router() -> Resource<AppState> {
Resource::named("credentials")
.create(create)
@@ -25,10 +29,10 @@ pub fn router() -> Resource<AppState> {
pub async fn create(
State(state): State<AppState>,
- TypedHeader(Authorization(basic)): TypedHeader<Authorization<Basic>>,
+ Json(Credential { password }): Json<Credential>,
) -> Result<(StatusCode, (AccessClaims, RefreshClaims)), Error> {
let salt = SaltString::generate(&mut OsRng);
- let password_hash = Argon2::default().hash_password(basic.password().as_bytes(), &salt)?;
+ let password_hash = Argon2::default().hash_password(password.as_bytes(), &salt)?;
let uuid = sqlx::query!(
"INSERT INTO credential (password_hash) VALUES ($1) RETURNING id",
@@ -52,15 +56,12 @@ pub async fn destroy(State(state): State<AppState>, Path(uuid): Path<Uuid>) -> R
.await?
.rows_affected();
- match rows {
- 0 => Err(Error::UserNotFound),
- 1 => {
- tx.commit().await?;
- Ok(())
- }
- _ => {
- tracing::error!("Delete query affected {rows} rows. This should not happen.");
- Ok(())
- }
+ if rows == 0 {
+ return Err(Error::UserNotFound);
+ } else if rows > 1 {
+ tracing::warn!("DELETE query affected {rows} rows. This should not happen.");
}
+
+ tx.commit().await?;
+ Ok(())
}