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/// `GET /api/v1/secrets/status`: each git host token the server uses, as its host sees it.
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct TokenStatus {
127    /// The secret's name, `git:<host>`.
128    pub name: String,
129    pub host: String,
130    /// `YYYY-MM-DD`; none when it never expires or the host does not say.
131    #[serde(default, skip_serializing_if = "Option::is_none")]
132    pub expires: Option<String>,
133    /// Days until it expires; negative once it has.
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub days_left: Option<i64>,
136    /// The host refused it: expired, revoked, or without access.
137    pub refused: bool,
138    pub expired: bool,
139    /// The host could not be asked.
140    #[serde(default, skip_serializing_if = "Option::is_none")]
141    pub unknown: Option<String>,
142}
143
144/// One secret: stored here with its details, or from the deployment or nowhere.
145#[derive(Debug, Serialize, Deserialize)]
146#[serde(untagged)]
147pub enum SecretState {
148    Elsewhere(SecretSource),
149    Stored {
150        #[serde(flatten)]
151        info: SecretInfo,
152        label: String,
153        /// The deployment provides one too; this one is used instead.
154        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
155        replaces_deployment: bool,
156    },
157}
158
159/// A secret the deployment provides (`deployment`) or nobody set (`none`).
160#[derive(Debug, Serialize, Deserialize)]
161#[serde(deny_unknown_fields)]
162pub struct SecretSource {
163    pub source: String,
164    pub name: String,
165    pub label: String,
166}
167
168/// `POST /api/v1/secrets`: an empty value removes it.
169#[derive(Debug, Clone, Serialize, Deserialize)]
170pub struct SecretRequest {
171    pub name: String,
172    #[serde(default)]
173    pub value: String,
174}
175
176/// `POST /api/v1/auth/device/start`: a command line asks to sign in.
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct DeviceStartRequest {
179    pub client: String,
180}
181
182/// Where to approve the sign-in, and how often to poll.
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct DeviceStart {
185    pub device_code: String,
186    pub user_code: String,
187    pub verify_url: String,
188    pub interval: u64,
189    pub expires_in: u64,
190}
191
192/// `POST /api/v1/auth/device/token`: what the command line polls with.
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct DeviceTokenRequest {
195    pub device_code: String,
196}
197
198/// Pending, or the token once a member approved; denied and expired are errors.
199#[derive(Debug, Clone, Serialize, Deserialize)]
200#[serde(tag = "status", rename_all = "lowercase")]
201pub enum DeviceToken {
202    Pending,
203    Approved {
204        token: String,
205        email: String,
206        expires: Option<String>,
207    },
208}
209
210/// `GET /api/v1/auth/device?code=`.
211#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct DeviceCodeQuery {
213    pub code: String,
214}
215
216/// `POST /api/v1/auth/device/approve`.
217#[derive(Debug, Clone, Serialize, Deserialize)]
218pub struct DeviceApprove {
219    pub code: String,
220    pub approve: bool,
221}