Skip to main content

comindly_api/
account.rs

1//! Signing in, members, access tokens, secrets and the audit log (`/api/v1/auth/*`, `/members`,
2//! `/tokens`, `/secrets`, `/audit`).
3
4use serde::{Deserialize, Serialize};
5
6pub use crate::server::{
7    service_email, service_name, AccessToken, AuditEntry, AuditPage, AuditQuery, DeviceRequest,
8    Invite, Member, MemberTokenRevoke, NewService, Role, SecretInfo, ServiceTokenRequest, SignIn,
9    SERVICE_DOMAIN,
10};
11
12/// `GET /api/v1/auth/me`: who is signed in, or `local` on this machine.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14#[serde(tag = "mode", rename_all = "lowercase")]
15pub enum Me {
16    Local,
17    Server {
18        signed_in: bool,
19        email: Option<String>,
20        role: Option<Role>,
21        /// One way to sign in, for older clients; `sign_in_methods` lists them all.
22        sign_in: SignIn,
23        #[serde(default)]
24        sign_in_methods: Vec<SignIn>,
25        /// The provider's name for the sign-in button, when members sign in through one.
26        #[serde(default, skip_serializing_if = "Option::is_none")]
27        oidc_label: Option<String>,
28        /// `microsoft` or `google` when the issuer is theirs, for the button's logo.
29        #[serde(default, skip_serializing_if = "Option::is_none")]
30        oidc_brand: Option<String>,
31        public_url: String,
32        /// The gateway agents connect through with the organisation's sign-in, when there is one.
33        #[serde(default, skip_serializing_if = "Option::is_none")]
34        gateway_url: Option<String>,
35    },
36}
37
38/// `{ email }`: request a sign-in link, reset a code, remove a member or withdraw an invitation.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct EmailRequest {
41    pub email: String,
42}
43
44/// `POST /api/v1/auth/redeem`: a sign-in link's token, an invitation or the admin token.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct RedeemRequest {
47    pub token: String,
48}
49
50/// `POST /api/v1/auth/code`: sign in with an email address and a code.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct CodeRequest {
53    pub email: String,
54    pub code: String,
55}
56
57/// A change that hands out a sign-in code: signing in with an invitation, or a reset.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct WithCode {
60    pub ok: bool,
61    pub message: String,
62    pub code: Option<String>,
63}
64
65/// `POST /api/v1/members/invite` and `/members/role`.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct MemberRole {
68    pub email: String,
69    pub role: Role,
70}
71
72/// An invitation sent: by email, or with a `code` the admin hands over.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct Invited {
75    pub ok: bool,
76    pub message: String,
77    #[serde(default, skip_serializing_if = "Option::is_none")]
78    pub code: Option<String>,
79}
80
81/// `GET /api/v1/members`.
82#[derive(Debug, Serialize, Deserialize)]
83pub struct Members {
84    pub members: Vec<Member>,
85    pub invites: Vec<Invite>,
86    pub sign_in: SignIn,
87    #[serde(default)]
88    pub sign_in_methods: Vec<SignIn>,
89}
90
91/// `GET /api/v1/tokens`: your personal access tokens.
92#[derive(Debug, Serialize, Deserialize)]
93pub struct Tokens {
94    pub tokens: Vec<AccessToken>,
95}
96
97fn default_token_days() -> Option<u32> {
98    Some(crate::server::TOKEN_DAYS)
99}
100
101/// `POST /api/v1/tokens`: absent `expires_in_days` means 90 days, `null` never.
102#[derive(Debug, Clone, Serialize, Deserialize)]
103pub struct NewToken {
104    pub name: String,
105    #[serde(default = "default_token_days")]
106    pub expires_in_days: Option<u32>,
107}
108
109/// A personal access token, shown once.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct TokenCreated {
112    pub ok: bool,
113    pub message: String,
114    pub token: String,
115}
116
117/// `GET /api/v1/secrets`: where each token and key comes from, never its value.
118#[derive(Debug, Serialize, Deserialize)]
119pub struct Secrets {
120    pub secrets: Vec<SecretState>,
121    pub can_store: bool,
122}
123
124/// One secret: stored here with its details, or from the deployment or nowhere.
125#[derive(Debug, Serialize, Deserialize)]
126#[serde(untagged)]
127pub enum SecretState {
128    Elsewhere(SecretSource),
129    Stored {
130        #[serde(flatten)]
131        info: SecretInfo,
132        label: String,
133        /// The deployment provides one too; this one is used instead.
134        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
135        replaces_deployment: bool,
136    },
137}
138
139/// A secret the deployment provides (`deployment`) or nobody set (`none`).
140#[derive(Debug, Serialize, Deserialize)]
141#[serde(deny_unknown_fields)]
142pub struct SecretSource {
143    pub source: String,
144    pub name: String,
145    pub label: String,
146}
147
148/// `POST /api/v1/secrets`: an empty value removes it.
149#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct SecretRequest {
151    pub name: String,
152    #[serde(default)]
153    pub value: String,
154}
155
156/// `POST /api/v1/auth/device/start`: a command line asks to sign in.
157#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct DeviceStartRequest {
159    pub client: String,
160}
161
162/// Where to approve the sign-in, and how often to poll.
163#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct DeviceStart {
165    pub device_code: String,
166    pub user_code: String,
167    pub verify_url: String,
168    pub interval: u64,
169    pub expires_in: u64,
170}
171
172/// `POST /api/v1/auth/device/token`: what the command line polls with.
173#[derive(Debug, Clone, Serialize, Deserialize)]
174pub struct DeviceTokenRequest {
175    pub device_code: String,
176}
177
178/// Pending, or the token once a member approved; denied and expired are errors.
179#[derive(Debug, Clone, Serialize, Deserialize)]
180#[serde(tag = "status", rename_all = "lowercase")]
181pub enum DeviceToken {
182    Pending,
183    Approved {
184        token: String,
185        email: String,
186        expires: Option<String>,
187    },
188}
189
190/// `GET /api/v1/auth/device?code=`.
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct DeviceCodeQuery {
193    pub code: String,
194}
195
196/// `POST /api/v1/auth/device/approve`.
197#[derive(Debug, Clone, Serialize, Deserialize)]
198pub struct DeviceApprove {
199    pub code: String,
200    pub approve: bool,
201}