|
1 | | -//! The one place that changes OIDC client registrations. Each change commits with |
2 | | -//! the event that records it. The `client` command uses it, as does the deployment |
| 1 | +//! The one place that changes OIDC client registrations and their approved |
| 2 | +//! related origins. Each change commits with the event that records it. The `client` command uses it, as does the deployment |
3 | 3 | //! job that registers the admin dashboard; the owner API (#43), dynamic |
4 | 4 | //! registration (#44) and the admin API (#38) will too. |
5 | 5 | //! |
|
8 | 8 |
|
9 | 9 | use piquel_common::{ |
10 | 10 | ClientId, |
11 | | - clients::{ClientKind, ClientRegistration}, |
| 11 | + clients::{ClientKind, ClientRegistration, RelatedOrigin}, |
12 | 12 | events::DomainEvent, |
13 | 13 | }; |
14 | 14 | use piquel_crypto::{ClientSecret, CryptoError}; |
15 | 15 | use piquel_repository::{Database, DatabaseError, DatabaseTransaction, StoredOidcClient}; |
| 16 | +use std::collections::BTreeSet; |
16 | 17 | use thiserror::Error; |
| 18 | +use url::Url; |
17 | 19 |
|
18 | 20 | use crate::{GracePeriod, Operator}; |
19 | 21 |
|
20 | | -/// Creates, updates, applies, enables, disables and rotates the secrets of clients. |
| 22 | +/// The most distinct domains related origins may span. Browsers must honour at |
| 23 | +/// least 5 (`WebAuthn` Level 3 §5.11.1) and ignore origins past their limit, so |
| 24 | +/// staying within 5 keeps every approved origin working in every browser. |
| 25 | +const MAX_RELATED_ORIGIN_DOMAINS: usize = 5; |
| 26 | + |
| 27 | +/// Creates, updates, applies, enables, disables and rotates the secrets of |
| 28 | +/// clients, and approves their related origins. |
21 | 29 | #[derive(Clone)] |
22 | 30 | pub struct ClientRegistry { |
23 | 31 | /// Where registrations and their events are stored. |
@@ -50,6 +58,44 @@ pub enum ClientRegistryError { |
50 | 58 | /// or secrets. |
51 | 59 | #[error("client {0}'s ID and kind cannot change; register a new client instead")] |
52 | 60 | Immutable(ClientId), |
| 61 | + /// Only trusted clients skip consent, which inline sign-in needs. |
| 62 | + #[error("client {0} is not trusted, so its pages cannot sign users in themselves")] |
| 63 | + Untrusted(ClientId), |
| 64 | + /// A client's pages are on the origins of its redirect URIs. |
| 65 | + #[error("{origin} is not the origin of any redirect URI of client {client}")] |
| 66 | + NotRedirectOrigin { |
| 67 | + /// The origin to approve. |
| 68 | + origin: RelatedOrigin, |
| 69 | + /// The client. |
| 70 | + client: ClientId, |
| 71 | + }, |
| 72 | + /// Browsers ignore an origin whose host has no registrable domain. |
| 73 | + #[error("{0} is on a public suffix, so browsers would ignore it")] |
| 74 | + PublicSuffix(RelatedOrigin), |
| 75 | + /// Browsers would ignore some approved origins. |
| 76 | + #[error( |
| 77 | + "approved origins would span {} domains ({}); browsers honour {MAX_RELATED_ORIGIN_DOMAINS}", |
| 78 | + .0.len(), |
| 79 | + .0.join(", ") |
| 80 | + )] |
| 81 | + TooManyDomains(Vec<String>), |
| 82 | + /// The origin is already approved, for this client or another. |
| 83 | + #[error("{origin} is already approved")] |
| 84 | + OriginApproved { |
| 85 | + /// The origin. |
| 86 | + origin: RelatedOrigin, |
| 87 | + /// The uniqueness violation. |
| 88 | + #[source] |
| 89 | + source: DatabaseError, |
| 90 | + }, |
| 91 | + /// The origin is not approved for this client. |
| 92 | + #[error("{origin} is not approved for client {client}")] |
| 93 | + OriginNotApproved { |
| 94 | + /// The origin. |
| 95 | + origin: RelatedOrigin, |
| 96 | + /// The client. |
| 97 | + client: ClientId, |
| 98 | + }, |
53 | 99 | /// A secret could not be generated. |
54 | 100 | #[error("could not generate a client secret")] |
55 | 101 | Crypto(#[from] CryptoError), |
@@ -321,6 +367,115 @@ impl ClientRegistry { |
321 | 367 | Ok(added || capped) |
322 | 368 | } |
323 | 369 |
|
| 370 | + /// Approves `origin` for client `id`'s pages to sign users in with passkeys |
| 371 | + /// for `rp_id`, auth's `WebAuthn` RP ID. The client must be trusted and have a |
| 372 | + /// redirect URI on the origin, and approved origins must stay within the |
| 373 | + /// domains every browser honours. |
| 374 | + /// |
| 375 | + /// # Errors |
| 376 | + /// Fails if the client does not exist or breaks a rule above, the origin is |
| 377 | + /// already approved or the database fails. |
| 378 | + pub async fn approve_origin( |
| 379 | + &self, |
| 380 | + operator: &Operator, |
| 381 | + id: &ClientId, |
| 382 | + origin: &RelatedOrigin, |
| 383 | + rp_id: &str, |
| 384 | + ) -> Result<(), ClientRegistryError> { |
| 385 | + let mut tx = self.database.begin().await?; |
| 386 | + let client = Self::locked(&mut tx, id).await?; |
| 387 | + if !client.trusted { |
| 388 | + return Err(ClientRegistryError::Untrusted(client.id)); |
| 389 | + } |
| 390 | + let on_origin = |uri: &String| { |
| 391 | + Url::parse(uri).is_ok_and(|uri| uri.origin().ascii_serialization() == origin.as_str()) |
| 392 | + }; |
| 393 | + if !client.redirect_uris.iter().any(on_origin) { |
| 394 | + return Err(ClientRegistryError::NotRedirectOrigin { |
| 395 | + origin: origin.clone(), |
| 396 | + client: client.id, |
| 397 | + }); |
| 398 | + } |
| 399 | + tx.oidc().lock_related_origins().await?; |
| 400 | + let mut domains = BTreeSet::new(); |
| 401 | + for approved in tx.oidc().related_origins().await? { |
| 402 | + domains.extend(Self::domain(&approved.origin, rp_id)?); |
| 403 | + } |
| 404 | + domains.extend(Self::domain(origin, rp_id)?); |
| 405 | + if domains.len() > MAX_RELATED_ORIGIN_DOMAINS { |
| 406 | + return Err(ClientRegistryError::TooManyDomains( |
| 407 | + domains.into_iter().collect(), |
| 408 | + )); |
| 409 | + } |
| 410 | + tx.oidc() |
| 411 | + .approve_related_origin(id, origin) |
| 412 | + .await |
| 413 | + .map_err(|error| match error { |
| 414 | + DatabaseError::Conflict(_) => ClientRegistryError::OriginApproved { |
| 415 | + origin: origin.clone(), |
| 416 | + source: error, |
| 417 | + }, |
| 418 | + error => error.into(), |
| 419 | + })?; |
| 420 | + tx.record( |
| 421 | + &operator.context(), |
| 422 | + None, |
| 423 | + DomainEvent::RelatedOriginApproved { |
| 424 | + client_id: id.clone(), |
| 425 | + origin: origin.clone(), |
| 426 | + }, |
| 427 | + ) |
| 428 | + .await?; |
| 429 | + tx.commit().await?; |
| 430 | + Ok(()) |
| 431 | + } |
| 432 | + |
| 433 | + /// Removes `origin` from client `id`'s approved origins. Ceremonies its pages |
| 434 | + /// already started can no longer finish. |
| 435 | + /// |
| 436 | + /// # Errors |
| 437 | + /// Fails if the origin is not approved for the client or the database fails. |
| 438 | + pub async fn remove_origin( |
| 439 | + &self, |
| 440 | + operator: &Operator, |
| 441 | + id: &ClientId, |
| 442 | + origin: &RelatedOrigin, |
| 443 | + ) -> Result<(), ClientRegistryError> { |
| 444 | + let mut tx = self.database.begin().await?; |
| 445 | + if !tx.oidc().remove_related_origin(id, origin).await? { |
| 446 | + return Err(ClientRegistryError::OriginNotApproved { |
| 447 | + origin: origin.clone(), |
| 448 | + client: id.clone(), |
| 449 | + }); |
| 450 | + } |
| 451 | + tx.record( |
| 452 | + &operator.context(), |
| 453 | + None, |
| 454 | + DomainEvent::RelatedOriginRemoved { |
| 455 | + client_id: id.clone(), |
| 456 | + origin: origin.clone(), |
| 457 | + }, |
| 458 | + ) |
| 459 | + .await?; |
| 460 | + tx.commit().await?; |
| 461 | + Ok(()) |
| 462 | + } |
| 463 | + |
| 464 | + /// The domain label browsers count for `origin` against their related-origins |
| 465 | + /// limit (`WebAuthn` Level 3 §5.11.1): its registrable domain's first label, e.g. |
| 466 | + /// `example` for `https://app.example.co.uk`. Browsers count every listed origin, |
| 467 | + /// those within `rp_id` included. They skip one without a registrable domain, |
| 468 | + /// such as `localhost`, so that works only within `rp_id`, which browsers allow |
| 469 | + /// without the list, and counts nothing. |
| 470 | + fn domain(origin: &RelatedOrigin, rp_id: &str) -> Result<Option<String>, ClientRegistryError> { |
| 471 | + let host = origin.host(); |
| 472 | + match psl::domain_str(host).and_then(|domain| domain.split('.').next()) { |
| 473 | + Some(label) => Ok(Some(label.to_owned())), |
| 474 | + None if host == rp_id || host.ends_with(&format!(".{rp_id}")) => Ok(None), |
| 475 | + None => Err(ClientRegistryError::PublicSuffix(origin.clone())), |
| 476 | + } |
| 477 | + } |
| 478 | + |
324 | 479 | /// Locks client `id` for the rest of `tx`, then reads it. |
325 | 480 | async fn locked( |
326 | 481 | tx: &mut DatabaseTransaction<'_>, |
|
0 commit comments