diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 099e8c982..ef7185b88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,8 @@ # rust-lint -> Clippy, rustdoc with warnings denied, and the doc tests # package -> the SDKs packaged as `just publish` publishes them, each crate # built from its package, without uploading anything -# msrv -> the published crates built with their oldest supported Rust +# msrv -> the published crates built with their oldest supported Rust, with +# all their features, none, and each one alone # e2e -> the browser suite over HTTPS against the real binary and PostgreSQL, # with the admin dashboard on its own listener and host, sourceorigin.dev's # web server on its own host, Mailpit receiving auth's email, and @@ -219,8 +220,10 @@ jobs: msrv=$(sed -n 's/^rust-version = "\(.*\)"$/\1/p' Cargo.toml) rustup toolchain install "$msrv" --profile minimal echo "RUSTUP_TOOLCHAIN=$msrv" >> "$GITHUB_ENV" + - *install-just - *rust-cache - run: cargo check --locked --all-features --package sourceorigin-common --package sourceorigin-crypto --package sourceorigin-auth + - run: just rust features rust: name: rust diff --git a/crates/common/src/admin.rs b/crates/common/src/admin.rs index 695486f38..e1190f6ef 100644 --- a/crates/common/src/admin.rs +++ b/crates/common/src/admin.rs @@ -3,14 +3,16 @@ //! database crate decodes some straight into them. //! //! Bodies the account settings also return come from [`crate::protocol::api`] -//! and are re-exported here. +//! and are re-exported here, as is the +//! [`UserAgentFamily`](crate::admin::UserAgentFamily) the session and device +//! views show, which events record too. pub use crate::protocol::api::{Identity, Passkey}; +pub use crate::user_agent::{Browser, Platform, UserAgentFamily}; use crate::{ AccountStatus, AnnouncementId, AuthenticationMethod, ClientId, DeliveryId, EventId, Handle, PictureId, SessionId, TokenId, TraceId, UserId, clients::RelatedOrigin, - events::UserAgentFamily, form::Checkbox, protocol::{jwt::KeyId, oauth::ClientAuthMethod}, }; @@ -723,10 +725,8 @@ pub enum DraftIntent { } /// The result of verifying the audit chain, as `auth-service audit verify` prints -/// it and the audit log records it, as [`DomainEvent::AuditChainVerified`], for +/// it and the audit log records it, as `DomainEvent::AuditChainVerified`, for /// the dashboard's audit page to show. Verification stops at the first problem. -/// -/// [`DomainEvent::AuditChainVerified`]: crate::events::DomainEvent::AuditChainVerified #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct AuditVerification { /// The first position that does not match, or `None` if the chain is intact. diff --git a/crates/common/src/events/context.rs b/crates/common/src/events/context.rs index b59b93173..122e5145a 100644 --- a/crates/common/src/events/context.rs +++ b/crates/common/src/events/context.rs @@ -1,6 +1,6 @@ //! Who caused an event, where their request came from and why, recorded beside it. -use crate::{ClientId, SessionId, UserId}; +use crate::{ClientId, SessionId, UserId, user_agent::UserAgentFamily}; use serde::{Deserialize, Serialize}; use std::net::IpAddr; @@ -91,128 +91,6 @@ pub enum JobName { AuditSealer, } -crate::str_enum! { - /// Coarse browser derived from a `User-Agent` header, which is never stored. - pub enum Browser { - /// Google Chrome and other Chromium browsers that announce no other name. - Chrome = "chrome", - /// Microsoft Edge. - Edge = "edge", - /// Mozilla Firefox. - Firefox = "firefox", - /// Opera. - Opera = "opera", - /// Apple Safari. - Safari = "safari", - /// Anything else, including back-channel HTTP clients, and browsers a newer - /// release recognizes. - Other = "other", - } else Other -} - -crate::str_enum! { - /// Coarse operating system derived from a `User-Agent` header. - pub enum Platform { - /// Android. - Android = "android", - /// `ChromeOS`. - ChromeOs = "chrome_os", - /// iOS and iPadOS. iPads that request desktop sites announce macOS. - Ios = "ios", - /// Linux on the desktop. - Linux = "linux", - /// macOS. - MacOs = "macos", - /// Windows. - Windows = "windows", - /// Anything else, including back-channel HTTP clients, and platforms a newer - /// release recognizes. - Other = "other", - } else Other -} - -/// Coarse browser and platform of a `User-Agent` header, the only part of it auth -/// keeps. Stored as text, `browser/platform` (`firefox/linux`); serialized as -/// `{"browser": …, "platform": …}`. A browser or platform this release doesn't -/// know reads as `other`, so a session or event a newer release tagged still loads. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct UserAgentFamily { - /// The browser. - pub browser: Browser, - /// The operating system it runs on. - pub platform: Platform, -} - -impl UserAgentFamily { - /// Classifies a `User-Agent` header. Markers are checked in order: Edge and Opera - /// also announce Chrome, Chrome announces Safari, Android and `ChromeOS` announce - /// Linux, and iOS announces macOS. - #[must_use] - pub fn classify(user_agent: &str) -> Self { - /// The value of the first marker `user_agent` contains, or `other`. - fn first(user_agent: &str, markers: &[(&str, T)], other: T) -> T { - markers - .iter() - .find(|(marker, _)| user_agent.contains(marker)) - .map_or(other, |&(_, value)| value) - } - Self { - browser: first( - user_agent, - &[ - ("Edg/", Browser::Edge), - ("EdgA/", Browser::Edge), - ("EdgiOS/", Browser::Edge), - ("OPR/", Browser::Opera), - ("Firefox/", Browser::Firefox), - ("FxiOS/", Browser::Firefox), - ("Chrome/", Browser::Chrome), - ("CriOS/", Browser::Chrome), - ("Safari/", Browser::Safari), - ], - Browser::Other, - ), - platform: first( - user_agent, - &[ - ("Android", Platform::Android), - ("CrOS", Platform::ChromeOs), - ("iPhone", Platform::Ios), - ("iPad", Platform::Ios), - ("iPod", Platform::Ios), - ("Mac OS X", Platform::MacOs), - ("Windows", Platform::Windows), - ("Linux", Platform::Linux), - ], - Platform::Other, - ), - } - } -} - -impl std::fmt::Display for UserAgentFamily { - /// Formats the stored text, `browser/platform`. - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}/{}", self.browser, self.platform) - } -} - -impl std::str::FromStr for UserAgentFamily { - type Err = crate::VocabularyError; - /// Parses the stored text, `browser/platform`. - fn from_str(value: &str) -> Result { - let (browser, platform) = value.split_once('/').ok_or(crate::VocabularyError { - vocabulary: "UserAgentFamily", - })?; - Ok(Self { - browser: browser.parse()?, - platform: platform.parse()?, - }) - } -} - -crate::__str_enum_sqlx!(UserAgentFamily); - /// Where a request came from. Both fields are personal data. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct RequestOrigin { diff --git a/crates/common/src/events/mod.rs b/crates/common/src/events/mod.rs index 74a48529c..f9864ae03 100644 --- a/crates/common/src/events/mod.rs +++ b/crates/common/src/events/mod.rs @@ -15,9 +15,8 @@ mod payload; #[cfg(test)] mod tests; -pub use context::{ - Actor, Browser, EventContext, JobName, Platform, RequestOrigin, UserAgentFamily, -}; +pub use crate::user_agent::{Browser, Platform, UserAgentFamily}; +pub use context::{Actor, EventContext, JobName, RequestOrigin}; pub use history::{FollowUp, HistoryActor, HistoryEntry}; pub use payload::{ CodeRedemptionFailure, GrantRevocation, InlineSignIn, RecordedOrigin, RecoveryRevocation, diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index e6be06a55..dc53c1704 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -52,6 +52,10 @@ pub mod protocol; /// Look-alike detection for handles, client names and the names reserved from them. #[cfg(feature = "similarity")] pub mod similarity; +/// The browser and platform of a `User-Agent` header, which `admin` and `events` +/// re-export. `events` enables `admin`. +#[cfg(feature = "admin")] +mod user_agent; use serde::{Deserialize, Serialize}; use std::{error::Error as StdError, fmt, str::FromStr}; diff --git a/crates/common/src/user_agent.rs b/crates/common/src/user_agent.rs new file mode 100644 index 000000000..9795012fc --- /dev/null +++ b/crates/common/src/user_agent.rs @@ -0,0 +1,126 @@ +//! The coarse browser and platform of a `User-Agent` header, the only part of it +//! auth keeps: sessions, events and the admin views record it. + +use serde::{Deserialize, Serialize}; + +crate::str_enum! { + /// Coarse browser derived from a `User-Agent` header, which is never stored. + pub enum Browser { + /// Google Chrome and other Chromium browsers that announce no other name. + Chrome = "chrome", + /// Microsoft Edge. + Edge = "edge", + /// Mozilla Firefox. + Firefox = "firefox", + /// Opera. + Opera = "opera", + /// Apple Safari. + Safari = "safari", + /// Anything else, including back-channel HTTP clients, and browsers a newer + /// release recognizes. + Other = "other", + } else Other +} + +crate::str_enum! { + /// Coarse operating system derived from a `User-Agent` header. + pub enum Platform { + /// Android. + Android = "android", + /// `ChromeOS`. + ChromeOs = "chrome_os", + /// iOS and iPadOS. iPads that request desktop sites announce macOS. + Ios = "ios", + /// Linux on the desktop. + Linux = "linux", + /// macOS. + MacOs = "macos", + /// Windows. + Windows = "windows", + /// Anything else, including back-channel HTTP clients, and platforms a newer + /// release recognizes. + Other = "other", + } else Other +} + +/// Coarse browser and platform of a `User-Agent` header, the only part of it auth +/// keeps. Stored as text, `browser/platform` (`firefox/linux`); serialized as +/// `{"browser": …, "platform": …}`. A browser or platform this release doesn't +/// know reads as `other`, so a session or event a newer release tagged still loads. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct UserAgentFamily { + /// The browser. + pub browser: Browser, + /// The operating system it runs on. + pub platform: Platform, +} + +impl UserAgentFamily { + /// Classifies a `User-Agent` header. Markers are checked in order: Edge and Opera + /// also announce Chrome, Chrome announces Safari, Android and `ChromeOS` announce + /// Linux, and iOS announces macOS. + #[must_use] + pub fn classify(user_agent: &str) -> Self { + /// The value of the first marker `user_agent` contains, or `other`. + fn first(user_agent: &str, markers: &[(&str, T)], other: T) -> T { + markers + .iter() + .find(|(marker, _)| user_agent.contains(marker)) + .map_or(other, |&(_, value)| value) + } + Self { + browser: first( + user_agent, + &[ + ("Edg/", Browser::Edge), + ("EdgA/", Browser::Edge), + ("EdgiOS/", Browser::Edge), + ("OPR/", Browser::Opera), + ("Firefox/", Browser::Firefox), + ("FxiOS/", Browser::Firefox), + ("Chrome/", Browser::Chrome), + ("CriOS/", Browser::Chrome), + ("Safari/", Browser::Safari), + ], + Browser::Other, + ), + platform: first( + user_agent, + &[ + ("Android", Platform::Android), + ("CrOS", Platform::ChromeOs), + ("iPhone", Platform::Ios), + ("iPad", Platform::Ios), + ("iPod", Platform::Ios), + ("Mac OS X", Platform::MacOs), + ("Windows", Platform::Windows), + ("Linux", Platform::Linux), + ], + Platform::Other, + ), + } + } +} + +impl std::fmt::Display for UserAgentFamily { + /// Formats the stored text, `browser/platform`. + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}/{}", self.browser, self.platform) + } +} + +impl std::str::FromStr for UserAgentFamily { + type Err = crate::VocabularyError; + /// Parses the stored text, `browser/platform`. + fn from_str(value: &str) -> Result { + let (browser, platform) = value.split_once('/').ok_or(crate::VocabularyError { + vocabulary: "UserAgentFamily", + })?; + Ok(Self { + browser: browser.parse()?, + platform: platform.parse()?, + }) + } +} + +crate::__str_enum_sqlx!(UserAgentFamily); diff --git a/docs/dev/testing.md b/docs/dev/testing.md index 7ad44c898..b03c766e7 100644 --- a/docs/dev/testing.md +++ b/docs/dev/testing.md @@ -159,6 +159,7 @@ expressions included. | Clippy and ESLint with warnings denied, and `publint` and `attw` on the built TypeScript SDK | `just lint` (`just rust lint` / `just web lint`) | | Unused Rust dependencies (cargo-machete), and unused web files, exports and dependencies (knip) | `just unused` (`just rust unused` / `just web unused`) | | Type-check Rust, and TypeScript with the pages' scripts | `just check` (`just rust check` / `just web check`) | +| Type-check each published crate with no features, then with each feature alone, as a dependent might enable it | `just rust features` (part of `just check`) | | Rust unit tests, or one package's | `just test` / `just test -p auth-service` | | Rust docs with warnings denied, and the doc tests | `just doc` / `just doc-test` | | TypeScript SDK tests with coverage, and the runtime smoke test | `just web test` / `just web smoke` | @@ -185,7 +186,7 @@ CI runs the same recipes, in these jobs: | `rust` | `just coverage` against a PostgreSQL 18 service, then `just db prepare-check` | | `rust-lint` | `just rust lint`, `just doc` and `just doc-test` | | `package` | `just publish --dry-run`: see [releasing](releasing.md#what-gets-published) | -| `msrv` | `cargo check` of the published crates with their oldest supported Rust, the workspace's `rust-version` | +| `msrv` | `cargo check` of the published crates with all their features, then `just rust features`, with their oldest supported Rust, the workspace's `rust-version` | | `e2e` | The browser suite, against the debug binary behind Caddy, with Mailpit receiving auth's email and sourceorigin.dev's web server on its own host | | `docker` | Builds the auth, web and docs images, the first two from the layers `main` cached last | | `infra` | `just infra check`, with the latest piquelctl | diff --git a/just/rust.just b/just/rust.just index 1463fd1e3..0818f54a2 100644 --- a/just/rust.just +++ b/just/rust.just @@ -3,6 +3,19 @@ set working-directory := '..' check: @cargo check --locked --workspace --all-targets +# The workspace unifies features, so a published crate's feature that needs +# another it doesn't enable still builds there. Checking each crate with no +# features, then each feature alone, builds them as a dependent might. +features: + #!/usr/bin/env bash + set -euo pipefail + for manifest in crates/{common,crypto,auth-sdk}/Cargo.toml; do + cargo check --locked --manifest-path "$manifest" --no-default-features + for feature in $(sed -n '/^\[features\]/,/^\[/s/^\([a-z0-9_-]*\) = .*/\1/p' "$manifest"); do + cargo check --locked --manifest-path "$manifest" --no-default-features --features "$feature" + done + done + fmt: @cargo fmt --all diff --git a/justfile b/justfile index 887d68e5b..39a14b3a5 100644 --- a/justfile +++ b/justfile @@ -60,7 +60,7 @@ fmt-check: rust::fmt-check web::fmt-check # Warnings fail both linters, like `-D warnings`. lint: rust::lint web::lint -check: rust::check web::check +check: rust::check rust::features web::check # Unused Rust dependencies, and unused web files, exports and dependencies. unused: rust::unused web::unused