From 79b1765ed57d6f01a4028462e45c689374dc1802 Mon Sep 17 00:00:00 2001 From: core Date: Sun, 19 Nov 2023 10:49:08 -0500 Subject: [PATCH] some signup work on new api --- Cargo.lock | 1 + docs/docusaurus.config.js | 4 +- trifid-api-old/Cargo.toml | 44 ++++++ trifid-api-old/diesel.toml | 5 + trifid-api-old/src/main.rs | 126 ++++++++++++++++++ trifid-api/Cargo.toml | 3 +- .../2023-11-19-033954_create_users/up.sql | 2 +- trifid-api/src/id.rs | 31 +++++ trifid-api/src/macros.rs | 63 --------- trifid-api/src/main.rs | 26 +++- trifid-api/src/models.rs | 9 ++ trifid-api/src/response.rs | 64 +++++++++ trifid-api/src/routes/v1/signup.rs | 18 ++- trifid-api/src/schema.rs | 8 ++ 14 files changed, 329 insertions(+), 75 deletions(-) create mode 100644 trifid-api-old/Cargo.toml create mode 100644 trifid-api-old/diesel.toml create mode 100644 trifid-api-old/src/main.rs create mode 100644 trifid-api/src/id.rs delete mode 100644 trifid-api/src/macros.rs create mode 100644 trifid-api/src/models.rs create mode 100644 trifid-api/src/schema.rs diff --git a/Cargo.lock b/Cargo.lock index 7d4eb36..b32c37c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2606,6 +2606,7 @@ dependencies = [ "diesel_migrations", "env_logger", "log", + "rand", "serde", "serde_json", "toml 0.8.5", diff --git a/docs/docusaurus.config.js b/docs/docusaurus.config.js index 7a30a64..9cbb918 100644 --- a/docs/docusaurus.config.js +++ b/docs/docusaurus.config.js @@ -82,8 +82,8 @@ const config = { to: '/docs/intro', }, { - label: 'trifid-api-old', - to: '/docs/trifid-api-old/intro', + label: 'trifid-api', + to: '/docs/trifid-api/intro', }, { label: 'tfweb', diff --git a/trifid-api-old/Cargo.toml b/trifid-api-old/Cargo.toml new file mode 100644 index 0000000..eae1626 --- /dev/null +++ b/trifid-api-old/Cargo.toml @@ -0,0 +1,44 @@ +[package] +name = "trifid-api" +version = "0.2.3" +edition = "2021" +description = "Pure-rust Defined Networking compatible management server" +license = "GPL-3.0-or-later" +documentation = "https://git.e3t.cc/~core/trifid" +homepage = "https://git.e3t.cc/~core/trifid" +repository = "https://git.e3t.cc/~core/trifid" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +actix-web = "4" # Web framework +actix-request-identifier = "4" # Web framework +actix-cors = "0.6.4" # Web framework + +serde = { version = "1", features = ["derive"] } # Serialization and deserialization +serde_json = "1.0.95" # Serialization and deserialization (cursors) + +once_cell = "1" # Config +toml = "0.8" # Config / Serialization and deserialization +serde_yaml = "0.9.21" # Config / Serialization and deserialization + +log = "0.4" # Logging +simple_logger = "4" # Logging + +sea-orm = { version = "0.12", features = [ "sqlx-postgres", "runtime-actix-rustls", "macros" ]} # Database +trifid_api_migration = { version = "0.2", path = "trifid_api_migration" } # Database +trifid_api_entities = { version = "0.2", path = "trifid_api_entities" } # Database + +rand = "0.8" # Misc. +hex = "0.4" # Misc. +totp-rs = { version = "5.0.1", features = ["gen_secret", "otpauth"] } # Misc. +base64 = "0.21.0" # Misc. +chrono = "0.4.24" # Misc. +derivative = "2.2.0" # Misc. + +trifid-pki = { version = "0.1", features = ["serde_derive"] } # Cryptography +aes-gcm = "0.10.1" # Cryptography +ed25519-dalek = "2.0.0-rc.2" # Cryptography + +dnapi-rs = { version = "0.2", path = "../dnapi-rs" } # API message types +ipnet = "2.7.2" # API message types diff --git a/trifid-api-old/diesel.toml b/trifid-api-old/diesel.toml new file mode 100644 index 0000000..4ae5e4b --- /dev/null +++ b/trifid-api-old/diesel.toml @@ -0,0 +1,5 @@ +[print_schema] +file = "src/schema.rs" + +[migrations_directory] +dir = "migrations" \ No newline at end of file diff --git a/trifid-api-old/src/main.rs b/trifid-api-old/src/main.rs new file mode 100644 index 0000000..e99cdc4 --- /dev/null +++ b/trifid-api-old/src/main.rs @@ -0,0 +1,126 @@ +// trifid-api-old, an open source reimplementation of the Defined Networking nebula management server. +// Copyright (C) 2023 c0repwn3r +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use actix_request_identifier::RequestIdentifier; +use actix_web::{ + web::{Data, JsonConfig}, + App, HttpResponse, HttpServer, +}; +use log::{info, Level}; +use sea_orm::{ConnectOptions, Database, DatabaseConnection}; +use std::error::Error; +use std::time::Duration; +use actix_cors::Cors; +use crate::config::CONFIG; +use crate::error::{APIError, APIErrorsResponse}; +use crate::tokens::random_id_no_id; +use trifid_api_migration::{Migrator, MigratorTrait}; + +pub mod auth_tokens; +pub mod codegen; +pub mod config; +pub mod crypto; +pub mod cursor; +pub mod error; +//pub mod legacy_keystore; // TODO- Remove +pub mod magic_link; +pub mod routes; +pub mod timers; +pub mod tokens; +pub mod response; + +pub struct AppState { + pub conn: DatabaseConnection, +} + +#[actix_web::main] +async fn main() -> Result<(), Box> { + simple_logger::init_with_level(Level::Debug).unwrap(); + + info!("Connecting to database at {}...", CONFIG.database.url); + + let mut opt = ConnectOptions::new(CONFIG.database.url.clone()); + opt.max_connections(CONFIG.database.max_connections) + .min_connections(CONFIG.database.min_connections) + .connect_timeout(Duration::from_secs(CONFIG.database.connect_timeout)) + .acquire_timeout(Duration::from_secs(CONFIG.database.acquire_timeout)) + .idle_timeout(Duration::from_secs(CONFIG.database.idle_timeout)) + .max_lifetime(Duration::from_secs(CONFIG.database.max_lifetime)) + .sqlx_logging(CONFIG.database.sqlx_logging) + .sqlx_logging_level(log::LevelFilter::Info); + + let db = Database::connect(opt).await?; + + info!("Performing database migration..."); + Migrator::up(&db, None).await?; + + let data = Data::new(AppState { conn: db }); + + HttpServer::new(move || { + App::new() + .wrap(Cors::permissive()) + .app_data(data.clone()) + .app_data( + JsonConfig::default() + .content_type_required(false) + .error_handler(|err, _req| { + let api_error: APIError = (&err).into(); + actix_web::error::InternalError::from_response( + err, + HttpResponse::BadRequest().json(APIErrorsResponse { + errors: vec![api_error], + }), + ) + .into() + }), + ) + .wrap(RequestIdentifier::with_generator(random_id_no_id)) + .service(routes::v1::auth::magic_link::magic_link_request) + .service(routes::v1::signup::signup_request) + .service(routes::v1::auth::verify_magic_link::verify_magic_link_request) + .service(routes::v1::totp_authenticators::totp_authenticators_request) + .service(routes::v1::verify_totp_authenticators::verify_totp_authenticators_request) + .service(routes::v1::auth::totp::totp_request) + .service(routes::v1::networks::get_networks) + .service(routes::v1::organization::create_org_request) + .service(routes::v1::networks::get_network_request) + .service(routes::v1::roles::create_role_request) + .service(routes::v1::roles::get_roles) + .service(routes::v1::roles::get_role) + .service(routes::v1::roles::delete_role) + .service(routes::v1::roles::update_role_request) + .service(routes::v1::trifid::trifid_extensions) + .service(routes::v1::hosts::get_hosts) + .service(routes::v1::hosts::create_hosts_request) + .service(routes::v1::hosts::get_host) + .service(routes::v1::hosts::delete_host) + .service(routes::v1::hosts::edit_host) + .service(routes::v1::hosts::block_host) + .service(routes::v1::hosts::enroll_host) + .service(routes::v1::hosts::create_host_and_enrollment_code) + .service(routes::v2::enroll::enroll) + .service(routes::v1::dnclient::dnclient) + .service(routes::v2::whoami::whoami) + .service(routes::v1::hosts::get_host_overrides) + .service(routes::v1::hosts::update_host_overrides) + }) + .workers(CONFIG.server.workers) + .bind(CONFIG.server.bind)? + .run() + .await?; + + Ok(()) +} diff --git a/trifid-api/Cargo.toml b/trifid-api/Cargo.toml index 2b22573..5bc0019 100644 --- a/trifid-api/Cargo.toml +++ b/trifid-api/Cargo.toml @@ -22,4 +22,5 @@ env_logger = "0.10" diesel = { version = "2" } diesel-async = { version = "0.4", features = ["postgres", "bb8", "async-connection-wrapper"] } diesel_migrations = "2" -bb8 = "0.8" \ No newline at end of file +bb8 = "0.8" +rand = "0.8" \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-19-033954_create_users/up.sql b/trifid-api/migrations/2023-11-19-033954_create_users/up.sql index 65f7aa6..0741c9b 100644 --- a/trifid-api/migrations/2023-11-19-033954_create_users/up.sql +++ b/trifid-api/migrations/2023-11-19-033954_create_users/up.sql @@ -1,4 +1,4 @@ CREATE TABLE users ( id VARCHAR NOT NULL PRIMARY KEY, - email VARCHAR NOT NULL PRIMARY KEY + email VARCHAR NOT NULL UNIQUE ); \ No newline at end of file diff --git a/trifid-api/src/id.rs b/trifid-api/src/id.rs new file mode 100644 index 0000000..85280c2 --- /dev/null +++ b/trifid-api/src/id.rs @@ -0,0 +1,31 @@ +use rand::Rng; + +pub const ID_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; +pub const ID_RAND_LEN: u32 = 26; +pub const TOKEN_CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; +pub const TOKEN_RAND_LEN: u32 = 43; + +#[macro_export] +macro_rules! randid { + (id) => { + $crate::id::random_with_charset($crate::id::ID_RAND_LEN, $crate::id::ID_CHARSET) + }; + (id $p:expr) => { + format!("{}-{}", $p, $crate::id::random_with_charset($crate::id::ID_RAND_LEN, $crate::id::ID_CHARSET)) + }; + (token) => { + random_with_charset($crate::id::TOKEN_RAND_LEN, $crate::id::TOKEN_CHARSET) + }; + (token $p:expr) => { + format!("{}-{}", $p, $crate::id::random_with_charset($crate::id::TOKEN_RAND_LEN, $crate::id::TOKEN_CHARSET)) + }; +} + +pub fn random_with_charset(len: u32, charset: &[u8]) -> String { + (0..len) + .map(|_| { + let idx = rand::thread_rng().gen_range(0..charset.len()); + charset[idx] as char + }) + .collect() +} \ No newline at end of file diff --git a/trifid-api/src/macros.rs b/trifid-api/src/macros.rs deleted file mode 100644 index 5508dc1..0000000 --- a/trifid-api/src/macros.rs +++ /dev/null @@ -1,63 +0,0 @@ -#[macro_export] -macro_rules! err { - ($c:expr,$e:expr) => { - return $crate::response::JsonAPIResponse::Error($c, $e) - }; -} - -#[macro_export] -macro_rules! ok { - ($c:expr,$e:expr) => { - return $crate::response::JsonAPIResponse::Success($c, $e) - }; - ($e:expr) => { - return $crate::response::JsonAPIResponse::Success(actix_web::http::StatusCode::OK, $e) - }; -} - -#[macro_export] -macro_rules! internal_error { - ($e:expr) => {{ - log::error!("internal error: {}", $e); - $crate::err!(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR, $crate::make_err!("ERR_INTERNAL_ERROR", $e)); - }}; -} - -#[macro_export] -macro_rules! handle_error { - ($e:expr,$c:expr,$r:expr) => { - match $e { - Ok(r) => r, - Err(e) => { - log::error!("error: {}", e); - $crate::err!($c, $r) - } - } - }; - ($e:expr) => { - match $e { - Ok(r) => r, - Err(e) => { - $crate::internal_error!(e) - } - } - }; -} - -#[macro_export] -macro_rules! make_err { - ($c:expr,$m:expr,$p:expr) => { - $crate::error::APIErrorResponse { - code: $c.to_string(), - message: $m.to_string(), - path: Some($p.to_string()) - } - }; - ($c:expr,$m:expr) => { - $crate::error::APIErrorResponse { - code: $c.to_string(), - message: $m.to_string(), - path: None - } - }; -} \ No newline at end of file diff --git a/trifid-api/src/main.rs b/trifid-api/src/main.rs index 5210f4a..7e9392a 100644 --- a/trifid-api/src/main.rs +++ b/trifid-api/src/main.rs @@ -14,10 +14,15 @@ use crate::config::Config; use crate::error::APIErrorResponse; pub mod error; +#[macro_use] pub mod response; -pub mod macros; pub mod config; pub mod routes; +pub mod schema; +pub mod models; +#[macro_use] +pub mod id; + #[derive(Clone)] pub struct AppState { @@ -73,8 +78,11 @@ async fn main() { info!("Running pending migrations..."); - { // Lock block - let mut conn = match AsyncConnectionWrapper::::establish(&config.database.url) { + let local_config = config.clone(); + let db_url = config.database.url.clone(); + + match actix_web::rt::task::spawn_blocking(move || { // Lock block + let mut conn = match AsyncConnectionWrapper::::establish(&db_url) { Ok(conn) => conn, Err(e) => { error!("Error acquiring connection from pool: {}", e); @@ -85,13 +93,18 @@ async fn main() { match conn.run_pending_migrations(MIGRATIONS) { Ok(_) => (), Err(e) => { - error!("Failed to run pending migrations: {}", e) + error!("Failed to run pending migrations: {}", e); + std::process::exit(1); } } + }).await { + Ok(_) => (), + Err(e) => { + error!("Error waiting for migrations: {}", e); + std::process::exit(1); + } } - let local_config = config.clone(); - let app_state = Data::new(AppState { config, pool @@ -108,6 +121,7 @@ async fn main() { ) }) })) + .service(routes::v1::signup::signup_req) .wrap(Logger::default()) .wrap(actix_cors::Cors::permissive()) .app_data(app_state.clone()) diff --git a/trifid-api/src/models.rs b/trifid-api/src/models.rs new file mode 100644 index 0000000..f046553 --- /dev/null +++ b/trifid-api/src/models.rs @@ -0,0 +1,9 @@ +use diesel::{Insertable, Queryable, Selectable}; + +#[derive(Queryable, Selectable, Insertable)] +#[diesel(table_name = crate::schema::users)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct User { + pub id: String, + pub email: String +} \ No newline at end of file diff --git a/trifid-api/src/response.rs b/trifid-api/src/response.rs index c15bf66..f1ef24e 100644 --- a/trifid-api/src/response.rs +++ b/trifid-api/src/response.rs @@ -37,4 +37,68 @@ impl Responder for JsonAPIResponse { } } } +} + +#[macro_export] +macro_rules! err { + ($c:expr,$e:expr) => { + return $crate::response::JsonAPIResponse::Error($c, $e) + }; +} + +#[macro_export] +macro_rules! ok { + ($c:expr,$e:expr) => { + return $crate::response::JsonAPIResponse::Success($c, $e) + }; + ($e:expr) => { + return $crate::response::JsonAPIResponse::Success(actix_web::http::StatusCode::OK, $e) + }; +} + +#[macro_export] +macro_rules! internal_error { + ($e:expr) => {{ + log::error!("internal error: {}", $e); + $crate::err!(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR, $crate::make_err!("ERR_INTERNAL_ERROR", $e)); + }}; +} + +#[macro_export] +macro_rules! handle_error { + ($e:expr,$c:expr,$r:expr) => { + match $e { + Ok(r) => r, + Err(e) => { + log::error!("error: {}", e); + $crate::err!($c, $r) + } + } + }; + ($e:expr) => { + match $e { + Ok(r) => r, + Err(e) => { + $crate::internal_error!(e) + } + } + }; +} + +#[macro_export] +macro_rules! make_err { + ($c:expr,$m:expr,$p:expr) => { + $crate::error::APIErrorResponse { + code: $c.to_string(), + message: $m.to_string(), + path: Some($p.to_string()) + } + }; + ($c:expr,$m:expr) => { + $crate::error::APIErrorResponse { + code: $c.to_string(), + message: $m.to_string(), + path: None + } + }; } \ No newline at end of file diff --git a/trifid-api/src/routes/v1/signup.rs b/trifid-api/src/routes/v1/signup.rs index e2fc73c..0881d15 100644 --- a/trifid-api/src/routes/v1/signup.rs +++ b/trifid-api/src/routes/v1/signup.rs @@ -1,7 +1,11 @@ +use actix_web::post; use actix_web::web::{Data, Json}; use serde::{Deserialize, Serialize}; -use crate::AppState; +use crate::{AppState, randid}; +use crate::models::User; use crate::response::JsonAPIResponse; +use crate::schema::users; +use diesel_async::RunQueryDsl; #[derive(Deserialize)] pub struct SignupRequest { @@ -16,6 +20,16 @@ pub struct SignupResponse { #[derive(Serialize, Debug)] pub struct SignupResponseMetadata {} +#[post("/v1/signup")] pub async fn signup_req(req: Json, state: Data) -> JsonAPIResponse { - todo!() + let mut conn = handle_error!(state.pool.get().await); + + let new_user = User { + id: randid!(id "user"), + email: req.email.clone(), + }; + + handle_error!(diesel::insert_into(users::table).values(&new_user).execute(&mut conn).await); + + ok!(SignupResponse { data: None, metadata: SignupResponseMetadata {} }) } \ No newline at end of file diff --git a/trifid-api/src/schema.rs b/trifid-api/src/schema.rs new file mode 100644 index 0000000..b9be79a --- /dev/null +++ b/trifid-api/src/schema.rs @@ -0,0 +1,8 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> Varchar, + email -> Varchar, + } +}