Skip to content

Commit

Permalink
Add Authentication Script endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
arturofigueroabim committed Mar 1, 2024
1 parent 494a3c3 commit 1a4af67
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,39 @@ impl Db {
.optional()
}

pub async fn check_token_status(&mut self, params: TokensQueryParams) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
pub fn is_token_available(&mut self, user: String, project: String, bridgehead: String) -> Result<Option<TokenManager>, Error> {
tokens
.filter(user_id.eq(user))
.filter(project_id.eq(project))
.filter(bk.eq(bridgehead))
.order(id.desc())
.select(TokenManager::as_select())
.first::<TokenManager>(&mut self.0)
.optional()
}

pub async fn check_script_status(&mut self, params: TokenParams) -> Result<String, (StatusCode, String)> {
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<Json<serde_json::Value>, (StatusCode, String)> {

let mut token_status_json = json!({
"project_id": params.project_id.clone(),
Expand Down
13 changes: 12 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ async fn check_token_status(
}
}

async fn check_script_status(
mut db: Db,
status_params: Json<TokenParams>,
) -> 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<TokenParams>,
Expand Down Expand Up @@ -124,6 +134,7 @@ pub fn configure_routes(pool: diesel::r2d2::Pool<diesel::r2d2::ConnectionManager
.route("/project-status", get(check_project_status))
.route("/script", post(generate_script))
.route("/refreshToken", put(refresh_token))
.route("/project", delete(remove_project_and_token))
.route("/project", delete(remove_project_and_token))
.route("/authentication-status", post(check_script_status))
.with_state(pool)
}

0 comments on commit 1a4af67

Please sign in to comment.