use std::fmt;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::time::Instant;
use url::Url;
use super::OAuthClient;
use super::transport::{BodyFailure, TransportFailure, json_get, read_within, send_within};
use crate::error::{Error, ErrorCode};
use crate::http::StatusCode;
use crate::security::{is_localhost, origin_of};
pub const LAUNCHPAD_ISSUER: &str = "https://launchpad.37signals.com";
const WELL_KNOWN_AS: &str = "/.well-known/oauth-authorization-server";
const WELL_KNOWN_RESOURCE: &str = "/.well-known/oauth-protected-resource";
const LIST_FIELDS: &[&str] = &[
"grant_types_supported",
"scopes_supported",
"code_challenge_methods_supported",
];
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ServerMetadata {
pub issuer: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub authorization_endpoint: Option<String>,
pub token_endpoint: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device_authorization_endpoint: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub registration_endpoint: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub grant_types_supported: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scopes_supported: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code_challenge_methods_supported: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ProtectedResourceMetadata {
pub resource: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub authorization_servers: Option<Vec<String>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FallbackReason {
ResourceDiscoveryFailed,
NoAsAdvertised,
}
impl FallbackReason {
pub fn as_str(&self) -> &'static str {
match self {
FallbackReason::ResourceDiscoveryFailed => "resource_discovery_failed",
FallbackReason::NoAsAdvertised => "no_as_advertised",
}
}
}
impl fmt::Display for FallbackReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiscoveryOutcome {
Selected(ServerMetadata),
Fallback(FallbackReason),
}
impl DiscoveryOutcome {
pub fn selected(&self) -> Option<&ServerMetadata> {
match self {
DiscoveryOutcome::Selected(metadata) => Some(metadata),
DiscoveryOutcome::Fallback(_) => None,
}
}
pub fn fallback(&self) -> Option<FallbackReason> {
match self {
DiscoveryOutcome::Selected(_) => None,
DiscoveryOutcome::Fallback(reason) => Some(*reason),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SelectionFailure {
AmbiguousIssuers,
ExpectedIssuerUnavailable,
InvalidIssuerOrigin,
AsFetchFailed,
IssuerMismatch,
CapabilityUnavailable,
}
impl SelectionFailure {
pub fn as_str(&self) -> &'static str {
match self {
SelectionFailure::AmbiguousIssuers => "ambiguous_issuers",
SelectionFailure::ExpectedIssuerUnavailable => "expected_issuer_unavailable",
SelectionFailure::InvalidIssuerOrigin => "invalid_issuer_origin",
SelectionFailure::AsFetchFailed => "as_fetch_failed",
SelectionFailure::IssuerMismatch => "issuer_mismatch",
SelectionFailure::CapabilityUnavailable => "capability_unavailable",
}
}
pub fn error_code(&self) -> ErrorCode {
match self {
SelectionFailure::CapabilityUnavailable => ErrorCode::Validation,
_ => ErrorCode::ApiError,
}
}
}
impl fmt::Display for SelectionFailure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug)]
pub struct SelectionError {
reason: SelectionFailure,
cause: Option<Error>,
}
impl SelectionError {
pub fn reason(&self) -> SelectionFailure {
self.reason
}
pub fn cause(&self) -> Option<&Error> {
self.cause.as_ref()
}
pub fn of(error: &Error) -> Option<&SelectionError> {
std::error::Error::source(error).and_then(|source| source.downcast_ref())
}
pub(super) fn into_error(
reason: SelectionFailure,
message: String,
cause: Option<Error>,
) -> Error {
let mut error = Error::new(reason.error_code(), message);
if let Some(cause) = &cause {
if let Some(status) = cause.http_status() {
error = error.with_status(status);
}
if let Some(request_id) = cause.request_id() {
error = error.with_request_id(request_id);
}
error = error.retryable(cause.is_retryable());
}
error.with_source(SelectionError { reason, cause })
}
}
impl fmt::Display for SelectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.reason.as_str())
}
}
impl std::error::Error for SelectionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.cause
.as_ref()
.map(|cause| cause as &(dyn std::error::Error + 'static))
}
}
pub fn require_origin_root(raw: &str) -> Result<String, Error> {
let shown = origin_of(raw);
let refuse = |what: &str| Error::usage(format!("{what}: {shown}"));
if raw
.chars()
.any(|character| character <= ' ' || character == '\\')
{
return Err(refuse("origin contains invalid characters"));
}
let Some((_, after_scheme)) = raw.split_once("://") else {
return Err(refuse("origin must be an absolute URL with an authority"));
};
if raw.contains(['?', '#']) {
return Err(refuse("origin must not carry a query or fragment"));
}
let (authority, path) = after_scheme
.split_once('/')
.map_or((after_scheme, ""), |(authority, rest)| (authority, rest));
if !path.is_empty() {
return Err(refuse("origin must not carry a path"));
}
if authority.is_empty() {
return Err(refuse("origin has no host"));
}
if authority.contains('@') {
return Err(refuse("origin must not carry userinfo"));
}
let port = if let Some(rest) = authority.strip_prefix('[') {
rest.split_once(']')
.and_then(|(_, after)| after.strip_prefix(':'))
} else {
authority.rsplit_once(':').map(|(_, port)| port)
};
if let Some(port) = port {
let valid = !port.is_empty()
&& port.bytes().all(|byte| byte.is_ascii_digit())
&& port
.parse::<u32>()
.is_ok_and(|number| (1..=65535).contains(&number));
if !valid {
return Err(refuse("origin has an invalid port"));
}
}
let url = Url::parse(raw).map_err(|_| refuse("origin is not a valid URL"))?;
let Some(host) = url.host_str() else {
return Err(refuse("origin has no host"));
};
let secure = url.scheme() == "https" || (url.scheme() == "http" && is_localhost(&url));
if !secure {
return Err(refuse("origin must use HTTPS, or HTTP on localhost"));
}
let port = url
.port()
.map(|port| format!(":{port}"))
.unwrap_or_default();
Ok(format!("{}://{host}{port}", url.scheme()))
}
enum MetadataFailure {
IssuerMismatch(Error),
Other(Error),
}
impl MetadataFailure {
fn into_error(self) -> Error {
match self {
MetadataFailure::IssuerMismatch(error) | MetadataFailure::Other(error) => error,
}
}
}
impl OAuthClient {
pub async fn discover(&self, issuer: &str) -> Result<ServerMetadata, Error> {
let origin = require_origin_root(issuer)?;
self.fetch_server_metadata(&origin, issuer)
.await
.map_err(MetadataFailure::into_error)
}
pub async fn discover_launchpad(&self) -> Result<ServerMetadata, Error> {
let issuer = self.launchpad_issuer.clone();
self.discover(&issuer).await
}
pub async fn discover_protected_resource(
&self,
resource: &str,
) -> Result<ProtectedResourceMetadata, Error> {
let origin = require_origin_root(resource)?;
self.fetch_protected_resource(&origin, resource).await
}
pub async fn discover_from_resource(
&self,
resource: &str,
expected_issuer: Option<&str>,
) -> Result<DiscoveryOutcome, Error> {
let origin = require_origin_root(resource)?;
let Ok(metadata) = self.fetch_protected_resource(&origin, resource).await else {
return Ok(DiscoveryOutcome::Fallback(
FallbackReason::ResourceDiscoveryFailed,
));
};
let advertised = metadata.authorization_servers.unwrap_or_default();
let selected = if let Some(expected) = expected_issuer {
match advertised.iter().find(|issuer| *issuer == expected) {
Some(issuer) => issuer.clone(),
None => {
return Err(SelectionError::into_error(
SelectionFailure::ExpectedIssuerUnavailable,
format!(
"expected issuer {} is not advertised by the resource",
origin_of(expected)
),
None,
));
}
}
} else {
let mut seen = std::collections::HashSet::new();
let mut candidates: Vec<&String> = Vec::new();
for issuer in &advertised {
if !self.is_launchpad_issuer(issuer) && seen.insert(issuer) {
candidates.push(issuer);
}
}
match candidates.as_slice() {
[] => return Ok(DiscoveryOutcome::Fallback(FallbackReason::NoAsAdvertised)),
[only] => (*only).clone(),
many => {
let named: Vec<String> = many.iter().map(|issuer| origin_of(issuer)).collect();
return Err(SelectionError::into_error(
SelectionFailure::AmbiguousIssuers,
format!(
"the resource advertises {} non-Launchpad issuers ({}); name the expected issuer",
many.len(),
named.join(", ")
),
None,
));
}
}
};
let issuer_origin = match require_origin_root(&selected) {
Ok(issuer_origin) => issuer_origin,
Err(cause) => {
return Err(SelectionError::into_error(
SelectionFailure::InvalidIssuerOrigin,
format!(
"advertised issuer {} is not a valid origin root",
origin_of(&selected)
),
Some(cause),
));
}
};
match self.fetch_server_metadata(&issuer_origin, &selected).await {
Ok(metadata) => Ok(DiscoveryOutcome::Selected(metadata)),
Err(MetadataFailure::IssuerMismatch(cause)) => Err(SelectionError::into_error(
SelectionFailure::IssuerMismatch,
format!("metadata at {issuer_origin} names another issuer than the advertised one"),
Some(cause),
)),
Err(MetadataFailure::Other(cause)) => Err(SelectionError::into_error(
SelectionFailure::AsFetchFailed,
format!(
"authorization server metadata could not be read from the committed issuer {issuer_origin}: {}",
cause.message()
),
Some(cause),
)),
}
}
fn is_launchpad_issuer(&self, issuer: &str) -> bool {
match (
require_origin_root(issuer),
require_origin_root(&self.launchpad_issuer),
) {
(Ok(candidate), Ok(launchpad)) => candidate == launchpad,
_ => false,
}
}
async fn fetch_server_metadata(
&self,
origin: &str,
bind_issuer: &str,
) -> Result<ServerMetadata, MetadataFailure> {
let (status, body) = self
.fetch_document(&format!("{origin}{WELL_KNOWN_AS}"))
.await
.map_err(MetadataFailure::Other)?;
parse_server_metadata(&body, bind_issuer, status)
}
async fn fetch_protected_resource(
&self,
origin: &str,
bind_resource: &str,
) -> Result<ProtectedResourceMetadata, Error> {
let (status, body) = self
.fetch_document(&format!("{origin}{WELL_KNOWN_RESOURCE}"))
.await?;
parse_protected_resource(&body, bind_resource, status)
}
async fn fetch_document(&self, url: &str) -> Result<(StatusCode, bytes::Bytes), Error> {
let deadline = Instant::now() + self.request_timeout;
let request = json_get(url)?;
let response = match send_within(self.http(), deadline, request).await {
Ok(response) => response,
Err(TransportFailure::TimedOut) => {
return Err(Error::network_at(&origin_of(url)).with_hint("the request timed out"));
}
Err(TransportFailure::Failed(error)) => return Err(error),
};
let status = response.status();
if !status.is_success() {
return Err(Error::new(
ErrorCode::ApiError,
format!(
"OAuth discovery at {} failed with status {}",
origin_of(url),
status.as_u16()
),
)
.with_status(status.as_u16())
.retryable(status.is_server_error()));
}
match read_within(deadline, response.into_body(), status).await {
Ok(body) => Ok((status, body)),
Err(BodyFailure::TooLarge(error) | BodyFailure::Failed(error)) => Err(error),
Err(BodyFailure::TimedOut) => {
Err(Error::network_at(&origin_of(url)).with_hint("the response timed out"))
}
}
}
}
fn invalid_metadata(status: StatusCode, detail: &str) -> Error {
Error::malformed_response(detail).with_status(status.as_u16())
}
fn parse_server_metadata(
body: &[u8],
bind_issuer: &str,
status: StatusCode,
) -> Result<ServerMetadata, MetadataFailure> {
let document = parse_object(body, status, "authorization server metadata")
.map_err(MetadataFailure::Other)?;
let issuer = match document.get("issuer").and_then(Value::as_str) {
Some(issuer) if !issuer.is_empty() => issuer,
_ => {
return Err(MetadataFailure::Other(invalid_metadata(
status,
"authorization server metadata has no issuer",
)));
}
};
if issuer != bind_issuer {
return Err(MetadataFailure::IssuerMismatch(invalid_metadata(
status,
&format!(
"authorization server metadata issuer {} does not equal the issuer it was read for, {}",
origin_of(issuer),
origin_of(bind_issuer)
),
)));
}
for (key, value) in &document {
if key.ends_with("_endpoint") && value.as_str().is_none_or(str::is_empty) {
return Err(MetadataFailure::Other(invalid_metadata(
status,
&format!("authorization server metadata {key} must be a non-empty string"),
)));
}
}
if !document.contains_key("token_endpoint") {
return Err(MetadataFailure::Other(invalid_metadata(
status,
"authorization server metadata has no token_endpoint",
)));
}
for key in LIST_FIELDS {
if document
.get(*key)
.is_some_and(|value| !is_string_array(value))
{
return Err(MetadataFailure::Other(invalid_metadata(
status,
&format!("authorization server metadata {key} must be an array of strings"),
)));
}
}
serde_json::from_value(Value::Object(document)).map_err(|error| {
MetadataFailure::Other(invalid_metadata(
status,
&format!("authorization server metadata: {error}"),
))
})
}
fn parse_protected_resource(
body: &[u8],
bind_resource: &str,
status: StatusCode,
) -> Result<ProtectedResourceMetadata, Error> {
let document = parse_object(body, status, "resource metadata")?;
let resource = match document.get("resource").and_then(Value::as_str) {
Some(resource) if !resource.is_empty() => resource,
_ => {
return Err(invalid_metadata(
status,
"resource metadata has no resource",
));
}
};
if resource != bind_resource {
return Err(invalid_metadata(
status,
&format!(
"resource metadata resource {} does not equal the resource it was read for, {}",
origin_of(resource),
origin_of(bind_resource)
),
));
}
let authorization_servers = match document.get("authorization_servers") {
None => None,
Some(value) if is_string_array(value) => serde_json::from_value(value.clone()).ok(),
Some(_) => {
return Err(invalid_metadata(
status,
"resource metadata authorization_servers must be an array of strings",
));
}
};
Ok(ProtectedResourceMetadata {
resource: resource.to_string(),
authorization_servers,
})
}
fn parse_object(
body: &[u8],
status: StatusCode,
what: &str,
) -> Result<serde_json::Map<String, Value>, Error> {
match serde_json::from_slice::<Value>(body) {
Ok(Value::Object(document)) => Ok(document),
Ok(_) => Err(invalid_metadata(
status,
&format!("{what} is not a JSON object"),
)),
Err(_) => Err(invalid_metadata(status, &format!("{what} is not JSON"))),
}
}
fn is_string_array(value: &Value) -> bool {
value
.as_array()
.is_some_and(|items| items.iter().all(Value::is_string))
}
#[cfg(all(test, feature = "reqwest"))]
mod tests {
use super::*;
#[test]
fn origin_roots_are_normalized() {
for (raw, want) in [
("https://api.example.com", "https://api.example.com"),
("https://api.example.com/", "https://api.example.com"),
(
"https://api.example.com:8443",
"https://api.example.com:8443",
),
("https://api.example.com:443", "https://api.example.com"),
("https://api.example.com:000443", "https://api.example.com"),
("https://api.example.com:0080", "https://api.example.com:80"),
("HTTPS://API.example.com", "https://api.example.com"),
("http://localhost:3000", "http://localhost:3000"),
("http://[::1]:3000", "http://[::1]:3000"),
("http://127.0.0.1:9999", "http://127.0.0.1:9999"),
] {
assert_eq!(require_origin_root(raw).unwrap(), want, "{raw}");
}
}
#[test]
fn origin_roots_are_refused() {
for raw in [
"http://api.example.com",
"https://api.example.com:notaport",
"https://h:99999",
"https://h:0",
"https://h:+1",
"https://h:",
"http://[::1]:notaport",
"http://[::1]:",
"https://api.example.com/tenant/1",
"https://api.example.com/a/..",
"https://api.example.com?x=1",
"https://api.example.com?",
"https://api.example.com#frag",
"https://api.example.com#",
"https://user:pass@api.example.com",
"https://@api.example.com",
"https:api.example.com",
"https:\\\\api.example.com",
"https://api.example.com\n",
"https:///",
"ftp://api.example.com",
"not a url",
"",
] {
let error = require_origin_root(raw).unwrap_err();
assert_eq!(error.code(), ErrorCode::Usage, "{raw:?}");
}
}
#[test]
fn a_refused_origin_is_named_by_its_origin_only() {
let error = require_origin_root("https://api.example.com/x?token=secret").unwrap_err();
assert!(!error.message().contains("secret"), "{}", error.message());
}
#[test]
fn server_metadata_keeps_absent_endpoints_absent() {
let body = br#"{"issuer":"https://as.example","token_endpoint":"https://as.example/t"}"#;
let metadata = parse_server_metadata(body, "https://as.example", StatusCode::OK)
.map_err(MetadataFailure::into_error)
.unwrap();
assert_eq!(metadata.authorization_endpoint, None);
assert_eq!(metadata.grant_types_supported, None);
assert_eq!(metadata.token_endpoint, "https://as.example/t");
}
#[test]
fn server_metadata_refuses_a_null_list() {
let body =
br#"{"issuer":"https://as.example","token_endpoint":"https://as.example/t","scopes_supported":null}"#;
let error = parse_server_metadata(body, "https://as.example", StatusCode::OK)
.map_err(MetadataFailure::into_error)
.unwrap_err();
assert_eq!(error.code(), ErrorCode::ApiError);
assert_eq!(error.http_status(), Some(200));
}
#[test]
fn a_selection_error_is_read_back_from_the_source_chain() {
let cause = Error::new(ErrorCode::ApiError, "boom")
.with_status(503)
.retryable(true);
let error = SelectionError::into_error(
SelectionFailure::AsFetchFailed,
"fetch failed".to_string(),
Some(cause),
);
assert_eq!(error.code(), ErrorCode::ApiError);
assert_eq!(error.http_status(), Some(503));
assert!(error.is_retryable());
let selection = SelectionError::of(&error).unwrap();
assert_eq!(selection.reason(), SelectionFailure::AsFetchFailed);
assert_eq!(selection.cause().unwrap().message(), "boom");
assert!(SelectionError::of(&Error::usage("plain")).is_none());
}
}
#[cfg(all(test, feature = "reqwest"))]
mod conformance {
use std::net::TcpListener;
use std::path::PathBuf;
use serde::Deserialize;
use serde_json::{Value, json};
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use super::*;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Exchange {
origin: Option<String>,
status: Option<u16>,
#[serde(default)]
transport_error: bool,
body: Option<Value>,
#[serde(default)]
oversized: bool,
redirect_to: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Expect {
outcome: String,
selected_issuer: Option<String>,
fallback_reason: Option<String>,
error: Option<String>,
launchpad_contacted: Option<bool>,
error_category: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Fixture {
name: String,
operation: String,
resource_origin: Option<String>,
issuer_origin: Option<String>,
expected_issuer: Option<String>,
hop1: Option<Exchange>,
hop2: Option<Exchange>,
expect: Expect,
}
fn fixture_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../conformance/oauth/fixtures")
}
fn fixture_paths() -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = std::fs::read_dir(fixture_dir())
.expect("the fixture directory exists")
.map(|entry| entry.unwrap().path())
.filter(|path| {
path.extension()
.is_some_and(|extension| extension == "json")
})
.collect();
paths.sort();
assert!(!paths.is_empty(), "no fixtures found");
paths
}
fn dead_origin() -> String {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
drop(listener);
format!("http://{address}")
}
fn template(exchange: &Exchange) -> ResponseTemplate {
if exchange.oversized {
return ResponseTemplate::new(200)
.insert_header("content-type", "application/json")
.set_body_bytes(vec![b'x'; super::super::transport::MAX_RESPONSE_BYTES + 1]);
}
if let Some(location) = &exchange.redirect_to {
return ResponseTemplate::new(exchange.status.unwrap_or(302))
.insert_header("location", location.as_str());
}
let template = ResponseTemplate::new(exchange.status.unwrap_or(200));
match &exchange.body {
Some(body) => template.set_body_json(body),
None => template,
}
}
async fn mount(server: &MockServer, well_known: &str, exchange: &Exchange) {
Mock::given(method("GET"))
.and(path(well_known))
.respond_with(template(exchange))
.mount(server)
.await;
}
async fn requests_seen(server: &MockServer) -> usize {
server.received_requests().await.unwrap_or_default().len()
}
fn category(name: &str) -> ErrorCode {
match name {
"usage" => ErrorCode::Usage,
"validation" => ErrorCode::Validation,
"api_error" => ErrorCode::ApiError,
"network" => ErrorCode::Network,
"auth_required" => ErrorCode::AuthRequired,
other => panic!("unknown error category {other}"),
}
}
fn failure(name: &str) -> Option<SelectionFailure> {
Some(match name {
"ambiguous_issuers" => SelectionFailure::AmbiguousIssuers,
"expected_issuer_unavailable" => SelectionFailure::ExpectedIssuerUnavailable,
"invalid_issuer_origin" => SelectionFailure::InvalidIssuerOrigin,
"as_fetch_failed" => SelectionFailure::AsFetchFailed,
"issuer_mismatch" => SelectionFailure::IssuerMismatch,
"capability_unavailable" => SelectionFailure::CapabilityUnavailable,
_ => return None,
})
}
struct Scenario {
fixture: Fixture,
client: OAuthClient,
launchpad: MockServer,
result: Result<Option<DiscoveryOutcome>, Error>,
}
async fn run(fixture_path: &PathBuf) -> Option<Scenario> {
let raw = std::fs::read_to_string(fixture_path).unwrap();
let transport_failure = raw.contains("\"transportError\": true");
let resource = MockServer::start().await;
let bc5 = MockServer::start().await;
let issuer = MockServer::start().await;
let launchpad = MockServer::start().await;
let resource_origin = if transport_failure {
dead_origin()
} else {
resource.uri()
};
let substituted = raw
.replace("{{RESOURCE_ORIGIN}}", &resource_origin)
.replace("{{BC5_ISSUER}}", &bc5.uri())
.replace("{{ISSUER_ORIGIN}}", &issuer.uri())
.replace("{{LAUNCHPAD_ORIGIN}}", &launchpad.uri());
let fixture: Fixture = serde_json::from_str(&substituted).unwrap();
let name = fixture.name.as_str();
Mock::given(method("GET"))
.and(path(WELL_KNOWN_AS))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"issuer": launchpad.uri(),
"authorization_endpoint": format!("{}/authorization/new", launchpad.uri()),
"token_endpoint": format!("{}/authorization/token", launchpad.uri()),
})))
.mount(&launchpad)
.await;
if let Some(origin) = fixture
.resource_origin
.as_deref()
.filter(|origin| origin.contains('['))
&& fixture.expect.outcome == "selected"
{
assert_eq!(
require_origin_root(origin).unwrap(),
fixture.expect.selected_issuer.as_deref().unwrap(),
"{name}"
);
return None;
}
if let Some(hop1) = &fixture.hop1
&& !hop1.transport_error
{
mount(&resource, WELL_KNOWN_RESOURCE, hop1).await;
}
if let Some(hop2) = &fixture.hop2 {
let server = match hop2.origin.as_deref() {
Some(origin) if origin == bc5.uri() => &bc5,
Some(origin) if origin == issuer.uri() => &issuer,
other => panic!("{name}: hop2 origin {other:?} matches no role server"),
};
mount(server, WELL_KNOWN_AS, hop2).await;
}
let client = OAuthClient::shipped()
.unwrap()
.with_launchpad_issuer(launchpad.uri());
let result = match fixture.operation.as_str() {
"discoverFromResource" => client
.discover_from_resource(
fixture.resource_origin.as_deref().unwrap(),
fixture.expected_issuer.as_deref(),
)
.await
.map(Some),
"discoverProtectedResource" => client
.discover_protected_resource(fixture.resource_origin.as_deref().unwrap())
.await
.map(|metadata| {
if let Some(want) = &fixture.expect.selected_issuer {
assert_eq!(&metadata.resource, want, "{name}");
}
None
}),
"discover" => client
.discover(fixture.issuer_origin.as_deref().unwrap())
.await
.map(|metadata| Some(DiscoveryOutcome::Selected(metadata))),
other => panic!("{name}: unknown operation {other}"),
};
Some(Scenario {
fixture,
client,
launchpad,
result,
})
}
async fn check(scenario: Scenario) {
let Scenario {
fixture,
client,
launchpad,
result,
} = scenario;
let name = fixture.name.as_str();
match fixture.expect.outcome.as_str() {
"raise" => {
let error = result
.err()
.unwrap_or_else(|| panic!("{name}: expected an error"));
if let Some(category_name) = &fixture.expect.error_category {
assert_eq!(error.code(), category(category_name), "{name}: {error:?}");
}
match fixture.expect.error.as_deref().unwrap() {
"usage" => assert_eq!(error.code(), ErrorCode::Usage, "{name}"),
"invalid_metadata" | "api_error" => {
assert_eq!(error.code(), ErrorCode::ApiError, "{name}: {error:?}");
}
typed => {
let selection = SelectionError::of(&error)
.unwrap_or_else(|| panic!("{name}: no selection failure on {error:?}"));
assert_eq!(selection.reason(), failure(typed).unwrap(), "{name}");
}
}
}
"fallback" => {
let outcome = result
.unwrap_or_else(|error| panic!("{name}: expected fallback, got {error:?}"))
.unwrap();
assert_eq!(
outcome.fallback().map(|reason| reason.as_str()),
fixture.expect.fallback_reason.as_deref(),
"{name}"
);
if fixture.expect.launchpad_contacted == Some(true) {
client.discover_launchpad().await.unwrap();
assert!(
requests_seen(&launchpad).await > 0,
"{name}: Launchpad not contacted"
);
}
}
"selected" => {
let outcome = result
.unwrap_or_else(|error| panic!("{name}: expected selected, got {error:?}"));
if let Some(outcome) = outcome {
let metadata = outcome
.selected()
.unwrap_or_else(|| panic!("{name}: fell back: {outcome:?}"));
if let Some(want) = &fixture.expect.selected_issuer {
assert_eq!(&metadata.issuer, want, "{name}");
}
}
}
other => panic!("{name}: unknown outcome {other}"),
}
if fixture.expect.launchpad_contacted == Some(false) {
assert_eq!(
requests_seen(&launchpad).await,
0,
"{name}: Launchpad was contacted"
);
}
}
#[tokio::test]
async fn every_discovery_fixture_holds() {
for path in fixture_paths() {
if let Some(scenario) = run(&path).await {
check(scenario).await;
}
}
}
}