Skip to content

Commit fbc9892

Browse files
committed
fix(auth): refactor auth_tokens fully
1 parent f7e6ea9 commit fbc9892

File tree

4 files changed

+44
-97
lines changed

4 files changed

+44
-97
lines changed

src/auth/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
pub mod github;
2-
pub mod token;

src/auth/token.rs

-68
This file was deleted.

src/database/repository/auth_tokens.rs

+37
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,40 @@ pub async fn generate_token(developer_id: i32, conn: &mut PgConnection) -> Resul
2626

2727
Ok(token)
2828
}
29+
30+
pub async fn remove_token(token: Uuid, conn: &mut PgConnection) -> Result<(), ApiError> {
31+
let hash = sha256::digest(token.to_string());
32+
33+
sqlx::query!(
34+
"DELETE FROM auth_tokens
35+
WHERE token = $1",
36+
hash
37+
)
38+
.execute(&mut *conn)
39+
.await
40+
.map_err(|e| {
41+
log::error!("Failed to remove auth token: {}", e);
42+
ApiError::DbError
43+
})?;
44+
45+
Ok(())
46+
}
47+
48+
pub async fn remove_developer_tokens(
49+
developer_id: i32,
50+
conn: &mut PgConnection,
51+
) -> Result<(), ApiError> {
52+
sqlx::query!(
53+
"DELETE FROM auth_tokens
54+
WHERE developer_id = $1",
55+
developer_id
56+
)
57+
.execute(&mut *conn)
58+
.await
59+
.map_err(|e| {
60+
log::error!("Failed to wipe developer tokens: {}", e);
61+
ApiError::DbError
62+
})?;
63+
64+
Ok(())
65+
}

src/endpoints/developers.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};
33
use sqlx::Acquire;
44

55
use crate::{
6-
auth::token,
76
extractors::auth::Auth,
87
types::{
98
api::{ApiError, ApiResponse},
@@ -15,6 +14,7 @@ use crate::{
1514
},
1615
AppData,
1716
};
17+
use crate::database::repository::auth_tokens;
1818

1919
#[derive(Deserialize, Serialize, Debug, Clone)]
2020
pub struct SimpleDevMod {
@@ -181,23 +181,11 @@ pub async fn delete_token(
181181
data: web::Data<AppData>,
182182
auth: Auth,
183183
) -> Result<impl Responder, ApiError> {
184-
let dev = auth.developer()?;
185184
let token = auth.token()?;
186185
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
187-
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
188-
if let Err(e) =
189-
token::invalidate_token_for_developer(dev.id, token.to_string(), &mut transaction).await
190-
{
191-
transaction
192-
.rollback()
193-
.await
194-
.or(Err(ApiError::TransactionError))?;
195-
return Err(e);
196-
}
197-
transaction
198-
.commit()
199-
.await
200-
.or(Err(ApiError::TransactionError))?;
186+
187+
auth_tokens::remove_token(token, &mut pool).await?;
188+
201189
Ok(HttpResponse::NoContent())
202190
}
203191

@@ -208,18 +196,9 @@ pub async fn delete_tokens(
208196
) -> Result<impl Responder, ApiError> {
209197
let dev = auth.developer()?;
210198
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
211-
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
212-
if let Err(e) = token::invalidate_tokens_for_developer(dev.id, &mut transaction).await {
213-
transaction
214-
.rollback()
215-
.await
216-
.or(Err(ApiError::TransactionError))?;
217-
return Err(e);
218-
}
219-
transaction
220-
.commit()
221-
.await
222-
.or(Err(ApiError::TransactionError))?;
199+
200+
auth_tokens::remove_developer_tokens(dev.id, &mut pool).await?;
201+
223202
Ok(HttpResponse::NoContent())
224203
}
225204

0 commit comments

Comments
 (0)