use std::error::Error;
use std::path::PathBuf;
use super::KeyId;
use crate::cause::cause;
use crate::jose::{JwsAlgorithm, KeyType};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum KeyStoreError {
#[error("key `{0}` not found")]
NotFound(KeyId),
#[error("key `{0}` already exists")]
AlreadyExists(KeyId),
#[error("key `{key}` signs with {key_alg}, not {requested}")]
AlgorithmMismatch {
key: KeyId,
key_alg: JwsAlgorithm,
requested: JwsAlgorithm,
},
#[error("key store does not support `{0}`")]
Unsupported(&'static str),
#[error("key store does not support {0}")]
UnsupportedAlgorithm(JwsAlgorithm),
#[error("key store does not support {0:?} keys")]
UnsupportedKeyType(KeyType),
#[error("{alg} cannot be used with {key_type:?} keys")]
InvalidSpec {
key_type: KeyType,
alg: JwsAlgorithm,
},
#[error("key `{key}` is unusable: {reason}{}", cause(.source.as_ref()))]
InvalidKey {
key: KeyId,
reason: String,
source: Option<Box<dyn Error + Send + Sync + 'static>>,
},
#[error("key store file `{}`: {source}", path.display())]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error(
"key store file `{}` has mode {mode:04o}; make it private (0600) or opt in with `allow_insecure_permissions`",
path.display()
)]
InsecurePermissions {
path: PathBuf,
mode: u32,
},
#[error(
"key store file `{}` is not a usable JWK set: {reason}{}",
path.display(),
cause(.source.as_ref())
)]
InvalidKeySet {
path: PathBuf,
reason: String,
source: Option<Box<dyn Error + Send + Sync + 'static>>,
},
#[error("key store backend error: {0}")]
Backend(#[source] Box<dyn Error + Send + Sync + 'static>),
}
impl KeyStoreError {
pub fn invalid_key(key: KeyId, reason: impl Into<String>) -> Self {
Self::InvalidKey {
key,
reason: reason.into(),
source: None,
}
}
pub fn backend<E: Error + Send + Sync + 'static>(error: E) -> Self {
Self::Backend(Box::new(error))
}
}