diff --git a/Plugins/Alice-Database/Cargo.toml b/Plugins/Alice-Database/Cargo.toml index 4e7c197..04a5102 100644 --- a/Plugins/Alice-Database/Cargo.toml +++ b/Plugins/Alice-Database/Cargo.toml @@ -2,7 +2,7 @@ name = "Alice-Database_DBMS" version = "1.2.0" edition = "2021" -include = ["**/*.rs", "Cargo.toml"] +include = ["**/*.rs", "Cargo.toml","proto/*.proto"] license = "MIT" description = "The Alice-Database is a comprehensive data management solution designed to integrate seamlessly with the Kingdom System. This plugin provides robust functionality for creating, updating, and deleting collections and documents, ensuring efficient and reliable data storage and retrieva" repository = "https://github.com/0xBLCKLPTN/Kingdom-System/tree/main/Plugins/Alice-Database" @@ -14,6 +14,8 @@ chrono = "0.4.38" env_logger = "0.11.5" log = "0.4.22" prost = "0.13.3" +rand = "0.8.5" +ring = "0.17.8" serde = { version = "1.0.213", features = ["derive"] } serde_json = "1.0.132" simplelog = "0.12.2" diff --git a/Plugins/Alice-Database/proto/instance.proto b/Plugins/Alice-Database/proto/instance.proto index 682771e..f5f5487 100644 --- a/Plugins/Alice-Database/proto/instance.proto +++ b/Plugins/Alice-Database/proto/instance.proto @@ -11,6 +11,7 @@ message Instance { message CreateInstanceRequest { string engine_type = 1; string root_path = 2; + Token token = 3; } message CreateInstanceResponse { @@ -19,6 +20,7 @@ message CreateInstanceResponse { message GetInstanceRequest { string instance_name = 1; + Token token = 2; } message GetInstanceResponse { @@ -27,14 +29,38 @@ message GetInstanceResponse { message GetAllInstancesRequest { string message = 1; + Token token = 2; } message GetAllInstancesResponse { repeated Instance instances = 1; } +message SignInRequest { + string secret_key = 1; + string app_name = 2; +} + +message SingInResponse { + string access = 1; +} + +message SignUpRequest { + string app_name = 1; +} + +message SignUpResponse { + string secret_key = 1; +} + +message Token { + string secret_key = 1; +} + service InstanceService { rpc CreateInstance(CreateInstanceRequest) returns (CreateInstanceResponse); rpc GetInstance(GetInstanceRequest) returns (GetInstanceResponse); rpc GetAllInstances(GetAllInstancesRequest) returns (GetAllInstancesResponse); + //rpc SignIn(SignInRequest) returns (SingInResponse); + rpc SignUp(SignUpRequest) returns (SignUpResponse); } diff --git a/Plugins/Alice-Database/src/instance.rs b/Plugins/Alice-Database/src/instance.rs index 3f9f7a7..c085c1d 100644 --- a/Plugins/Alice-Database/src/instance.rs +++ b/Plugins/Alice-Database/src/instance.rs @@ -2,6 +2,10 @@ use crate::Engines; use uuid::Uuid; use std::path::PathBuf; use crate::JSONEngine; +use std::collections::HashMap; +use ring::{rand::{SecureRandom, SystemRandom}, hmac}; +use rand::rngs::OsRng; + #[derive(Debug, Clone)] pub struct Instance { @@ -12,22 +16,26 @@ pub struct Instance { #[derive(Debug, Clone, Default)] pub struct InstanceManager { pub name: String, - pub instances: Vec + pub instances: Vec, + pub root_path: PathBuf, + pub authenticated_apps: HashMap, } impl InstanceManager { - pub fn new() -> Self { + pub fn new(root_path: &PathBuf) -> Self { let name = Uuid::new_v4().to_string(); + let mut instances: Vec = vec![]; - Self {name, instances} + let mut authenticated_apps: HashMap = HashMap::new(); + Self {name, instances, root_path: root_path.to_owned(), authenticated_apps} } - pub fn create_instance(&mut self, engine_type: &str, root_path: &PathBuf) -> String { + pub fn create_instance(&mut self, engine_type: &str) -> String { let instance_name: String = Uuid::new_v4().to_string(); let mut engine = match engine_type { - "json_engine" => Engines::JSONEngine(JSONEngine::new(&root_path)), + "json_engine" => Engines::JSONEngine(JSONEngine::new(&self.root_path)), _ => panic!("Engine not found"), }; let mut instance = Instance {engine, name: instance_name.clone()}; @@ -49,4 +57,14 @@ impl InstanceManager { } None } + + pub fn sign_up(&mut self, app_name: String) -> String { + let key = Uuid::new_v4().to_string(); + &self.authenticated_apps.insert(app_name, key.clone()); + return key; + } + + pub fn get_all_apps(&self) { + println!("{:#?}", self.authenticated_apps); + } } \ No newline at end of file diff --git a/Plugins/Alice-Database/src/json_engine.rs b/Plugins/Alice-Database/src/json_engine.rs index 165f551..279d628 100644 --- a/Plugins/Alice-Database/src/json_engine.rs +++ b/Plugins/Alice-Database/src/json_engine.rs @@ -106,6 +106,7 @@ impl JSONEngine { JSONEngine { collections } } + /// Retrieve a mutable reference to a collection by its name. /// /// # Parameters diff --git a/Plugins/Alice-Database/src/main.rs b/Plugins/Alice-Database/src/main.rs index e2148b1..c3cc98b 100644 --- a/Plugins/Alice-Database/src/main.rs +++ b/Plugins/Alice-Database/src/main.rs @@ -24,8 +24,15 @@ use std::sync::{Arc, Mutex}; pub mod instance_g { tonic::include_proto!("instance"); } -pub use instance_g::{CreateInstanceRequest, CreateInstanceResponse, - GetInstanceRequest, GetInstanceResponse, GetAllInstancesRequest, GetAllInstancesResponse}; +pub use instance_g::{ + CreateInstanceRequest, CreateInstanceResponse, + GetInstanceRequest, GetInstanceResponse, + GetAllInstancesRequest, GetAllInstancesResponse, + //SignInRequest, SignInResponse, + SignUpRequest, SignUpResponse, + Token, + +}; use crate::instance_g::instance_service_server::InstanceServiceServer; use crate::instance_g::instance_service_server::InstanceService; @@ -102,10 +109,9 @@ impl InstanceService for MyInstanceManager { request: Request, ) -> Result, Status> { let inner = request.into_inner(); - let engine_type = inner.engine_type; - let root_path = PathBuf::from(inner.root_path); // assuming root_path is a string path + let engine_type = inner.engine_type; // assuming root_path is a string path let mut manager = self.instances.lock().unwrap(); - let id = manager.create_instance(&engine_type,&root_path); + let id = manager.create_instance(&engine_type); let response = CreateInstanceResponse { instance: id }; Ok(Response::new(response)) @@ -142,20 +148,43 @@ impl InstanceService for MyInstanceManager { Ok(Response::new(response)) } + + async fn sign_up( + &self, + request: Request, + ) -> Result, Status> { + let inner = request.into_inner(); + let mut manager = self.instances.lock().unwrap(); + let mut key: String = String::new(); + if !manager.authenticated_apps.contains_key(&inner.app_name) { + key = manager.sign_up(inner.app_name); + } else { + key = "whoops...".to_string(); + } + let response = SignUpResponse { secret_key: key }; + + manager.get_all_apps(); + Ok(Response::new(response)) + } } // Main function to start the gRPC server #[tokio::main] async fn main() -> Result<(), Box> { + print_ascii(); + let root_path = match prepare() { + Ok(k) => k, + _ => panic!("Errors in prepare function.") + }; let instance_manager = MyInstanceManager { - instances: Arc::new(Mutex::new(InstanceManager::new())), + instances: Arc::new(Mutex::new(InstanceManager::new(&root_path))), }; println!("Starting gRPC server..."); Server::builder() .add_service(InstanceServiceServer::new(instance_manager)) - .serve("[::1]:50051".parse()?) + .serve("[::1]:50052".parse()?) .await?; Ok(())