use crate::{SignatureVerificationError, SigningError};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::hash::Hash;
new_type![
#[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
JsonWebKeyId(String)
];
pub trait JsonWebKey: Clone + Debug + DeserializeOwned + Serialize + 'static {
type KeyUse: JsonWebKeyUse;
type SigningAlgorithm: JwsSigningAlgorithm;
fn key_id(&self) -> Option<&JsonWebKeyId>;
fn key_type(&self) -> &<Self::SigningAlgorithm as JwsSigningAlgorithm>::KeyType;
fn key_use(&self) -> Option<&Self::KeyUse>;
fn signing_alg(&self) -> JsonWebKeyAlgorithm<&Self::SigningAlgorithm>;
fn new_symmetric(key: Vec<u8>) -> Self;
fn verify_signature(
&self,
signature_alg: &Self::SigningAlgorithm,
message: &[u8],
signature: &[u8],
) -> Result<(), SignatureVerificationError>;
fn hash_bytes(&self, bytes: &[u8], alg: &Self::SigningAlgorithm) -> Result<Vec<u8>, String>;
}
#[derive(Debug)]
pub enum JsonWebKeyAlgorithm<A: Debug> {
Algorithm(A),
Unspecified,
Unsupported,
}
pub trait PrivateSigningKey {
type VerificationKey: JsonWebKey;
fn sign(
&self,
signature_alg: &<Self::VerificationKey as JsonWebKey>::SigningAlgorithm,
message: &[u8],
) -> Result<Vec<u8>, SigningError>;
fn as_verification_key(&self) -> Self::VerificationKey;
}
pub trait JsonWebKeyType:
Clone + Debug + DeserializeOwned + PartialEq + Serialize + 'static
{
}
pub trait JsonWebKeyUse: Debug + DeserializeOwned + Serialize + 'static {
fn allows_signature(&self) -> bool;
fn allows_encryption(&self) -> bool;
}
pub trait JweContentEncryptionAlgorithm:
Clone + Debug + DeserializeOwned + Serialize + 'static
{
type KeyType: JsonWebKeyType;
fn key_type(&self) -> Result<Self::KeyType, String>;
}
pub trait JweKeyManagementAlgorithm: Debug + DeserializeOwned + Serialize + 'static {
}
pub trait JwsSigningAlgorithm:
Clone + Debug + DeserializeOwned + Eq + Hash + PartialEq + Serialize + 'static
{
type KeyType: JsonWebKeyType;
fn key_type(&self) -> Option<Self::KeyType>;
fn uses_shared_secret(&self) -> bool;
fn rsa_sha_256() -> Self;
}