From f17a5bdae03e4c9c58db7572c9875d0649e60338 Mon Sep 17 00:00:00 2001 From: Arturo Figueroa Date: Wed, 14 Feb 2024 10:15:58 +0100 Subject: [PATCH] Update refreshToken endpoint --- src/db.rs | 15 +++++++++++---- src/handlers.rs | 21 +++++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/db.rs b/src/db.rs index 448db99..1138ee6 100644 --- a/src/db.rs +++ b/src/db.rs @@ -11,7 +11,7 @@ use crate::config::CONFIG; use crate::schema::tokens::dsl::*; use crate::schema::tokens; use crate::enums::{OpalTokenStatus, OpalProjectStatus}; -use crate::handlers::{check_project_status_request, fetch_project_tables_request, check_token_status_request}; +use crate::handlers::{check_project_status_request, fetch_project_tables_names_request, check_token_status_request}; use crate::models::{NewToken, TokenManager, TokenParams, TokenStatus}; const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); @@ -150,8 +150,6 @@ impl Db { } pub fn get_token_name(&mut self, user: String, project: String) -> Result, Error> { - - tokens .filter(user_id.eq(user)) .filter(project_id.eq(project)) @@ -160,6 +158,15 @@ impl Db { .optional() } + pub fn get_token_value(&mut self, user: String, project: String) -> Result, Error> { + tokens + .filter(user_id.eq(user)) + .filter(project_id.eq(project)) + .select(token) + .first::(&mut self.0) + .optional() + } + pub async fn check_token_status( &mut self, user: String, @@ -229,7 +236,7 @@ impl Db { } pub async fn generate_user_script(&mut self, query: TokenParams) -> Result { - let tables_result = fetch_project_tables_request(query.clone()).await; + let tables_result = fetch_project_tables_names_request(query.clone()).await; let mut script_lines = Vec::new(); if let Ok(tables) = tables_result { diff --git a/src/handlers.rs b/src/handlers.rs index 0d3de5c..e0efc09 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -110,12 +110,21 @@ pub async fn remove_tokens_request(mut db: Db, token_params: TokensQueryParams) } pub async fn refresh_token_request(mut db: Db, token_params: TokenParams) -> Result { - let token_name_result = db.get_token_name(token_params.user_id.clone(), token_params.project_id.clone()); - - let token_name = match token_name_result { + + let token_name = match db.get_token_name(token_params.user_id.clone(), token_params.project_id.clone()) { Ok(Some(name)) => name, Ok(None) => { - return Err(anyhow::Error::msg("Token not found")) + return Err(anyhow::Error::msg("Token name not found")) + }, + Err(e) => { + return Err(e.into()); + } + }; + + let token_value = match db.get_token_value(token_params.user_id.clone(), token_params.project_id.clone()) { + Ok(Some(value)) => value, + Ok(None) => { + return Err(anyhow::Error::msg("Token value not found")) }, Err(e) => { return Err(e.into()); @@ -127,7 +136,7 @@ pub async fn refresh_token_request(mut db: Db, token_params: TokenParams) -> Res Some(token_name.clone()), Some(token_params.project_id.clone().to_string()), Some(&token_params.bridgehead_ids), - None, None + None, Some(token_value.clone()) ).await?; info!("Refresh token task {task:#?}"); @@ -140,7 +149,7 @@ pub async fn refresh_token_request(mut db: Db, token_params: TokenParams) -> Res } } -pub async fn fetch_project_tables_request(token_params: TokenParams) -> Result, anyhow::Error> { +pub async fn fetch_project_tables_names_request(token_params: TokenParams) -> Result, anyhow::Error> { let task = create_and_send_task_request( OpalRequestType::SCRIPT, Some(token_params.user_id.clone().to_string()),