Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions apps/piquelctl/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions apps/piqueld/src/auth/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -661,7 +661,7 @@ impl Auth {
code: &str,
request: Option<&AuditRequest>,
) -> Result<DeviceToken> {
let pending = |status: &str| DeviceToken {
let pending = |status: DeviceLoginStatus| DeviceToken {
status: status.into(),
token: None,
user: None,
Expand All @@ -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()),
Expand Down Expand Up @@ -748,7 +748,7 @@ impl Auth {
};
issuing.finish();
Ok(DeviceToken {
status: "complete".into(),
status: DeviceLoginStatus::Complete.into(),
token: Some(prepared.token),
user: Some(user),
})
Expand Down Expand Up @@ -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),
})
Expand Down
23 changes: 23 additions & 0 deletions crates/piqueld-core/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -571,10 +572,32 @@ pub struct DeviceRequest {
pub confirms: Option<DeviceConfirmation>,
}

string_enum! {
/// 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 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.
Expand Down
20 changes: 11 additions & 9 deletions docs/dev/rust-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down