use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::b64::B64;
use super::tls::platform;
use crate::error::{Validate, ValidationError, check_known};
use crate::http::Page;
use crate::id::Id;
use crate::time::Timestamp;
pub const DEVICE_PUBKEY_BYTES: usize = 65;
pub const P256_SIG_BYTES: usize = 64;
pub const DEVICE_LABEL_MAX_BYTES: usize = 64;
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum DevicePlatform {
Web = "web",
Desktop = "desktop",
Ios = "ios",
Android = "android",
Other = "other",
}
}
impl DevicePlatform {
pub fn code(self) -> Option<u8> {
platform::code(self.as_str())
}
pub fn from_code(code: u8) -> Option<DevicePlatform> {
platform::name(code).and_then(DevicePlatform::from_wire)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DeviceEnrollRequest {
pub pubkey: B64,
pub platform: DevicePlatform,
pub label: String,
pub proof: B64,
}
impl Validate for DeviceEnrollRequest {
fn validate(&self) -> Result<(), ValidationError> {
if self.pubkey.len() != DEVICE_PUBKEY_BYTES {
return Err(ValidationError::invalid(
"pubkey",
"pubkey must be a 65-byte SEC1 uncompressed P-256 point",
));
}
check_known("platform", self.platform)?;
if self.label.len() > DEVICE_LABEL_MAX_BYTES {
return Err(ValidationError::invalid(
"label",
format!("label must be at most {DEVICE_LABEL_MAX_BYTES} bytes"),
));
}
if self.proof.len() != P256_SIG_BYTES {
return Err(ValidationError::invalid(
"proof",
"proof must be a 64-byte raw r||s signature",
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeviceCertificateResponse {
pub device_id: Id,
pub serial: Id,
pub cert: B64,
pub not_after: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeviceSummary {
pub device_id: Id,
pub platform: DevicePlatform,
pub label: String,
pub created_at: Timestamp,
pub serial: Id,
pub not_after: Timestamp,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub revoked_at: Option<Timestamp>,
}
pub type DevicePage = Page<DeviceSummary>;
coded_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum DeviceRevokeReason {
Lost = "lost" => 1,
Retired = "retired" => 2,
Compromised = "compromised" => 3,
Other = "other" => 4,
LoginRevoked = "login_revoked" => 5,
Deprovisioned = "deprovisioned" => 6,
}
}
impl DeviceRevokeReason {
pub const fn caller_may_send(self) -> bool {
matches!(
self,
DeviceRevokeReason::Lost
| DeviceRevokeReason::Retired
| DeviceRevokeReason::Compromised
| DeviceRevokeReason::Other
)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DeviceRevokeRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<DeviceRevokeReason>,
}
impl Validate for DeviceRevokeRequest {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(r) = self.reason {
check_known("reason", r)?;
if !r.caller_may_send() {
return Err(ValidationError::invalid(
"reason",
"reason must be lost, retired, compromised or other",
));
}
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeviceRevokeResponse {
pub ok: bool,
pub serials: Vec<Id>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct CrlPage {
pub items: Vec<Id>,
pub has_more: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct CrlQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
pub const CRL_PAGE_MAX: u32 = 5_000;
impl Validate for CrlQuery {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(l) = self.limit
&& (l == 0 || l > CRL_PAGE_MAX)
{
return Err(ValidationError::invalid(
"limit",
format!("limit must be 1–{CRL_PAGE_MAX}"),
));
}
Ok(())
}
}