identity_credential/sd_jwt_vc/metadata/
issuer.rsuse identity_core::common::Url;
use identity_verification::jwk::JwkSet;
use serde::Deserialize;
use serde::Serialize;
use crate::sd_jwt_vc::Error;
use crate::sd_jwt_vc::SdJwtVc;
#[allow(unused_imports)]
use crate::sd_jwt_vc::SdJwtVcClaims;
pub const WELL_KNOWN_VC_ISSUER: &str = "/.well-known/jwt-vc-issuer";
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct IssuerMetadata {
pub issuer: Url,
#[serde(flatten)]
pub jwks: Jwks,
}
impl IssuerMetadata {
pub fn validate(&self, sd_jwt_vc: &SdJwtVc) -> Result<(), Error> {
let expected_issuer = &sd_jwt_vc.claims().iss;
let actual_issuer = &self.issuer;
if actual_issuer != expected_issuer {
Err(Error::InvalidIssuerMetadata(anyhow::anyhow!(
"expected issuer \"{expected_issuer}\", but found \"{actual_issuer}\""
)))
} else {
Ok(())
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub enum Jwks {
#[serde(rename = "jwks_uri")]
Uri(Url),
#[serde(rename = "jwks")]
Object(JwkSet),
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_URI_ISSUER_METADATA: &str = r#"
{
"issuer":"https://example.com",
"jwks_uri":"https://jwt-vc-issuer.example.org/my_public_keys.jwks"
}
"#;
const EXAMPLE_JWKS_ISSUER_METADATA: &str = r#"
{
"issuer":"https://example.com",
"jwks":{
"keys":[
{
"kid":"doc-signer-05-25-2022",
"e":"AQAB",
"n":"nj3YJwsLUFl9BmpAbkOswCNVx17Eh9wMO-_AReZwBqfaWFcfGHrZXsIV2VMCNVNU8Tpb4obUaSXcRcQ-VMsfQPJm9IzgtRdAY8NN8Xb7PEcYyklBjvTtuPbpzIaqyiUepzUXNDFuAOOkrIol3WmflPUUgMKULBN0EUd1fpOD70pRM0rlp_gg_WNUKoW1V-3keYUJoXH9NztEDm_D2MQXj9eGOJJ8yPgGL8PAZMLe2R7jb9TxOCPDED7tY_TU4nFPlxptw59A42mldEmViXsKQt60s1SLboazxFKveqXC_jpLUt22OC6GUG63p-REw-ZOr3r845z50wMuzifQrMI9bQ",
"kty":"RSA"
}
]
}
}
"#;
#[test]
fn deserializing_uri_metadata_works() {
let issuer_metadata: IssuerMetadata = serde_json::from_str(EXAMPLE_URI_ISSUER_METADATA).unwrap();
assert!(matches!(issuer_metadata.jwks, Jwks::Uri(_)));
}
#[test]
fn deserializing_jwks_metadata_works() {
let issuer_metadata: IssuerMetadata = serde_json::from_str(EXAMPLE_JWKS_ISSUER_METADATA).unwrap();
assert!(matches!(issuer_metadata.jwks, Jwks::Object { .. }));
}
}