Skip to content
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions crates/common/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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.
Expand Down
124 changes: 1 addition & 123 deletions crates/common/src/events/context.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<T: Copy>(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<Self, Self::Err> {
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 {
Expand Down
5 changes: 2 additions & 3 deletions crates/common/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
126 changes: 126 additions & 0 deletions crates/common/src/user_agent.rs
Original file line number Diff line number Diff line change
@@ -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<T: Copy>(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<Self, Self::Err> {
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);
3 changes: 2 additions & 1 deletion docs/dev/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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 |
Expand Down
13 changes: 13 additions & 0 deletions just/rust.just
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down