some signup work on new api
/ build (push) Successful in 48s Details
/ build_x64 (push) Successful in 2m12s Details
/ build_arm64 (push) Successful in 2m35s Details
/ build_win64 (push) Successful in 2m35s Details

This commit is contained in:
core 2023-11-19 10:49:08 -05:00
parent 53c6fb18fd
commit 79b1765ed5
Signed by: core
GPG Key ID: FDBF740DADDCEECF
14 changed files with 329 additions and 75 deletions

1
Cargo.lock generated
View File

@ -2606,6 +2606,7 @@ dependencies = [
"diesel_migrations", "diesel_migrations",
"env_logger", "env_logger",
"log", "log",
"rand",
"serde", "serde",
"serde_json", "serde_json",
"toml 0.8.5", "toml 0.8.5",

View File

@ -82,8 +82,8 @@ const config = {
to: '/docs/intro', to: '/docs/intro',
}, },
{ {
label: 'trifid-api-old', label: 'trifid-api',
to: '/docs/trifid-api-old/intro', to: '/docs/trifid-api/intro',
}, },
{ {
label: 'tfweb', label: 'tfweb',

44
trifid-api-old/Cargo.toml Normal file
View File

@ -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

View File

@ -0,0 +1,5 @@
[print_schema]
file = "src/schema.rs"
[migrations_directory]
dir = "migrations"

126
trifid-api-old/src/main.rs Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
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<dyn Error>> {
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(())
}

View File

@ -23,3 +23,4 @@ diesel = { version = "2" }
diesel-async = { version = "0.4", features = ["postgres", "bb8", "async-connection-wrapper"] } diesel-async = { version = "0.4", features = ["postgres", "bb8", "async-connection-wrapper"] }
diesel_migrations = "2" diesel_migrations = "2"
bb8 = "0.8" bb8 = "0.8"
rand = "0.8"

View File

@ -1,4 +1,4 @@
CREATE TABLE users ( CREATE TABLE users (
id VARCHAR NOT NULL PRIMARY KEY, id VARCHAR NOT NULL PRIMARY KEY,
email VARCHAR NOT NULL PRIMARY KEY email VARCHAR NOT NULL UNIQUE
); );

31
trifid-api/src/id.rs Normal file
View File

@ -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()
}

View File

@ -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
}
};
}

View File

@ -14,10 +14,15 @@ use crate::config::Config;
use crate::error::APIErrorResponse; use crate::error::APIErrorResponse;
pub mod error; pub mod error;
#[macro_use]
pub mod response; pub mod response;
pub mod macros;
pub mod config; pub mod config;
pub mod routes; pub mod routes;
pub mod schema;
pub mod models;
#[macro_use]
pub mod id;
#[derive(Clone)] #[derive(Clone)]
pub struct AppState { pub struct AppState {
@ -73,8 +78,11 @@ async fn main() {
info!("Running pending migrations..."); info!("Running pending migrations...");
{ // Lock block let local_config = config.clone();
let mut conn = match AsyncConnectionWrapper::<AsyncPgConnection>::establish(&config.database.url) { let db_url = config.database.url.clone();
match actix_web::rt::task::spawn_blocking(move || { // Lock block
let mut conn = match AsyncConnectionWrapper::<AsyncPgConnection>::establish(&db_url) {
Ok(conn) => conn, Ok(conn) => conn,
Err(e) => { Err(e) => {
error!("Error acquiring connection from pool: {}", e); error!("Error acquiring connection from pool: {}", e);
@ -85,12 +93,17 @@ async fn main() {
match conn.run_pending_migrations(MIGRATIONS) { match conn.run_pending_migrations(MIGRATIONS) {
Ok(_) => (), Ok(_) => (),
Err(e) => { 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 { let app_state = Data::new(AppState {
config, config,
@ -108,6 +121,7 @@ async fn main() {
) )
}) })
})) }))
.service(routes::v1::signup::signup_req)
.wrap(Logger::default()) .wrap(Logger::default())
.wrap(actix_cors::Cors::permissive()) .wrap(actix_cors::Cors::permissive())
.app_data(app_state.clone()) .app_data(app_state.clone())

9
trifid-api/src/models.rs Normal file
View File

@ -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
}

View File

@ -38,3 +38,67 @@ impl<T: Serialize + Debug> Responder for JsonAPIResponse<T> {
} }
} }
} }
#[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
}
};
}

View File

@ -1,7 +1,11 @@
use actix_web::post;
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::AppState; use crate::{AppState, randid};
use crate::models::User;
use crate::response::JsonAPIResponse; use crate::response::JsonAPIResponse;
use crate::schema::users;
use diesel_async::RunQueryDsl;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct SignupRequest { pub struct SignupRequest {
@ -16,6 +20,16 @@ pub struct SignupResponse {
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct SignupResponseMetadata {} pub struct SignupResponseMetadata {}
#[post("/v1/signup")]
pub async fn signup_req(req: Json<SignupRequest>, state: Data<AppState>) -> JsonAPIResponse<SignupResponse> { pub async fn signup_req(req: Json<SignupRequest>, state: Data<AppState>) -> JsonAPIResponse<SignupResponse> {
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 {} })
} }

8
trifid-api/src/schema.rs Normal file
View File

@ -0,0 +1,8 @@
// @generated automatically by Diesel CLI.
diesel::table! {
users (id) {
id -> Varchar,
email -> Varchar,
}
}