fc-server: fix clippy warnings about manual clamp patterns
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I5cc30a1f6f7444a7acd836430ad44a576a6a6964
This commit is contained in:
parent
6c322c061b
commit
d620ec5454
3 changed files with 88 additions and 88 deletions
|
|
@ -73,43 +73,45 @@ pub async fn require_api_key(
|
||||||
{
|
{
|
||||||
// Try user session first (new fc_user_session cookie)
|
// Try user session first (new fc_user_session cookie)
|
||||||
if let Some(session_id) = parse_cookie(cookie_header, "fc_user_session")
|
if let Some(session_id) = parse_cookie(cookie_header, "fc_user_session")
|
||||||
&& let Some(session) = state.sessions.get(&session_id) {
|
&& let Some(session) = state.sessions.get(&session_id)
|
||||||
// Check session expiry (24 hours)
|
{
|
||||||
if session.created_at.elapsed()
|
// Check session expiry (24 hours)
|
||||||
< std::time::Duration::from_secs(24 * 60 * 60)
|
if session.created_at.elapsed()
|
||||||
{
|
< std::time::Duration::from_secs(24 * 60 * 60)
|
||||||
// Insert both user and session data
|
{
|
||||||
if let Some(ref user) = session.user {
|
// Insert both user and session data
|
||||||
request.extensions_mut().insert(user.clone());
|
if let Some(ref user) = session.user {
|
||||||
}
|
request.extensions_mut().insert(user.clone());
|
||||||
if let Some(ref api_key) = session.api_key {
|
|
||||||
request.extensions_mut().insert(api_key.clone());
|
|
||||||
}
|
|
||||||
return Ok(next.run(request).await);
|
|
||||||
} else {
|
|
||||||
// Expired, remove it
|
|
||||||
drop(session);
|
|
||||||
state.sessions.remove(&session_id);
|
|
||||||
}
|
}
|
||||||
|
if let Some(ref api_key) = session.api_key {
|
||||||
|
request.extensions_mut().insert(api_key.clone());
|
||||||
|
}
|
||||||
|
return Ok(next.run(request).await);
|
||||||
|
} else {
|
||||||
|
// Expired, remove it
|
||||||
|
drop(session);
|
||||||
|
state.sessions.remove(&session_id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try legacy API key session (fc_session cookie)
|
// Try legacy API key session (fc_session cookie)
|
||||||
if let Some(session_id) = parse_cookie(cookie_header, "fc_session")
|
if let Some(session_id) = parse_cookie(cookie_header, "fc_session")
|
||||||
&& let Some(session) = state.sessions.get(&session_id) {
|
&& let Some(session) = state.sessions.get(&session_id)
|
||||||
// Check session expiry (24 hours)
|
{
|
||||||
if session.created_at.elapsed()
|
// Check session expiry (24 hours)
|
||||||
< std::time::Duration::from_secs(24 * 60 * 60)
|
if session.created_at.elapsed()
|
||||||
{
|
< std::time::Duration::from_secs(24 * 60 * 60)
|
||||||
if let Some(ref api_key) = session.api_key {
|
{
|
||||||
request.extensions_mut().insert(api_key.clone());
|
if let Some(ref api_key) = session.api_key {
|
||||||
}
|
request.extensions_mut().insert(api_key.clone());
|
||||||
return Ok(next.run(request).await);
|
|
||||||
} else {
|
|
||||||
// Expired, remove it
|
|
||||||
drop(session);
|
|
||||||
state.sessions.remove(&session_id);
|
|
||||||
}
|
}
|
||||||
|
return Ok(next.run(request).await);
|
||||||
|
} else {
|
||||||
|
// Expired, remove it
|
||||||
|
drop(session);
|
||||||
|
state.sessions.remove(&session_id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No valid auth found
|
// No valid auth found
|
||||||
|
|
@ -133,18 +135,19 @@ impl FromRequestParts<AppState> for RequireAdmin {
|
||||||
) -> Result<Self, Self::Rejection> {
|
) -> Result<Self, Self::Rejection> {
|
||||||
// Check for user first (new auth)
|
// Check for user first (new auth)
|
||||||
if let Some(user) = parts.extensions.get::<User>()
|
if let Some(user) = parts.extensions.get::<User>()
|
||||||
&& user.role == "admin" {
|
&& user.role == "admin"
|
||||||
// Create a synthetic API key for compatibility
|
{
|
||||||
return Ok(RequireAdmin(ApiKey {
|
// Create a synthetic API key for compatibility
|
||||||
id: user.id,
|
return Ok(RequireAdmin(ApiKey {
|
||||||
name: user.username.clone(),
|
id: user.id,
|
||||||
key_hash: String::new(),
|
name: user.username.clone(),
|
||||||
role: user.role.clone(),
|
key_hash: String::new(),
|
||||||
created_at: user.created_at,
|
role: user.role.clone(),
|
||||||
last_used_at: user.last_login_at,
|
created_at: user.created_at,
|
||||||
user_id: Some(user.id),
|
last_used_at: user.last_login_at,
|
||||||
}));
|
user_id: Some(user.id),
|
||||||
}
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
// Fall back to API key
|
// Fall back to API key
|
||||||
let key = parts
|
let key = parts
|
||||||
|
|
@ -173,17 +176,18 @@ impl RequireRoles {
|
||||||
) -> Result<ApiKey, StatusCode> {
|
) -> Result<ApiKey, StatusCode> {
|
||||||
// Check for user first
|
// Check for user first
|
||||||
if let Some(user) = extensions.get::<User>()
|
if let Some(user) = extensions.get::<User>()
|
||||||
&& (user.role == "admin" || allowed.contains(&user.role.as_str())) {
|
&& (user.role == "admin" || allowed.contains(&user.role.as_str()))
|
||||||
return Ok(ApiKey {
|
{
|
||||||
id: user.id,
|
return Ok(ApiKey {
|
||||||
name: user.username.clone(),
|
id: user.id,
|
||||||
key_hash: String::new(),
|
name: user.username.clone(),
|
||||||
role: user.role.clone(),
|
key_hash: String::new(),
|
||||||
created_at: user.created_at,
|
role: user.role.clone(),
|
||||||
last_used_at: user.last_login_at,
|
created_at: user.created_at,
|
||||||
user_id: Some(user.id),
|
last_used_at: user.last_login_at,
|
||||||
});
|
user_id: Some(user.id),
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Fall back to API key
|
// Fall back to API key
|
||||||
let key = extensions
|
let key = extensions
|
||||||
|
|
@ -217,38 +221,40 @@ pub async fn extract_session(
|
||||||
if let Some(cookie_header) = cookie_header {
|
if let Some(cookie_header) = cookie_header {
|
||||||
// Try user session first
|
// Try user session first
|
||||||
if let Some(session_id) = parse_cookie(&cookie_header, "fc_user_session")
|
if let Some(session_id) = parse_cookie(&cookie_header, "fc_user_session")
|
||||||
&& let Some(session) = state.sessions.get(&session_id) {
|
&& let Some(session) = state.sessions.get(&session_id)
|
||||||
// Check session expiry
|
{
|
||||||
if session.created_at.elapsed()
|
// Check session expiry
|
||||||
< std::time::Duration::from_secs(24 * 60 * 60)
|
if session.created_at.elapsed()
|
||||||
{
|
< std::time::Duration::from_secs(24 * 60 * 60)
|
||||||
if let Some(ref user) = session.user {
|
{
|
||||||
request.extensions_mut().insert(user.clone());
|
if let Some(ref user) = session.user {
|
||||||
}
|
request.extensions_mut().insert(user.clone());
|
||||||
if let Some(ref api_key) = session.api_key {
|
|
||||||
request.extensions_mut().insert(api_key.clone());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
drop(session);
|
|
||||||
state.sessions.remove(&session_id);
|
|
||||||
}
|
}
|
||||||
|
if let Some(ref api_key) = session.api_key {
|
||||||
|
request.extensions_mut().insert(api_key.clone());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
drop(session);
|
||||||
|
state.sessions.remove(&session_id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try legacy API key session
|
// Try legacy API key session
|
||||||
if let Some(session_id) = parse_cookie(&cookie_header, "fc_session")
|
if let Some(session_id) = parse_cookie(&cookie_header, "fc_session")
|
||||||
&& let Some(session) = state.sessions.get(&session_id) {
|
&& let Some(session) = state.sessions.get(&session_id)
|
||||||
// Check session expiry
|
{
|
||||||
if session.created_at.elapsed()
|
// Check session expiry
|
||||||
< std::time::Duration::from_secs(24 * 60 * 60)
|
if session.created_at.elapsed()
|
||||||
{
|
< std::time::Duration::from_secs(24 * 60 * 60)
|
||||||
if let Some(ref api_key) = session.api_key {
|
{
|
||||||
request.extensions_mut().insert(api_key.clone());
|
if let Some(ref api_key) = session.api_key {
|
||||||
}
|
request.extensions_mut().insert(api_key.clone());
|
||||||
} else {
|
|
||||||
drop(session);
|
|
||||||
state.sessions.remove(&session_id);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
drop(session);
|
||||||
|
state.sessions.remove(&session_id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
next.run(request).await
|
next.run(request).await
|
||||||
|
|
|
||||||
|
|
@ -435,7 +435,7 @@ async fn projects_page(
|
||||||
Query(params): Query<PageParams>,
|
Query(params): Query<PageParams>,
|
||||||
extensions: Extensions,
|
extensions: Extensions,
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
let limit = params.limit.unwrap_or(50).min(200).max(1);
|
let limit = params.limit.unwrap_or(50).clamp(1, 200);
|
||||||
let offset = params.offset.unwrap_or(0).max(0);
|
let offset = params.offset.unwrap_or(0).max(0);
|
||||||
let items = fc_common::repo::projects::list(&state.pool, limit, offset)
|
let items = fc_common::repo::projects::list(&state.pool, limit, offset)
|
||||||
.await
|
.await
|
||||||
|
|
@ -602,7 +602,7 @@ async fn evaluations_page(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Query(params): Query<PageParams>,
|
Query(params): Query<PageParams>,
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
let limit = params.limit.unwrap_or(50).min(200).max(1);
|
let limit = params.limit.unwrap_or(50).clamp(1, 200);
|
||||||
let offset = params.offset.unwrap_or(0).max(0);
|
let offset = params.offset.unwrap_or(0).max(0);
|
||||||
let items = fc_common::repo::evaluations::list_filtered(
|
let items = fc_common::repo::evaluations::list_filtered(
|
||||||
&state.pool,
|
&state.pool,
|
||||||
|
|
@ -760,7 +760,7 @@ async fn builds_page(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Query(params): Query<BuildFilterParams>,
|
Query(params): Query<BuildFilterParams>,
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
let limit = params.limit.unwrap_or(50).min(200).max(1);
|
let limit = params.limit.unwrap_or(50).clamp(1, 200);
|
||||||
let offset = params.offset.unwrap_or(0).max(0);
|
let offset = params.offset.unwrap_or(0).max(0);
|
||||||
let items = fc_common::repo::builds::list_filtered(
|
let items = fc_common::repo::builds::list_filtered(
|
||||||
&state.pool,
|
&state.pool,
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,7 @@ use axum::{
|
||||||
routing::get,
|
routing::get,
|
||||||
};
|
};
|
||||||
use fc_common::{
|
use fc_common::{
|
||||||
models::{
|
models::{CreateStarredJob, CreateUser, PaginationParams, UpdateUser, User},
|
||||||
CreateStarredJob,
|
|
||||||
CreateUser,
|
|
||||||
PaginationParams,
|
|
||||||
UpdateUser,
|
|
||||||
User,
|
|
||||||
},
|
|
||||||
repo::{self},
|
repo::{self},
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue