From 34329678d7f8e1dae89f9ae4340691e0d5750fc0 Mon Sep 17 00:00:00 2001 From: John Cantrell Date: Fri, 23 Sep 2022 17:01:51 -0400 Subject: [PATCH] ensure init instance only works once --- senseicore/src/services/admin.rs | 26 ++++++++++++++++---------- src/http/admin.rs | 31 +++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/senseicore/src/services/admin.rs b/senseicore/src/services/admin.rs index f54dab2..4e56d2e 100644 --- a/senseicore/src/services/admin.rs +++ b/senseicore/src/services/admin.rs @@ -358,16 +358,22 @@ impl AdminService { username, passphrase, } => { - let root_token = self.database.create_root_access_token().await.unwrap(); - let _user = self - .database - .create_user(username, passphrase) - .await - .unwrap(); - - Ok(AdminResponse::CreateAdmin { - token: root_token.token, - }) + let root_token = self.database.get_root_access_token().await.unwrap(); + match root_token { + Some(_) => Err(Error::Generic("Instance already initialized".to_string())), + None => { + let root_token = self.database.create_root_access_token().await.unwrap(); + let _user = self + .database + .create_user(username, passphrase) + .await + .unwrap(); + + Ok(AdminResponse::CreateAdmin { + token: root_token.token, + }) + } + } } AdminRequest::StartNode { pubkey, passphrase } => { let node = self.database.get_node_by_pubkey(&pubkey).await?; diff --git a/src/http/admin.rs b/src/http/admin.rs index 66dad31..a5c8489 100644 --- a/src/http/admin.rs +++ b/src/http/admin.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use axum::{ extract::{Extension, Query}, + response::IntoResponse, routing::{delete, get, post}, Json, Router, }; @@ -22,7 +23,7 @@ use serde_json::{json, Value}; use senseicore::{ services::{ - admin::{AdminRequest, AdminResponse, AdminService, NodeCreateInfo}, + admin::{AdminRequest, AdminResponse, AdminService, Error, NodeCreateInfo}, PaginationRequest, }, utils, @@ -759,12 +760,18 @@ pub async fn init_sensei( Extension(admin_service): Extension>, cookies: Cookies, Json(payload): Json, -) -> Result, StatusCode> { +) -> impl IntoResponse { let params: Result = serde_json::from_value(payload); + let request = match params { - Ok(params) => Ok(params.into()), - Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY), - }?; + Ok(params) => params.into(), + Err(_) => { + return ( + StatusCode::UNPROCESSABLE_ENTITY, + Json(json!({"error": "invalid params"})), + ) + } + }; match admin_service.call(request).await { Ok(response) => match response { @@ -774,11 +781,19 @@ pub async fn init_sensei( .finish(); cookies.add(token_cookie); - Ok(Json(AdminResponse::CreateAdmin { token })) + (StatusCode::OK, Json(json!({ "token": token }))) } - _ => Err(StatusCode::UNPROCESSABLE_ENTITY), + _ => ( + StatusCode::UNPROCESSABLE_ENTITY, + Json(json!({"error": "unexpected error"})), + ), + }, + Err(err) => match err { + Error::Generic(msg) => ( + StatusCode::UNPROCESSABLE_ENTITY, + Json(json!({ "error": msg })), + ), }, - Err(err) => Ok(Json(AdminResponse::Error(err))), } }