Skip to content

Commit 377792d

Browse files
committed
🎨 format codes
1 parent 1845cc1 commit 377792d

2 files changed

Lines changed: 48 additions & 26 deletions

File tree

src/handlers.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub async fn registry_v2_check(
109109
pub async fn auth_handler(
110110
State(state): State<Arc<AppState>>,
111111
Query(auth_req): Query<AuthRequest>,
112-
headers: HeaderMap
112+
headers: HeaderMap,
113113
) -> impl IntoResponse {
114114
debug!(
115115
service = auth_req.service,
@@ -238,34 +238,48 @@ pub async fn auth_handler(
238238
let mut scope_parts_clone = scope_parts.clone();
239239
let library_scope = format!("library/{}", scope_parts_clone[1]);
240240
scope_parts_clone[1] = &library_scope;
241-
info!("Converted scope {} -> {}", auth_req.scope.clone().unwrap_or_default(), library_scope);
241+
info!(
242+
"Converted scope {} -> {}",
243+
auth_req.scope.clone().unwrap_or_default(),
244+
library_scope
245+
);
242246
auth_req.scope = Some(scope_parts_clone.join(":"));
243247
} else {
244248
// Check if user is admin of the specified game scope
245249
match state.database.get_game_by_namespace(namespace).await {
246250
Ok(Some(game)) => {
247251
if !game.is_admin(user.id) {
248-
warn!("User '{}' is not admin of game {}: {}", username, game.id, game.bucket);
252+
warn!(
253+
"User '{}' is not admin of game {}: {}",
254+
username, game.id, game.bucket
255+
);
249256
return create_error_response(
250257
StatusCode::UNAUTHORIZED,
251258
"Access denied",
252259
);
253260
}
254261
let bucket_clone = game.bucket.clone();
255262
if bucket_clone != namespace {
256-
// convert namespace from id to bucket
257-
let mut scope_image_parts = scope_image_parts.to_vec();
258-
scope_image_parts[0] = &bucket_clone;
259-
let mut scope_parts = scope_parts.to_vec();
260-
let scope_name = scope_image_parts.join("/");
261-
scope_parts[1] = &scope_name;
262-
let new_scope = scope_parts.join(":");
263-
info!("Converted scope {} -> {}", auth_req.scope.unwrap_or_default(), new_scope);
264-
auth_req.scope = Some(new_scope);
263+
// convert namespace from id to bucket
264+
let mut scope_image_parts = scope_image_parts.to_vec();
265+
scope_image_parts[0] = &bucket_clone;
266+
let mut scope_parts = scope_parts.to_vec();
267+
let scope_name = scope_image_parts.join("/");
268+
scope_parts[1] = &scope_name;
269+
let new_scope = scope_parts.join(":");
270+
info!(
271+
"Converted scope {} -> {}",
272+
auth_req.scope.unwrap_or_default(),
273+
new_scope
274+
);
275+
auth_req.scope = Some(new_scope);
265276
}
266277
}
267278
Ok(None) => {
268-
warn!(user = username, "Game not found for namespace: {}", namespace);
279+
warn!(
280+
user = username,
281+
"Game not found for namespace: {}", namespace
282+
);
269283
return create_error_response(StatusCode::UNAUTHORIZED, "Access denied");
270284
}
271285
Err(e) => {

src/middleware.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use axum::{
77
};
88
use jsonwebtoken::{decode, DecodingKey, Validation};
99
use std::sync::Arc;
10-
use tracing::{debug, info, warn, error};
10+
use tracing::{debug, error, info, warn};
1111

1212
use crate::{
1313
models::{Claims, ProxyContext},
@@ -24,7 +24,10 @@ impl AuthMiddleware {
2424
next: Next,
2525
) -> Result<Response, StatusCode> {
2626
// Extract scope from path
27-
let namespace = params.get("namespace").map(String::as_str).unwrap_or("library");
27+
let namespace = params
28+
.get("namespace")
29+
.map(String::as_str)
30+
.unwrap_or("library");
2831
let repository = params.get("repository").map(String::as_str).unwrap_or("_");
2932
if repository == "_" {
3033
return Err(StatusCode::FORBIDDEN);
@@ -39,10 +42,14 @@ impl AuthMiddleware {
3942
if namespace != game.bucket {
4043
info!("Translate namespace {} -> {}", namespace, game.bucket);
4144
let uri = request.uri();
42-
let new_uri = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or(uri.path()).replace(
43-
&format!("/v2/{}/{}/", namespace, repository),
44-
&format!("/v2/{}/{}/", game.bucket, repository)
45-
);
45+
let new_uri = uri
46+
.path_and_query()
47+
.map(|pq| pq.as_str())
48+
.unwrap_or(uri.path())
49+
.replace(
50+
&format!("/v2/{}/{}/", namespace, repository),
51+
&format!("/v2/{}/{}/", game.bucket, repository),
52+
);
4653

4754
// change request uri
4855
debug!("Modify uri: {} -> {}", uri.path(), new_uri);
@@ -62,7 +69,6 @@ impl AuthMiddleware {
6269
}
6370
};
6471

65-
6672
let repo = format!("{}/{}", namespace, repository);
6773
debug!("Checking scope admin permission for scope: {}", repo);
6874

@@ -151,18 +157,21 @@ impl AuthMiddleware {
151157
Some(_) => {}
152158
None => {
153159
let uri = request.uri();
154-
let new_uri = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or(uri.path()).replace(
155-
&format!("/v2/{}/", repository),
156-
&format!("/v2/library/{}/", repository)
157-
);
160+
let new_uri = uri
161+
.path_and_query()
162+
.map(|pq| pq.as_str())
163+
.unwrap_or(uri.path())
164+
.replace(
165+
&format!("/v2/{}/", repository),
166+
&format!("/v2/library/{}/", repository),
167+
);
158168

159169
// change request uri
160170
debug!("Modify uri: {} -> {}", uri.path(), new_uri);
161171
*request.uri_mut() = axum::http::Uri::try_from(new_uri).unwrap();
162172
}
163173
}
164174

165-
166175
debug!("Checking library access for repository: {}", repository);
167176

168177
// Check JWT token
@@ -247,4 +256,3 @@ impl AuthMiddleware {
247256
Err(StatusCode::UNAUTHORIZED)
248257
}
249258
}
250-

0 commit comments

Comments
 (0)