Skip to content

Commit

Permalink
Update refreshToken endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
arturofigueroabim committed Feb 14, 2024
1 parent 388fba4 commit f17a5bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down Expand Up @@ -150,8 +150,6 @@ impl Db {
}

pub fn get_token_name(&mut self, user: String, project: String) -> Result<Option<String>, Error> {


tokens
.filter(user_id.eq(user))
.filter(project_id.eq(project))
Expand All @@ -160,6 +158,15 @@ impl Db {
.optional()
}

pub fn get_token_value(&mut self, user: String, project: String) -> Result<Option<String>, Error> {
tokens
.filter(user_id.eq(user))
.filter(project_id.eq(project))
.select(token)
.first::<String>(&mut self.0)
.optional()
}

pub async fn check_token_status(
&mut self,
user: String,
Expand Down Expand Up @@ -229,7 +236,7 @@ impl Db {
}

pub async fn generate_user_script(&mut self, query: TokenParams) -> Result<String, String> {
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 {
Expand Down
21 changes: 15 additions & 6 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpalResponse> {
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());
Expand All @@ -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:#?}");
Expand All @@ -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<Vec<String>, anyhow::Error> {
pub async fn fetch_project_tables_names_request(token_params: TokenParams) -> Result<Vec<String>, anyhow::Error> {
let task = create_and_send_task_request(
OpalRequestType::SCRIPT,
Some(token_params.user_id.clone().to_string()),
Expand Down

0 comments on commit f17a5bd

Please sign in to comment.