Skip to content

Commit 1caadff

Browse files
committed
refactor
1 parent 02f6b0d commit 1caadff

File tree

12 files changed

+90
-52
lines changed

12 files changed

+90
-52
lines changed

docker-compose.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ services:
3333
timeout: 5s
3434
retries: 5
3535

36-
open-erase-iso-builder:
37-
container_name: open-erase-iso-builder
38-
image: open-erase-iso-builder
39-
cap_add:
40-
- SYS_ADMIN
41-
stdin_open: true
42-
build:
43-
context: .
44-
dockerfile: iso-builder.Dockerfile
45-
volumes:
46-
- iso:/iso/out
47-
command: >
48-
sh -c "ls -a && mkarchiso -v -w /tmp/archiso-tmp -o /iso/out /iso"
36+
# open-erase-iso-builder:
37+
# container_name: open-erase-iso-builder
38+
# image: open-erase-iso-builder
39+
# cap_add:
40+
# - SYS_ADMIN
41+
# stdin_open: true
42+
# build:
43+
# context: .
44+
# dockerfile: iso-builder.Dockerfile
45+
# volumes:
46+
# - iso:/iso/out
47+
# command: >
48+
# sh -c "ls -a && mkarchiso -v -w /tmp/archiso-tmp -o /iso/out /iso"
4949

5050
volumes:
5151
db:

server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ chrono = { version = "0.4.42", features = ["serde"] }
1313
getrandom = "0.3.4"
1414
jsonwebtoken = "9.3.1"
1515
serde = { version = "1.0.228", features = ["derive"] }
16+
serde_json = "1.0.145"
1617
sqlx = { version = "0.8.6", features = [
1718
"chrono",
1819
"postgres",
@@ -37,4 +38,3 @@ utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
3738
uuid = { version = "1.18.1", features = ["serde"] }
3839

3940
[dev-dependencies]
40-
serde_json = "1.0.145"

server/src/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub enum ServiceError {
5858
Hash(argon2::password_hash::Error),
5959
Token(jsonwebtoken::errors::Error),
6060
Uuid(uuid::Error),
61+
Serialization(serde_json::Error),
6162
}
6263

6364
impl IntoResponse for ServiceError {
@@ -92,6 +93,12 @@ impl From<uuid::Error> for ServiceError {
9293
}
9394
}
9495

96+
impl From<serde_json::Error> for ServiceError {
97+
fn from(value: serde_json::Error) -> Self {
98+
Self::Serialization(value)
99+
}
100+
}
101+
95102
#[allow(dead_code)]
96103
#[derive(Debug)]
97104
pub enum RepositoryError {
@@ -118,6 +125,7 @@ impl From<ServiceError> for ErrorResponse {
118125
ServiceError::Hash(_error) => ErrorResponse::unauthorized(),
119126
ServiceError::Token(_error) => ErrorResponse::internal_server_error(),
120127
ServiceError::Uuid(_error) => ErrorResponse::internal_server_error(),
128+
ServiceError::Serialization(_error) => ErrorResponse::internal_server_error(),
121129
}
122130
}
123131
}

server/src/handlers/auth.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use axum::{Extension, extract::State};
1+
use axum::{Extension, Json, extract::State};
22

33
use crate::{
44
error::AppResult,
55
models::User,
6-
schemas::token::{LoginResponse, RefreshResponse},
6+
schemas::token::{LoginResponse, RefreshRequest, RefreshResponse},
77
state::AppState,
88
};
99

@@ -23,6 +23,7 @@ pub async fn login(
2323
pub async fn refresh(
2424
State(state): State<AppState>,
2525
Extension(user): Extension<User>,
26+
Json(refresh_request): Json<RefreshRequest>,
2627
) -> AppResult<RefreshResponse> {
2728
let access_token = state.auth_service.generate_access_token(&user)?;
2829
Ok(RefreshResponse::new(access_token))

server/src/middleware/auth.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ pub async fn validate_basic_auth(
4242
next: Next,
4343
) -> AppResult<impl IntoResponse> {
4444
let authorization_header = header_result.map_err(|_| ClientError::Unauthorized)?;
45+
4546
let user = state
46-
.user_service
47-
.find_user_by_email(authorization_header.username())
47+
.auth_service
48+
.validate_basic_auth(
49+
authorization_header.username(),
50+
authorization_header.password(),
51+
)
4852
.await?
4953
.ok_or(ClientError::Unauthorized)?;
50-
state
51-
.auth_service
52-
.verify_password(authorization_header.password(), &user.password_hash)
53-
.map_err(|_| ClientError::Unauthorized)?;
5454
request.extensions_mut().insert(user);
5555
Ok(next.run(request).await)
5656
}

server/src/models/refresh_token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chrono::{DateTime, Local};
1+
use chrono::{DateTime, Utc};
22
use sqlx::FromRow;
33
use uuid::Uuid;
44

@@ -7,6 +7,6 @@ pub struct RefreshToken {
77
pub id: Uuid,
88
pub user_id: Uuid,
99
pub refresh_token_hash: String,
10-
pub created_at: DateTime<Local>,
11-
pub updated_at: DateTime<Local>,
10+
pub created_at: DateTime<Utc>,
11+
pub updated_at: DateTime<Utc>,
1212
}

server/src/models/user.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chrono::{DateTime, Local};
1+
use chrono::{DateTime, Utc};
22
use sqlx::prelude::FromRow;
33
use uuid::Uuid;
44

@@ -7,6 +7,6 @@ pub struct User {
77
pub id: Uuid,
88
pub email: String,
99
pub password_hash: String,
10-
pub created_at: DateTime<Local>,
11-
pub updated_at: DateTime<Local>,
10+
pub created_at: DateTime<Utc>,
11+
pub updated_at: DateTime<Utc>,
1212
}

server/src/repositories/mocks/refresh_token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::{Arc, Mutex};
22

33
use async_trait::async_trait;
4-
use chrono::{DateTime, Local};
4+
use chrono::DateTime;
55
use uuid::Uuid;
66

77
use crate::{

server/src/schemas/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
use serde::Serialize;
2+
3+
use crate::error::ServiceResult;
4+
15
pub mod token;
26
pub mod user;
7+
8+
fn json<T: Serialize>(val: T) -> ServiceResult<String> {
9+
Ok(serde_json::to_string_pretty(&val)?)
10+
}

server/src/schemas/token.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use axum::{
2-
Json,
32
http::{StatusCode, header},
43
response::{IntoResponse, Response},
54
};
65
use serde::{Deserialize, Serialize};
6+
use utoipa::ToSchema;
7+
8+
use crate::schemas::json;
79

810
#[derive(Serialize)]
911
pub struct LoginResponse {
@@ -35,13 +37,13 @@ impl IntoResponse for LoginResponse {
3537
&refresh_token_cookie(&self.refresh_token),
3638
),
3739
],
38-
Json(self),
40+
json(self),
3941
)
4042
.into_response()
4143
}
4244
}
4345

44-
#[derive(Deserialize)]
46+
#[derive(Deserialize, ToSchema)]
4547
pub struct RefreshRequest {
4648
pub access_token: String,
4749
}
@@ -70,7 +72,7 @@ impl IntoResponse for RefreshResponse {
7072
(header::CACHE_CONTROL, "no-store"),
7173
(header::PRAGMA, "no-cache"),
7274
],
73-
Json(self),
75+
json(self),
7476
)
7577
.into_response()
7678
}

0 commit comments

Comments
 (0)