code cleanup

This commit is contained in:
c0repwn3r 2023-05-11 13:35:10 -04:00
parent 68c120a5ab
commit b3e83f4586
Signed by: core
GPG Key ID: FDBF740DADDCEECF
3 changed files with 37 additions and 20 deletions

View File

@ -82,7 +82,7 @@ pub struct TrifidConfigTokens {
#[serde(default = "mfa_tokens_expiry_time")]
pub mfa_tokens_expiry_time_seconds: u64,
#[serde(default = "enrollment_tokens_expiry_time")]
pub enrollment_tokens_expiry_time: u64
pub enrollment_tokens_expiry_time: u64,
}
#[derive(Serialize, Deserialize, Debug)]

View File

@ -56,6 +56,7 @@
// This endpoint requires the `definednetworking` extension to be enabled to be used.
use crate::auth_tokens::{enforce_2fa, enforce_api_token, TokenInfo};
use crate::config::CONFIG;
use crate::cursor::Cursor;
use crate::error::{APIError, APIErrorsResponse};
use crate::routes::v1::trifid::SUPPORTED_EXTENSIONS;
@ -76,7 +77,6 @@ use std::net::{Ipv4Addr, SocketAddrV4};
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use trifid_api_entities::entity::{host, host_static_address, network, organization};
use crate::config::CONFIG;
#[derive(Serialize, Deserialize)]
pub struct ListHostsRequestOpts {
@ -1801,26 +1801,30 @@ pub async fn block_host(
pub struct CodeResponse {
pub code: String,
#[serde(rename = "lifetimeSeconds")]
pub lifetime_seconds: u64
pub lifetime_seconds: u64,
}
#[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponse {
pub data: EnrollmentCodeResponseData,
pub metadata: EnrollmentCodeResponseMetadata
pub metadata: EnrollmentCodeResponseMetadata,
}
#[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponseData {
#[serde(rename = "enrollmentCode")]
pub enrollment_code: CodeResponse
pub enrollment_code: CodeResponse,
}
#[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponseMetadata {}
#[post("/v1/hosts/{host_id}/enrollment-code")]
pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppState>) -> HttpResponse {
pub async fn enroll_host(
id: Path<String>,
req_info: HttpRequest,
db: Data<AppState>,
) -> HttpResponse {
let session_info = enforce_2fa(&req_info, &db.conn)
.await
.unwrap_or(TokenInfo::NotPresent);
@ -1964,8 +1968,8 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
errors: vec![APIError {
code: "ERR_UNAUTHORIZED".to_string(),
message:
"This resource does not exist or you do not have permission to access it."
.to_string(),
"This resource does not exist or you do not have permission to access it."
.to_string(),
path: None,
}],
})
@ -2006,7 +2010,12 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
};
HttpResponse::Ok().json(EnrollmentCodeResponse {
data: EnrollmentCodeResponseData { enrollment_code: CodeResponse { code: code.id, lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time } },
data: EnrollmentCodeResponseData {
enrollment_code: CodeResponse {
code: code.id,
lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time,
},
},
metadata: EnrollmentCodeResponseMetadata {},
})
}
@ -2014,20 +2023,19 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
#[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponse {
pub data: CreateHostAndCodeResponseData,
pub metadata: CreateHostAndCodeResponseMetadata
pub metadata: CreateHostAndCodeResponseMetadata,
}
#[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponseData {
pub host: HostResponse,
#[serde(rename = "enrollmentCode")]
pub enrollment_code: CodeResponse
pub enrollment_code: CodeResponse,
}
#[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponseMetadata {}
#[post("/v1/host-and-enrollment-code")]
pub async fn create_host_and_enrollment_code(
req: Json<CreateHostRequest>,
@ -2307,7 +2315,7 @@ pub async fn create_host_and_enrollment_code(
enrollment_code: CodeResponse {
code: code.id,
lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time,
}
},
},
metadata: CreateHostAndCodeResponseMetadata {},
})

View File

@ -1,5 +1,5 @@
use sea_orm_migration::prelude::*;
use crate::m20230427_170037_create_table_hosts::Host;
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
@ -11,15 +11,24 @@ impl MigrationTrait for Migration {
.create_table(
Table::create()
.table(HostEnrollmentCode::Table)
.col(ColumnDef::new(HostEnrollmentCode::Id).string().not_null().primary_key())
.col(
ColumnDef::new(HostEnrollmentCode::Id)
.string()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(HostEnrollmentCode::Host).string().not_null())
.col(ColumnDef::new(HostEnrollmentCode::ExpiresOn).big_integer().not_null())
.col(
ColumnDef::new(HostEnrollmentCode::ExpiresOn)
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.from(HostEnrollmentCode::Table, HostEnrollmentCode::Host)
.to(Host::Table, Host::Id)
.on_update(ForeignKeyAction::Cascade)
.on_delete(ForeignKeyAction::Cascade)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
@ -39,5 +48,5 @@ pub enum HostEnrollmentCode {
Table,
Id,
Host,
ExpiresOn
ExpiresOn,
}