//! A discoverable client's ID and document, checked as //! `spec/OAUTH-CLIENTS.md` says under "Client IDs" and "What it must say": //! each field's type, then the checks in the page's order, the first that //! fails refusing the document. use super::super::{RESPONSE_TYPES, client, scope}; use super::{Failure, assertion}; use serde_json::{Map, Value as Json}; use std::net::{Ipv4Addr, Ipv6Addr}; use url::{Host, Url}; const META: &str = "invalid_client_metadata"; /// A document failing a numbered check: `invalid_client_metadata`, or for /// some redirect URIs `invalid_redirect_uri`. const REFUSED: Failure = Failure::Check(META); const REDIRECT: Failure = Failure::Check("invalid_redirect_uri"); const GRANT_TYPES: &[&str] = &[ "authorization_code", "implicit", "refresh_token", "password", "client_credentials", "urn:ietf:params:oauth:grant-type:jwt-bearer", "urn:ietf:params:oauth:grant-type:saml2-bearer", ]; const AUTH_METHODS: &[&str] = &[ "client_secret_basic", "client_secret_jwt", "client_secret_post", "none", "private_key_jwt", "self_signed_tls_client_auth", "tls_client_auth", ]; /// A domain name no client on the internet can own: fewer than two labels, /// or a last label of `test`, `local`, `localhost`, `invalid` or `example`. pub(super) fn local_name(host: &str) -> bool { let labels: Vec<&str> = host.split('.').collect(); let last = labels.last().copied().unwrap_or_default(); labels.len() < 2 || ["test", "local", "localhost", "invalid", "example"] .iter() .any(|l| last.eq_ignore_ascii_case(l)) } /// A host name as an https client ID or redirect URI may have: not /// `localhost`, at least two labels, not ending in `.local`. fn domain_ok(d: &str) -> bool { let labels: Vec<&str> = d.split('.').collect(); !d.eq_ignore_ascii_case("localhost") && labels.len() >= 2 && labels.iter().all(|l| !l.is_empty()) && !d.to_ascii_lowercase().ends_with(".local") } /// A discoverable client's ID, parsed, or the error that refuses it. pub(super) fn client_id(id: &str) -> Result { let after = id.strip_prefix("https://").ok_or(META)?; if id.contains('#') { return Err(META); } let u = Url::parse(id).map_err(|_| META)?; if !u.username().is_empty() || u.password().is_some() { return Err(META); } let authority = &after[..after.find(['/', '?', '\\']).unwrap_or(after.len())]; let rest = &after[authority.len()..]; let raw_path = &rest[..rest.find('?').unwrap_or(rest.len())]; if raw_path != u.path() || u.path() == "/" || u.path().ends_with('/') { return Err(META); } let Some(Host::Domain(host)) = u.host() else { return Err(META); }; if !domain_ok(host) || authority.contains(':') || u.port().is_some() { return Err(META); } if local_name(host) { return Err("invalid_client_id"); } Ok(u) } /// A redirect URI's form. pub(super) enum Form { Https(Url), Loopback(Url), /// Its scheme. PrivateUse(String), } /// An https URL whose host is not `localhost`, `127.0.0.1` or `[::1]`, and, /// if a domain name, has at least two labels and does not end in `.local`. fn https_url(uri: &str) -> Option { if !uri.starts_with("https://") { return None; } let u = Url::parse(uri).ok()?; let ok = match u.host()? { Host::Domain(d) => domain_ok(d), Host::Ipv4(a) => a != Ipv4Addr::LOCALHOST, Host::Ipv6(a) => a != Ipv6Addr::LOCALHOST, }; ok.then_some(u) } pub(super) fn form(uri: &str) -> Option
{ if let Some(u) = https_url(uri) { return Some(Form::Https(u)); } if uri.starts_with("http://") { return Url::parse(uri) .ok() .filter(client::loopback) .map(Form::Loopback); } // A private-use URI: `