Skip to main content

adk_surface/web/
phone.rs

1//! The wire types of the device routes and the phone's routes (spec 3.3 §5.4, §8.2, §10 K2).
2//!
3//! They compile under `web`, not `mobile`, so the OpenAPI document and the TypeScript types
4//! generated from it are the same in every `web` build (see [`openapi`](fn@super::openapi)).
5//! The routes themselves are served by [`devices_router`](super::devices_router) and, with
6//! feature `mobile`, `adk_surface::mobile::router`.
7
8use chrono::{DateTime, Utc};
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13use crate::rules::Answer;
14
15/// `POST /surface/decisions/{id}/answer` body: an answer from a paired device, at the version
16/// it saw (spec 3.3 §7.2, S-I2).
17///
18/// # Example
19///
20/// ```rust
21/// use adk_surface::web::AnswerRequest;
22///
23/// let request: AnswerRequest = serde_json::from_value(serde_json::json!({
24///     "version": 3,
25///     "answer": "deny",
26///     "expanded": true,
27/// }))
28/// .unwrap();
29/// assert_eq!(request.answer, adk_surface::Answer::Deny);
30/// ```
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
32#[serde(rename_all = "camelCase")]
33pub struct AnswerRequest {
34    /// The card's `version`.
35    pub version: u64,
36    /// The answer.
37    pub answer: Answer,
38    /// The changed arguments of an `edit`.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub edited_args: Option<Value>,
41    /// Why, for a `deny`; reported to the agent.
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub reason: Option<String>,
44    /// The device showed the call in full (§7.1).
45    #[serde(default)]
46    pub expanded: bool,
47    /// The digests of the images the device showed, for a call with images (spec 3.7 §8.2).
48    #[serde(default, skip_serializing_if = "Vec::is_empty")]
49    pub evidence: Vec<String>,
50}
51
52/// `POST /surface/stop` body: **Stop all** from a paired device (spec 3.3 §11 S-I10).
53#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
54#[serde(rename_all = "camelCase")]
55pub struct StopRequest {
56    /// Why.
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub reason: Option<String>,
59}
60
61/// `GET /surface/phone` answer: what the phone needs to subscribe and to confirm it is you
62/// (spec 3.3 §5.4, §8.2).
63#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
64#[serde(rename_all = "camelCase")]
65pub struct PhoneInfo {
66    /// The kernel's push key, which the phone subscribes with (`applicationServerKey`).
67    pub push_key: String,
68    /// The passkeys' relying party: the home's host.
69    pub rp_id: String,
70    /// The caller has a passkey here: this phone's, or for the web home, the person's.
71    pub passkey: bool,
72    /// This phone is subscribed to wake-ups.
73    pub push: bool,
74    /// The caller may set up a passkey now: a phone within 10 minutes of pairing without one,
75    /// or the person at the web home.
76    pub can_add_passkey: bool,
77}
78
79/// The keys a browser gives with its push subscription.
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
81#[serde(rename_all = "camelCase")]
82pub struct PushKeys {
83    /// The subscription's P-256 public key, base64url.
84    pub p256dh: String,
85    /// The subscription's authentication secret, base64url.
86    pub auth: String,
87}
88
89/// `PUT /surface/phone/push` body: the phone's push subscription, as `PushSubscription.toJSON()`
90/// gives it (spec 3.3 §5.4).
91///
92/// # Example
93///
94/// ```rust
95/// use adk_surface::web::PushSubscriptionRequest;
96///
97/// let request: PushSubscriptionRequest = serde_json::from_value(serde_json::json!({
98///     "endpoint": "https://web.push.apple.com/QGu1",
99///     "keys": { "p256dh": "BLn9…", "auth": "_ord…" },
100/// }))
101/// .unwrap();
102/// assert_eq!(request.keys.auth, "_ord…");
103/// ```
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
105#[serde(rename_all = "camelCase")]
106pub struct PushSubscriptionRequest {
107    /// The push service URL: https, on Apple's, Google's, Mozilla's or Microsoft's service.
108    pub endpoint: String,
109    /// The browser's keys.
110    pub keys: PushKeys,
111}
112
113/// `POST /surface/phone/push/received` body: the phone's acknowledgement of a wake-up.
114#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
115#[serde(rename_all = "camelCase")]
116pub struct PushReceivedRequest {
117    /// The wake-up's `id`.
118    pub id: String,
119}
120
121/// One wake-up a phone acknowledged, for "Push delivery" (spec 3.3 §5.4).
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
123#[serde(rename_all = "camelCase")]
124pub struct Delivery {
125    /// The phone.
126    pub device_id: String,
127    /// When the kernel sent it.
128    pub sent_at: DateTime<Utc>,
129    /// When the phone acknowledged it.
130    pub received_at: DateTime<Utc>,
131    /// The time between, in milliseconds.
132    pub latency_ms: u64,
133}
134
135/// `GET /surface/phone/push/deliveries` answer, for the operator.
136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
137#[serde(rename_all = "camelCase")]
138pub struct DeliveriesResponse {
139    /// The latest acknowledged wake-ups, oldest first.
140    pub deliveries: Vec<Delivery>,
141}
142
143/// `POST /surface/phone/passkey/options` answer: the options for
144/// `navigator.credentials.create`, as WebAuthn's JSON form gives them (spec 3.3 §8.2).
145#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
146#[serde(rename_all = "camelCase")]
147pub struct PasskeyOptions {
148    /// What the registration answers, with the credential.
149    pub registration_id: String,
150    /// `PublicKeyCredentialCreationOptionsJSON`: a platform authenticator, user verification
151    /// required, attestation none.
152    pub public_key: Value,
153}
154
155/// `POST /surface/phone/passkey` body: the new passkey.
156#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
157#[serde(rename_all = "camelCase")]
158pub struct PasskeyRegistration {
159    /// The options' `registrationId`.
160    pub registration_id: String,
161    /// The `PublicKeyCredential` as JSON (`credential.toJSON()`).
162    pub credential: Value,
163}
164
165/// `POST /surface/decisions/{id}/confirm/options` body: the answer the person is about to
166/// confirm, bound on the server to this decision, its version and these arguments (spec 3.3
167/// §8.2).
168///
169/// # Example
170///
171/// ```rust
172/// use adk_surface::web::ConfirmOptionsRequest;
173///
174/// let request: ConfirmOptionsRequest = serde_json::from_value(serde_json::json!({
175///     "version": 2,
176///     "answer": "allowOnce",
177///     "expanded": true,
178/// }))
179/// .unwrap();
180/// assert_eq!(request.answer, adk_surface::Answer::AllowOnce);
181/// ```
182#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
183#[serde(rename_all = "camelCase")]
184pub struct ConfirmOptionsRequest {
185    /// The card's `version`.
186    pub version: u64,
187    /// `allowOnce` or `edit`.
188    pub answer: Answer,
189    /// The changed arguments of an `edit`.
190    #[serde(default, skip_serializing_if = "Option::is_none")]
191    pub edited_args: Option<Value>,
192    /// The surface showed the call in full (§7.1).
193    #[serde(default)]
194    pub expanded: bool,
195}
196
197/// `POST /surface/agents/confirm/options` body: the change the person is about to confirm —
198/// `addAgent` or `changeAgent` with the digest its preview showed — bound on the server to
199/// exactly this command (spec 3.3 §7.2: the web home adds an agent at high risk after
200/// confirming it is you).
201///
202/// # Example
203///
204/// ```rust
205/// use adk_surface::web::AgentConfirmOptionsRequest;
206///
207/// let request: AgentConfirmOptionsRequest = serde_json::from_value(serde_json::json!({
208///     "command": { "type": "pauseAgent", "agent": "finance" },
209/// }))
210/// .unwrap();
211/// assert!(matches!(request.command, adk_surface::Command::PauseAgent { .. }));
212/// ```
213#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
214#[serde(rename_all = "camelCase")]
215pub struct AgentConfirmOptionsRequest {
216    /// The change.
217    pub command: crate::engine::Command,
218}
219
220/// `POST /surface/decisions/{id}/confirm/options` answer: the options for
221/// `navigator.credentials.get`, single use, for two minutes.
222#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
223#[serde(rename_all = "camelCase")]
224pub struct ConfirmOptions {
225    /// What the confirmation answers, with the assertion.
226    pub challenge_id: String,
227    /// `PublicKeyCredentialRequestOptionsJSON`: user verification required.
228    pub public_key: Value,
229}
230
231/// `POST /surface/decisions/{id}/confirm` body: the passkey's assertion for the bound answer.
232#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
233#[serde(rename_all = "camelCase")]
234pub struct ConfirmRequest {
235    /// The options' `challengeId`.
236    pub challenge_id: String,
237    /// The `PublicKeyCredential` as JSON (`credential.toJSON()`).
238    pub credential: Value,
239    /// The answer, when the page sends it again: it must be the bound one.
240    #[serde(default, skip_serializing_if = "Option::is_none")]
241    pub answer: Option<Answer>,
242    /// The changed arguments, when the page sends them again: they must be the bound ones.
243    #[serde(default, skip_serializing_if = "Option::is_none")]
244    pub edited_args: Option<Value>,
245}
246
247/// `POST /surface/phone/qr` body: the pairing link to draw, for the operator.
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
249#[serde(rename_all = "camelCase")]
250pub struct QrRequest {
251    /// The link: it starts with the home's URL.
252    pub url: String,
253}
254
255/// A paired device's phone attachments, as "Where you can say yes from" lists them.
256#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
257#[serde(rename_all = "camelCase")]
258pub struct PhoneDevice {
259    /// The device.
260    pub device_id: String,
261    /// It asks for the person's face or fingerprint.
262    pub passkey: bool,
263    /// It is woken by push.
264    pub push: bool,
265}
266
267/// A passkey the person set up on a computer for the web home.
268#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
269#[serde(rename_all = "camelCase")]
270pub struct ComputerPasskey {
271    /// Its id, which **Remove** names.
272    pub id: String,
273    /// When it was set up.
274    pub created_at: DateTime<Utc>,
275}
276
277/// `GET /surface/phone/devices` answer, for the operator.
278#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
279#[serde(rename_all = "camelCase")]
280pub struct PhoneDevicesResponse {
281    /// Every paired device with a passkey or a push subscription.
282    pub devices: Vec<PhoneDevice>,
283    /// The caller's computer passkeys.
284    pub computer_passkeys: Vec<ComputerPasskey>,
285}