121 lines
No EOL
5.9 KiB
Rust
121 lines
No EOL
5.9 KiB
Rust
// trifid-api, 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 rocket::{get, post, options, State};
|
|
use rocket::http::{ContentType, Status};
|
|
use rocket::serde::json::Json;
|
|
use serde::{Serialize, Deserialize};
|
|
use sqlx::PgPool;
|
|
use crate::auth::TOTPAuthenticatedUserInfo;
|
|
use crate::config::TFConfig;
|
|
use crate::crypto::{encrypt_with_nonce, generate_random_iv, get_cipher_from_config};
|
|
use crate::org::{get_associated_orgs, get_org_by_owner_id, get_users_by_assoc_org, user_has_org_assoc};
|
|
|
|
#[options("/v1/orgs")]
|
|
pub fn options() -> &'static str {
|
|
""
|
|
}
|
|
#[options("/v1/org/<_id>")]
|
|
pub fn orgidoptions(_id: i32) -> &'static str {
|
|
""
|
|
}
|
|
#[options("/v1/org")]
|
|
pub fn createorgoptions() -> &'static str {""}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
#[serde(crate = "rocket::serde")]
|
|
pub struct OrglistStruct {
|
|
pub org_ids: Vec<i32>
|
|
}
|
|
|
|
|
|
#[get("/v1/orgs")]
|
|
pub async fn orglist_req(user: TOTPAuthenticatedUserInfo, db: &State<PgPool>) -> Result<(ContentType, Json<OrglistStruct>), (Status, String)> {
|
|
// this endpoint lists the associated organizations this user has access to
|
|
let associated_orgs = match get_associated_orgs(user.user_id, db.inner()).await {
|
|
Ok(r) => r,
|
|
Err(e) => return Err((Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_DB_QUERY_FAILED", "an error occurred while running the database query", e)))
|
|
};
|
|
|
|
Ok((ContentType::JSON, Json(OrglistStruct {
|
|
org_ids: associated_orgs
|
|
})))
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
#[serde(crate = "rocket::serde")]
|
|
pub struct OrginfoStruct {
|
|
pub org_id: i32,
|
|
pub owner_id: i32,
|
|
pub ca_crt: String,
|
|
pub authorized_users: Vec<i32>
|
|
}
|
|
|
|
#[get("/v1/org/<orgid>")]
|
|
pub async fn orginfo_req(orgid: i32, user: TOTPAuthenticatedUserInfo, db: &State<PgPool>) -> Result<(ContentType, Json<OrginfoStruct>), (Status, String)> {
|
|
if !user_has_org_assoc(orgid, user.user_id, db.inner()).await.map_err(|e| (Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_QL_QUERY_FAILED", "an error occurred while running the graphql query", e)))? {
|
|
return Err((Status::Unauthorized, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{}\"}}]}}", "ERR_MISSING_ORG_AUTHORIZATION", "this user does not have permission to access this org")));
|
|
}
|
|
|
|
let org = sqlx::query!("SELECT id, owner, ca_crt FROM organizations WHERE id = $1", orgid).fetch_one(db.inner()).await.map_err(|e| (Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_QL_QUERY_FAILED", "an error occurred while running the graphql query", e)))?;
|
|
let authorized_users = get_users_by_assoc_org(orgid, db.inner()).await.map_err(|e| (Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_QL_QUERY_FAILED", "an error occurred while running the graphql query", e)))?;
|
|
|
|
Ok((ContentType::JSON, Json(
|
|
OrginfoStruct {
|
|
org_id: orgid,
|
|
owner_id: org.owner,
|
|
ca_crt: org.ca_crt,
|
|
authorized_users,
|
|
}
|
|
)))
|
|
}
|
|
|
|
#[post("/v1/org")]
|
|
pub async fn create_org(user: TOTPAuthenticatedUserInfo, db: &State<PgPool>, config: &State<TFConfig>) -> Result<(ContentType, Json<OrginfoStruct>), (Status, String)> {
|
|
if get_org_by_owner_id(user.user_id, db.inner()).await.map_err(|e| (Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_QL_QUERY_FAILED", "an error occurred while running the graphql query", e)))?.is_some() {
|
|
return Err((Status::Conflict, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{}\"}}]}}", "ERR_USER_OWNS_ORG", "a user can only own one organization at a time")))
|
|
}
|
|
|
|
// generate an AES iv to use for key encryption
|
|
let iv = generate_random_iv();
|
|
let iv_hex = hex::encode(iv);
|
|
|
|
let owner_id = user.user_id;
|
|
let cipher = match get_cipher_from_config(config) {
|
|
Ok(c) => c,
|
|
Err(e) => {
|
|
return Err((Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_CRYPTOGRAPHY_CREATE_CIPHER", "Unable to build cipher construct, please try again later", e)));
|
|
}
|
|
};
|
|
let ca_key = match encrypt_with_nonce(b"", iv, &cipher) {
|
|
Ok(key) => hex::encode(key),
|
|
Err(e) => {
|
|
return Err((Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_CRYPTOGRAPHY_ENCRYPT_KEY", "Unable to build cipher construct, please try again later", e)));
|
|
}
|
|
};
|
|
let ca_crt = hex::encode(b"");
|
|
|
|
let result = sqlx::query!("INSERT INTO organizations (owner, ca_key, ca_crt, iv) VALUES ($1, $2, $3, $4) RETURNING id", owner_id, ca_key, ca_crt, iv_hex).fetch_one(db.inner()).await.map_err(|e| (Status::InternalServerError, format!("{{\"errors\":[{{\"code\":\"{}\",\"message\":\"{} - {}\"}}]}}", "ERR_QL_QUERY_FAILED", "an error occurred while running the graphql query", e)))?;
|
|
|
|
Ok((ContentType::JSON, Json(
|
|
OrginfoStruct {
|
|
org_id: result.id,
|
|
owner_id,
|
|
ca_crt,
|
|
authorized_users: vec![owner_id],
|
|
}
|
|
)))
|
|
} |