use crate::core::jwt::validator::JwtValidationError;
use http::header::ToStrError;
use snafu::prelude::*;
use crate::{
TokenType,
error::{ToRfc6750Error, TokenErrorCode, TokenValidationError},
validator::{
binding::{DPoPBindingError, MtlsBindingError},
extract::TokenExtractError,
},
};
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(super)))]
pub enum TokenBindingError {
MissingDPoPHeader,
DPoPHeaderNotString {
source: ToStrError,
},
#[snafu(display("Token is DPoP-bound but was presented as Bearer"))]
DpopRequiredForBoundToken,
#[snafu(display("DPoP-bound tokens are required"))]
DpopRequired,
#[snafu(display("Unsupported cnf confirmation method: {method}"))]
UnsupportedCnfMethod {
method: &'static str,
},
DPoPBinding {
source: DPoPBindingError,
},
MtlsBinding {
source: MtlsBindingError,
},
}
impl ToRfc6750Error for TokenBindingError {
fn attempted_scheme(&self) -> Option<TokenType> {
match self {
Self::MissingDPoPHeader
| Self::DPoPHeaderNotString { .. }
| Self::DPoPBinding { .. } => Some(TokenType::DPoP),
Self::DpopRequiredForBoundToken
| Self::DpopRequired
| Self::UnsupportedCnfMethod { .. }
| Self::MtlsBinding { .. } => None,
}
}
fn token_error(&self) -> TokenValidationError {
match self {
Self::MissingDPoPHeader | Self::DPoPHeaderNotString { .. } => {
TokenValidationError::Client(TokenErrorCode::InvalidRequest)
}
Self::DpopRequiredForBoundToken
| Self::DpopRequired
| Self::UnsupportedCnfMethod { .. }
| Self::MtlsBinding { .. } => {
TokenValidationError::Client(TokenErrorCode::InvalidToken)
}
Self::DPoPBinding { source } => source.token_error(),
}
}
fn error_description(&self) -> Option<String> {
match self {
Self::MissingDPoPHeader => Some("The DPoP header is missing".to_string()),
Self::DPoPHeaderNotString { .. } => {
Some("The DPoP header value is invalid".to_string())
}
Self::DpopRequiredForBoundToken => Some("The access token is DPoP-bound".to_string()),
Self::DpopRequired => Some("DPoP is required to access this resource".to_string()),
Self::UnsupportedCnfMethod { .. } => {
Some("The access token confirmation method is not supported".to_string())
}
Self::DPoPBinding { source } => source.error_description(),
Self::MtlsBinding { source } => source.error_description(),
}
}
}
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(super)))]
pub enum ValidateHeadersError {
#[snafu(display("Token presentation error"))]
Extract {
source: TokenExtractError,
},
#[snafu(display("Token binding error"))]
Binding {
token_type: TokenType,
source: TokenBindingError,
},
InvalidJwt {
token_type: TokenType,
source: JwtValidationError,
},
}
impl ToRfc6750Error for ValidateHeadersError {
fn attempted_scheme(&self) -> Option<TokenType> {
match self {
Self::Extract { source } => source.attempted_scheme(),
Self::Binding { token_type, .. } => Some(*token_type),
Self::InvalidJwt { token_type, .. } => Some(*token_type),
}
}
fn token_error(&self) -> TokenValidationError {
match self {
Self::Extract { source } => source.token_error(),
Self::Binding { source, .. } => source.token_error(),
Self::InvalidJwt { source, .. } => source.token_error(),
}
}
fn error_description(&self) -> Option<String> {
match self {
Self::Extract { source } => source.error_description(),
Self::Binding { source, .. } => source.error_description(),
Self::InvalidJwt { source, .. } => source.error_description(),
}
}
}