diff --git a/.idea/trifid.iml b/.idea/trifid.iml
index 1324adf..bef05f9 100644
--- a/.idea/trifid.iml
+++ b/.idea/trifid.iml
@@ -4,6 +4,7 @@
+
diff --git a/Cargo.lock b/Cargo.lock
index 93025dc..4d5d078 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2205,11 +2205,14 @@ dependencies = [
name = "trifid-api"
version = "0.1.0"
dependencies = [
+ "aes-gcm",
"base64 0.21.0",
"chrono",
"dotenvy",
+ "hex",
"log",
"paste",
+ "rand",
"rocket",
"serde",
"sqlx",
@@ -2221,6 +2224,10 @@ dependencies = [
"uuid",
]
+[[package]]
+name = "trifid-pki"
+version = "0.1.0"
+
[[package]]
name = "try-lock"
version = "0.2.4"
diff --git a/Cargo.toml b/Cargo.toml
index 54ab1ef..1ad3cfa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,6 @@
[workspace]
members = [
"trifid-api",
- "tfclient"
+ "tfclient",
+ "trifid-pki"
]
\ No newline at end of file
diff --git a/trifid-api/.cargo/config.toml b/trifid-api/.cargo/config.toml
new file mode 100644
index 0000000..a7194eb
--- /dev/null
+++ b/trifid-api/.cargo/config.toml
@@ -0,0 +1,2 @@
+[build]
+rustc-wrapper = "sccache"
\ No newline at end of file
diff --git a/trifid-api/Cargo.toml b/trifid-api/Cargo.toml
index 9f9ac6e..84d8cb0 100644
--- a/trifid-api/Cargo.toml
+++ b/trifid-api/Cargo.toml
@@ -19,4 +19,7 @@ totp-rs = { version = "4.2.0", features = ["qr", "otpauth", "gen_secret"]}
uuid = { version = "1.3.0", features = ["v4", "fast-rng", "macro-diagnostics"]}
url = { version = "2.3.1", features = ["serde"] }
urlencoding = "2.1.2"
-chrono = "0.4.23"
\ No newline at end of file
+chrono = "0.4.23"
+aes-gcm = "0.10.1"
+hex = "0.4.3"
+rand = "0.8.5"
\ No newline at end of file
diff --git a/trifid-api/migrations/20230226020713_create_orgs_authorized_users.sql b/trifid-api/migrations/20230226020713_create_orgs_authorized_users.sql
new file mode 100644
index 0000000..cf1b794
--- /dev/null
+++ b/trifid-api/migrations/20230226020713_create_orgs_authorized_users.sql
@@ -0,0 +1,7 @@
+CREATE TABLE organization_authorized_users (
+ id SERIAL NOT NULL PRIMARY KEY,
+ user_id SERIAL NOT NULL REFERENCES users(id),
+ org_id SERIAL NOT NULL REFERENCES organizations(id)
+);
+CREATE INDEX idx_organization_authorized_users_user ON organization_authorized_users(user_id);
+CREATE INDEX idx_organization_authorized_users_org ON organization_authorized_users(org_id);
\ No newline at end of file
diff --git a/trifid-api/src/crypto.rs b/trifid-api/src/crypto.rs
new file mode 100644
index 0000000..193ddf0
--- /dev/null
+++ b/trifid-api/src/crypto.rs
@@ -0,0 +1,21 @@
+use std::error::Error;
+use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
+use aes_gcm::aead::{Aead, Payload};
+use crate::config::TFConfig;
+
+pub fn get_cipher_from_config(config: &TFConfig) -> Result> {
+ let key_slice = hex::decode(&config.data_key)?;
+ Ok(Aes256Gcm::new_from_slice(&key_slice)?)
+}
+
+pub fn encrypt_with_nonce(plaintext: &[u8], nonce: [u8; 12], cipher: &Aes256Gcm) -> Result, aes_gcm::Error> {
+ let nonce = Nonce::from_slice(&nonce);
+ let ciphertext = cipher.encrypt(nonce, plaintext)?;
+ Ok(ciphertext)
+}
+
+pub fn decrypt_with_nonce(ciphertext: &[u8], nonce: [u8; 12], cipher: &Aes256Gcm) -> Result, aes_gcm::Error> {
+ let nonce = Nonce::from_slice(&nonce);
+ let plaintext = cipher.decrypt(nonce, Payload::from(ciphertext))?;
+ Ok(plaintext)
+}
\ No newline at end of file
diff --git a/trifid-api/src/main.rs b/trifid-api/src/main.rs
index 2d7fa80..e7f143d 100644
--- a/trifid-api/src/main.rs
+++ b/trifid-api/src/main.rs
@@ -19,6 +19,8 @@ pub mod config;
pub mod tokens;
pub mod routes;
pub mod auth;
+pub mod crypto;
+pub mod org;
static MIGRATOR: Migrator = sqlx::migrate!();
@@ -118,7 +120,13 @@ async fn main() -> Result<(), Box> {
crate::routes::v1::auth::check_session::options,
crate::routes::v1::auth::check_session::options_auth,
crate::routes::v2::whoami::whoami_request,
- crate::routes::v2::whoami::options
+ crate::routes::v2::whoami::options,
+
+ crate::routes::v1::organization::options,
+ crate::routes::v1::organization::orgidoptions,
+ crate::routes::v1::organization::orginfo_req,
+ crate::routes::v1::organization::orglist_req,
+ crate::routes::v1::organization::create_org
])
.register("/", catchers![
crate::routes::handler_400,
diff --git a/trifid-api/src/org.rs b/trifid-api/src/org.rs
new file mode 100644
index 0000000..de1dcb4
--- /dev/null
+++ b/trifid-api/src/org.rs
@@ -0,0 +1,50 @@
+use std::error::Error;
+use rocket::form::validate::Contains;
+use sqlx::PgPool;
+
+pub async fn get_org_by_owner_id(user: i32, db: &PgPool) -> Result