Skip to main content

kanidm_proto/v1/
mod.rs

1//! Kanidm Version 1
2//!
3//! Items defined in this module will remain stable, or change in ways that are forward
4//! compatible with newer releases.
5
6#![allow(non_upper_case_globals)]
7
8use serde::{Deserialize, Serialize};
9use std::{collections::BTreeMap, fmt, fmt::Display};
10use utoipa::ToSchema;
11use uuid::Uuid;
12
13mod auth;
14mod message;
15mod unix;
16
17pub use self::{auth::*, message::*, unix::*};
18
19/// The type of Account in use.
20#[derive(Serialize, Deserialize, Clone, Copy, Debug, ToSchema)]
21pub enum AccountType {
22    Person,
23    ServiceAccount,
24}
25
26impl Display for AccountType {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.write_str(match self {
29            AccountType::Person => "person",
30            AccountType::ServiceAccount => "service_account",
31        })
32    }
33}
34
35/* ===== higher level types ===== */
36// These are all types that are conceptually layers on top of entry and
37// friends. They allow us to process more complex requests and provide
38// domain specific fields for the purposes of IDM, over the normal
39// entry/ava/filter types. These related deeply to schema.
40
41/// The current purpose of a User Auth Token. It may be read-only, read-write
42/// or privilege capable (able to step up to read-write after re-authentication).
43#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
44#[serde(rename_all = "lowercase")]
45pub enum UatPurposeStatus {
46    ReadOnly,
47    ReadWrite,
48    PrivilegeCapable,
49}
50
51/// The expiry of the User Auth Token.
52#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
53#[serde(rename_all = "lowercase")]
54pub enum UatStatusState {
55    #[serde(with = "time::serde::timestamp")]
56    ExpiresAt(time::OffsetDateTime),
57    NeverExpires,
58    Revoked,
59}
60
61impl fmt::Display for UatStatusState {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            UatStatusState::ExpiresAt(odt) => write!(f, "expires at {odt}"),
65            UatStatusState::NeverExpires => write!(f, "never expires"),
66            UatStatusState::Revoked => write!(f, "revoked"),
67        }
68    }
69}
70
71/// The status of a User Auth Token
72#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
73#[serde(rename_all = "lowercase")]
74pub struct UatStatus {
75    pub account_id: Uuid,
76    pub session_id: Uuid,
77    pub state: UatStatusState,
78    #[serde(with = "time::serde::timestamp")]
79    pub issued_at: time::OffsetDateTime,
80    pub purpose: UatPurposeStatus,
81}
82
83impl fmt::Display for UatStatus {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        writeln!(f, "account_id: {}", self.account_id)?;
86        writeln!(f, "session_id: {}", self.session_id)?;
87        writeln!(f, "state: {}", self.state)?;
88        writeln!(f, "issued_at: {}", self.issued_at)?;
89        match &self.purpose {
90            UatPurposeStatus::ReadOnly => writeln!(f, "purpose: read only")?,
91            UatPurposeStatus::ReadWrite => writeln!(f, "purpose: read write")?,
92            UatPurposeStatus::PrivilegeCapable => writeln!(f, "purpose: privilege capable")?,
93        }
94        Ok(())
95    }
96}
97
98/// A request to generate a new API token for a service account
99#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
100#[serde(rename_all = "lowercase")]
101pub struct ApiTokenGenerate {
102    pub label: String,
103    #[serde(with = "time::serde::timestamp::option")]
104    pub expiry: Option<time::OffsetDateTime>,
105    pub read_write: bool,
106    #[serde(default)]
107    pub compact: bool,
108}
109
110/* ===== low level proto types ===== */
111
112/// A limited view of an entry in Kanidm.
113#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default, ToSchema)]
114pub struct Entry {
115    pub attrs: BTreeMap<String, Vec<String>>,
116}
117
118impl fmt::Display for Entry {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        writeln!(f, "---")?;
121        self.attrs
122            .iter()
123            .try_for_each(|(k, vs)| vs.iter().try_for_each(|v| writeln!(f, "{k}: {v}")))
124    }
125}
126
127/// A response to a whoami request
128#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema)]
129pub struct WhoamiResponse {
130    // Should we just embed the entry? Or destructure it?
131    pub youare: Entry,
132}
133
134impl WhoamiResponse {
135    pub fn new(youare: Entry) -> Self {
136        WhoamiResponse { youare }
137    }
138}
139
140// Simple string value provision.
141#[derive(Debug, Serialize, Deserialize, ToSchema)]
142pub struct SingleStringRequest {
143    pub value: String,
144}
145
146impl SingleStringRequest {
147    pub fn new(s: String) -> Self {
148        SingleStringRequest { value: s }
149    }
150}