use http::{StatusCode, header::InvalidHeaderValue};
use snafu::Snafu;
use crate::core::{Error, ErrorKind};
pub(crate) fn registration_error(source: RegistrationError) -> Error {
let kind = match &source {
RegistrationError::JwksConflict
| RegistrationError::Serialize { .. }
| RegistrationError::AuthHeader { .. } => ErrorKind::Config,
RegistrationError::InvalidRedirectUri { .. }
| RegistrationError::InvalidClientMetadata { .. }
| RegistrationError::InvalidSoftwareStatement { .. }
| RegistrationError::UnapprovedSoftwareStatement { .. }
| RegistrationError::ServerError { .. } => ErrorKind::RequestRejected,
RegistrationError::MissingContentType
| RegistrationError::UnexpectedContentType { .. }
| RegistrationError::Deserialize { .. }
| RegistrationError::BadStatus { .. } => ErrorKind::Protocol,
};
Error::new(kind, source)
}
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum RegistrationError {
#[snafu(display("client metadata may not set both jwks and jwks_uri (RFC 7591 §2)"))]
JwksConflict,
#[snafu(display("failed to serialize client metadata"))]
Serialize {
source: serde_json::Error,
},
#[snafu(display("initial access token is not a valid HTTP header value"))]
AuthHeader {
source: InvalidHeaderValue,
},
#[snafu(display("the authorization server rejected one or more redirect URIs"))]
InvalidRedirectUri {
description: Option<String>,
},
#[snafu(display("the authorization server rejected the client metadata"))]
InvalidClientMetadata {
description: Option<String>,
},
#[snafu(display("the software statement is invalid"))]
InvalidSoftwareStatement {
description: Option<String>,
},
#[snafu(display("the software statement is not approved by the authorization server"))]
UnapprovedSoftwareStatement {
description: Option<String>,
},
#[snafu(display("the registration request was rejected: {error}"))]
ServerError {
error: String,
description: Option<String>,
},
#[snafu(display("registration response is missing the Content-Type header"))]
MissingContentType,
#[snafu(display("registration endpoint returned unexpected Content-Type: {content_type}"))]
UnexpectedContentType {
content_type: String,
},
#[snafu(display("failed to deserialize the client information response"))]
Deserialize {
source: serde_json::Error,
},
#[snafu(display("registration endpoint returned HTTP {status}"))]
BadStatus {
status: StatusCode,
body: Vec<u8>,
},
}