From 1a4af67ce3b0233f5018e2ecb173d80961a1deab Mon Sep 17 00:00:00 2001 From: Arturo Figueroa Date: Fri, 1 Mar 2024 09:54:56 +0100 Subject: [PATCH] Add Authentication Script endpoint --- src/db.rs | 34 +++++++++++++++++++++++++++++++++- src/routes.rs | 13 ++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 0b94945..5d422d3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -167,7 +167,39 @@ impl Db { .optional() } - pub async fn check_token_status(&mut self, params: TokensQueryParams) -> Result, (StatusCode, String)> { + pub fn is_token_available(&mut self, user: String, project: String, bridgehead: String) -> Result, Error> { + tokens + .filter(user_id.eq(user)) + .filter(project_id.eq(project)) + .filter(bk.eq(bridgehead)) + .order(id.desc()) + .select(TokenManager::as_select()) + .first::(&mut self.0) + .optional() + } + + pub async fn check_script_status(&mut self, params: TokenParams) -> Result { + let mut missing_bridgeheads = Vec::new(); + for bridgehead in params.bridgehead_ids.iter() { + let token_available = self.is_token_available(params.user_id.clone(), params.project_id.clone(), bridgehead.clone()); + if token_available.is_err() || token_available.unwrap().is_none() { + missing_bridgeheads.push(bridgehead.clone()); + } + } + + if missing_bridgeheads.len() < params.bridgehead_ids.len() { + let message = format!("Missing some tokens for bridgeheads: {:?}", missing_bridgeheads); + info!(message); + Ok(message) + } else { + let error_message = format!("Missing all tokens for bridgeheads: {:?}", missing_bridgeheads); + info!(error_message); + Err((StatusCode::NOT_FOUND, error_message )) + } + } + + + pub async fn check_token_status(&mut self, params: TokensQueryParams) -> Result, (StatusCode, String)> { let mut token_status_json = json!({ "project_id": params.project_id.clone(), diff --git a/src/routes.rs b/src/routes.rs index c258e36..e9b6f3d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -52,6 +52,16 @@ async fn check_token_status( } } +async fn check_script_status( + mut db: Db, + status_params: Json, +) -> impl IntoResponse { + match db.check_script_status(status_params.0).await { + Ok(json) => (StatusCode::OK, json).into_response(), + Err((status, message)) => (status, Json(json!({"message": message}))).into_response(), + } +} + async fn generate_script( mut db: Db, script_params: Json, @@ -124,6 +134,7 @@ pub fn configure_routes(pool: diesel::r2d2::Pool