From f7c578860dd7ddede6ae02f7a06580dece50890e Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 15:18:14 +0100 Subject: [PATCH 1/2] refactor(core): name device login statuses in one enum A device login poll's status (`authorization_pending`, `slow_down`, `complete`) was a string the daemon wrote and `piquelctl login` matched on as literals. `DeviceLoginStatus` in piqueld-core names each one; the daemon builds `DeviceToken.status` from it and the CLI parses it. `DeviceToken.status` stays a `String`, so the CLI still reads statuses a newer daemon adds, and the OpenAPI document stays the same. Fixes PQD-161 --- apps/piquelctl/src/auth.rs | 14 ++++++++------ apps/piqueld/src/auth/sessions.rs | 14 +++++++------- crates/piqueld-core/src/auth/mod.rs | 22 ++++++++++++++++++++++ crates/piqueld-core/src/auth/tests.rs | 17 +++++++++++++++++ docs/dev/rust-conventions.md | 20 +++++++++++--------- 5 files changed, 65 insertions(+), 22 deletions(-) diff --git a/apps/piquelctl/src/auth.rs b/apps/piquelctl/src/auth.rs index 0402227a5..2b0e6d29b 100644 --- a/apps/piquelctl/src/auth.rs +++ b/apps/piquelctl/src/auth.rs @@ -6,7 +6,9 @@ use crate::{ }; use piqueld_client::{ Client, ClientError, - auth::{DeviceStart, DeviceToken, OperatorLink, SensitiveAction, SetupLink, User}, + auth::{ + DeviceLoginStatus, DeviceStart, DeviceToken, OperatorLink, SensitiveAction, SetupLink, User, + }, }; use serde::{Deserialize, Serialize}; use std::{ @@ -576,11 +578,11 @@ impl Approval { loop { tokio::time::sleep(Duration::from_secs(interval)).await; let result = client.auth_device_poll(&start.device_code).await?; - match result.status.as_str() { - "authorization_pending" => {} - "slow_down" => interval += 5, - "complete" => return Ok(result), - _ => { + match DeviceLoginStatus::parse(&result.status) { + Some(DeviceLoginStatus::AuthorizationPending) => {} + Some(DeviceLoginStatus::SlowDown) => interval += 5, + Some(DeviceLoginStatus::Complete) => return Ok(result), + None => { return Err(CliError::new( ErrorKind::General, "unexpected device login response", diff --git a/apps/piqueld/src/auth/sessions.rs b/apps/piqueld/src/auth/sessions.rs index 867e77ec8..5799406ff 100644 --- a/apps/piqueld/src/auth/sessions.rs +++ b/apps/piqueld/src/auth/sessions.rs @@ -3,8 +3,8 @@ use super::{Auth, AuthError, CredentialKind, DAY, MAX_PENDING, Result, now_secs}; use piqueld_core::access::{Denied, Grants, Reauthenticate}; use piqueld_core::auth::{ - DeviceConfirmation, DeviceRequest, DeviceStart, DeviceToken, HostOperator, OperatorLink, - Principal, SensitiveAction, Session, User, + DeviceConfirmation, DeviceLoginStatus, DeviceRequest, DeviceStart, DeviceToken, HostOperator, + OperatorLink, Principal, SensitiveAction, Session, User, }; use piqueld_store::{ Actor, AuditRequest, Author, Caller, Cascade, NewCredential, Proof, StoreError, @@ -661,7 +661,7 @@ impl Auth { code: &str, request: Option<&AuditRequest>, ) -> Result { - let pending = |status: &str| DeviceToken { + let pending = |status: DeviceLoginStatus| DeviceToken { status: status.into(), token: None, user: None, @@ -679,11 +679,11 @@ impl Auth { return Err(AuthError::Unauthorized); } if device.next_poll > now { - return Ok(pending("slow_down")); + return Ok(pending(DeviceLoginStatus::SlowDown)); } device.next_poll = now + 5; let Some(approver) = device.approved_by.clone().filter(|_| !device.issuing) else { - return Ok(pending("authorization_pending")); + return Ok(pending(DeviceLoginStatus::AuthorizationPending)); }; let next = match &device.confirms { Some(confirms) => Approved::Confirm(confirms.clone()), @@ -748,7 +748,7 @@ impl Auth { }; issuing.finish(); Ok(DeviceToken { - status: "complete".into(), + status: DeviceLoginStatus::Complete.into(), token: Some(prepared.token), user: Some(user), }) @@ -796,7 +796,7 @@ impl Auth { "confirmed CLI login with passkey" ); Ok(DeviceToken { - status: "complete".into(), + status: DeviceLoginStatus::Complete.into(), token: None, user: Some(confirms.user), }) diff --git a/crates/piqueld-core/src/auth/mod.rs b/crates/piqueld-core/src/auth/mod.rs index 206d9b589..c4c346706 100644 --- a/crates/piqueld-core/src/auth/mod.rs +++ b/crates/piqueld-core/src/auth/mod.rs @@ -1,5 +1,6 @@ //! Authentication contracts shared by the daemon, browser, and CLI. use crate::access::Grants; +use crate::names::{compared_as_string, string_enum}; use serde::{Deserialize, Serialize}; use std::fmt; use utoipa::ToSchema; @@ -571,10 +572,31 @@ pub struct DeviceRequest { pub confirms: Option, } +string_enum! { + #[serde] + /// Where a device login stands, as a poll reports it in + /// [`DeviceToken::status`]. + pub enum DeviceLoginStatus { + /// Not approved yet: poll again after the interval. + AuthorizationPending => "authorization_pending", + /// Polled sooner than five seconds after the last poll: wait five + /// seconds longer between polls. + SlowDown => "slow_down", + /// Approved: the response carries the issued token, exactly once, + /// unless the login confirmed an existing one. + Complete => "complete", + } +} + +compared_as_string!(DeviceLoginStatus); + /// Poll result; the token is returned exactly once after explicit approval. #[derive(Clone, Serialize, Deserialize, ToSchema)] pub struct DeviceToken { /// `authorization_pending`, `slow_down`, or `complete`. + // + // A `DeviceLoginStatus`, kept a string so a CLI reads statuses a newer + // daemon adds; compare it with `DeviceLoginStatus::parse` or `==`. pub status: String, /// Issued CLI credential; absent when the request confirmed an existing /// login instead. diff --git a/crates/piqueld-core/src/auth/tests.rs b/crates/piqueld-core/src/auth/tests.rs index 50fc6c45c..61f7ac9cc 100644 --- a/crates/piqueld-core/src/auth/tests.rs +++ b/crates/piqueld-core/src/auth/tests.rs @@ -40,3 +40,20 @@ fn revocations_cascade_unless_asked_not_to() { Manage::RevokeCredential { cascade: true, .. } )); } + +/// Each device login status is sent as its own string and read back as +/// itself, and no two share one. +#[test] +fn device_login_statuses_serialize_to_distinct_strings() { + for status in DeviceLoginStatus::ALL { + let value = status.as_str(); + assert_eq!(serde_json::to_value(status).unwrap(), value); + assert_eq!( + serde_json::from_value::(value.into()).unwrap(), + status + ); + assert_eq!(DeviceLoginStatus::parse(value), Some(status)); + } + let values = DeviceLoginStatus::ALL.map(DeviceLoginStatus::as_str); + assert_eq!(std::collections::BTreeSet::from(values).len(), values.len()); +} diff --git a/docs/dev/rust-conventions.md b/docs/dev/rust-conventions.md index 46f22031b..238e058fe 100644 --- a/docs/dev/rust-conventions.md +++ b/docs/dev/rust-conventions.md @@ -37,15 +37,17 @@ still reads a code it doesn't know yet; compare one with their own `DiagnosticCode`. Other strings that cross crates are named the same way, as a `string_enum!` -in `piqueld-core`: the kind of each history event is an `EventKind`, and the -name of each message a live server-sent event stream sends a `StreamMessage`. -Record and compare one through its variant, never its string, and in SQL bind -it as a parameter. Migrations can't name an `EventKind`, so a store test -checks that every kind the schema spells out, in an index or a view, is one. -`Event.kind` stays a `String`, since history holds kinds older daemons -recorded and a client may read kinds a newer daemon adds; compare it with -`Event::known_kind` or `==`. A stream reader likewise skips message names it -doesn't know. +in `piqueld-core`: the kind of each history event is an `EventKind`, the name +of each message a live server-sent event stream sends a `StreamMessage`, and +the status a device login poll reports a `DeviceLoginStatus`. Record and +compare one through its variant, never its string, and in SQL bind it as a +parameter. Migrations can't name an `EventKind`, so a store test checks that +every kind the schema spells out, in an index or a view, is one. A wire field +that holds one stays a `String`, so a client can still decode values a newer +daemon adds, and history the kinds older daemons recorded: compare +`Event.kind` with `Event::known_kind` or `==`, and `DeviceToken.status` with +`DeviceLoginStatus::parse` or `==`. A stream reader likewise skips message +names it doesn't know. Production code never panics without saying why: `expect_used`, `panic`, `todo`, `unimplemented` and `dbg_macro` are denied outside tests. Return an From 03dc7a2dd90db360e6f2ae417aeacfb82bd8181d Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 16:11:27 +0100 Subject: [PATCH 2/2] refactor(core): drop unused derives from the device login status `DeviceLoginStatus` drops the serde derives nothing uses, with the test of them: the macro already makes each status parse back and rejects a duplicate string. The note on `DeviceToken.status` says what an unknown status does: it still decodes, and `piquelctl` stops with an unexpected response. Fixes PQD-161 --- crates/piqueld-core/src/auth/mod.rs | 7 ++++--- crates/piqueld-core/src/auth/tests.rs | 17 ----------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/crates/piqueld-core/src/auth/mod.rs b/crates/piqueld-core/src/auth/mod.rs index c4c346706..b8289e482 100644 --- a/crates/piqueld-core/src/auth/mod.rs +++ b/crates/piqueld-core/src/auth/mod.rs @@ -573,7 +573,6 @@ pub struct DeviceRequest { } string_enum! { - #[serde] /// Where a device login stands, as a poll reports it in /// [`DeviceToken::status`]. pub enum DeviceLoginStatus { @@ -595,8 +594,10 @@ compared_as_string!(DeviceLoginStatus); pub struct DeviceToken { /// `authorization_pending`, `slow_down`, or `complete`. // - // A `DeviceLoginStatus`, kept a string so a CLI reads statuses a newer - // daemon adds; compare it with `DeviceLoginStatus::parse` or `==`. + // A `DeviceLoginStatus`, kept a string so a response with a status this + // build doesn't know still decodes, and `piquelctl` stops with + // "unexpected device login response". Compare it with + // `DeviceLoginStatus::parse` or `==`. pub status: String, /// Issued CLI credential; absent when the request confirmed an existing /// login instead. diff --git a/crates/piqueld-core/src/auth/tests.rs b/crates/piqueld-core/src/auth/tests.rs index 61f7ac9cc..50fc6c45c 100644 --- a/crates/piqueld-core/src/auth/tests.rs +++ b/crates/piqueld-core/src/auth/tests.rs @@ -40,20 +40,3 @@ fn revocations_cascade_unless_asked_not_to() { Manage::RevokeCredential { cascade: true, .. } )); } - -/// Each device login status is sent as its own string and read back as -/// itself, and no two share one. -#[test] -fn device_login_statuses_serialize_to_distinct_strings() { - for status in DeviceLoginStatus::ALL { - let value = status.as_str(); - assert_eq!(serde_json::to_value(status).unwrap(), value); - assert_eq!( - serde_json::from_value::(value.into()).unwrap(), - status - ); - assert_eq!(DeviceLoginStatus::parse(value), Some(status)); - } - let values = DeviceLoginStatus::ALL.map(DeviceLoginStatus::as_str); - assert_eq!(std::collections::BTreeSet::from(values).len(), values.len()); -}