Skip to content

Commit

Permalink
parse VC from VerifiableCredential to SpeicalizedJsonCredential to us…
Browse files Browse the repository at this point in the history
…e JsonPresentation

Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Sep 19, 2024
1 parent ae2c5c9 commit e4302e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
17 changes: 6 additions & 11 deletions src/core/response/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ impl From<IdToken> for Json {
/// See: [OpenID.VP#section-6.1-2.2](https://openid.net/specs/openid-4-verifiable-presentations-1_0.html#section-6.1-2.2)
#[derive(Debug, Clone)]
pub enum VpToken {
Single(String),
// Meant to used for URL encoding.
Single(Vec<u8>),
// Vp Token for `POST` methods?
SingleAsMap(Map<String, Json>),
// Many VP Token for `POST` methods?
Many(Vec<VpToken>),
}

Expand All @@ -59,7 +62,7 @@ impl TryFrom<Json> for VpToken {
fn try_from(value: Json) -> Result<Self, Self::Error> {
match value {
// NOTE: When parsing a Json string object, it must be base64Url encoded.
Json::String(s) => Ok(Self::Single(s)),
Json::String(s) => Ok(Self::Single(BASE64_URL_SAFE_NO_PAD.decode(s)?)),
// NOTE: When the Json is an object, it must be a map.
Json::Object(map) => Ok(Self::SingleAsMap(map)),
Json::Array(arr) => {
Expand All @@ -79,7 +82,7 @@ impl TryFrom<VpToken> for Json {

fn try_from(value: VpToken) -> Result<Self, Self::Error> {
match value {
VpToken::Single(s) => Ok(serde_json::Value::String(s)),
VpToken::Single(s) => Ok(serde_json::Value::String(BASE64_URL_SAFE_NO_PAD.encode(s))),
VpToken::SingleAsMap(map) => Ok(serde_json::Value::Object(map)),
VpToken::Many(tokens) => {
let mut arr: Vec<Json> = Vec::new();
Expand All @@ -100,14 +103,6 @@ impl TryFrom<AnyJsonPresentation> for VpToken {
}
}

impl VpToken {
pub fn as_base64_url_encoded(self) -> Result<String, Error> {
let json: Json = self.try_into()?;
let string = serde_json::to_string(&json)?;
Ok(BASE64_URL_SAFE_NO_PAD.encode(string.as_bytes()))
}
}

#[derive(Debug, Clone)]
pub struct PresentationSubmission {
raw: Json,
Expand Down
40 changes: 34 additions & 6 deletions tests/jwt_vp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use oid4vp::verifier::request_signer::P256Signer;
use ssi::claims::vc::v1::{JsonCredential, JsonPresentation};
use ssi::claims::jwt::VerifiableCredential;
use ssi::claims::vc::v1::{Context, JsonPresentation};
use ssi::dids::DIDKey;
use ssi::json_ld::iref::UriBuf;
use ssi::prelude::AnyJsonPresentation;
Expand All @@ -16,17 +17,44 @@ pub async fn create_test_verifiable_presentation() -> Result<AnyJsonPresentation

let holder_did = DIDKey::generate_url(signer.jwk())?;

println!("holder_did: {:?}", holder_did);

// Create a verifiable presentation using the `examples/vc.jwt` file
// The signer information is the holder's key, also found in the `examples/subject.jwk` file.
let verifiable_credential: JsonCredential =
let verifiable_credential: VerifiableCredential =
ssi::claims::jwt::decode_unverified(include_str!("examples/vc.jwt"))?;

let mut vp = JsonPresentation::default();
let parsed_vc = &verifiable_credential
.0
.as_object()
.expect("Failed to parse credential")
.get("vc")
.next()
.expect("Failed to parse credential")
.to_owned();

vp.verifiable_credentials.push(verifiable_credential);
vp.holder = Some(holder_did.into());
let mut json = parsed_vc.clone().into_serde_json();

// NOTE: the `id` in the VC is a UUID string, but it should be a URI
// according to the `SpecializedJsonCredential` type.
json.as_object_mut().map(|obj| {
// Update the ID to be a UriBuf.
let id = obj
.get("id")
.expect("failed to parse vc id")
.as_str()
.expect("failed to parse id into string");

// NOTE: Should this be a `DID` or other value?
let id_urn = format!("urn:uuid:{id}").as_bytes().to_vec();
let id_url = UriBuf::new(id_urn).expect("failed to parse id into UriBuf");
obj.insert("id".to_string(), serde_json::json!(id_url));
});

let mut vp = JsonPresentation::default();
vp.context = Context::default();
vp.verifiable_credentials
.push(serde_json::from_value(json)?);
vp.holder = Some(holder_did.into());
vp.id = UriBuf::new(
format!("urn:uuid:{}", Uuid::new_v4().to_string())
.as_bytes()
Expand Down

0 comments on commit e4302e3

Please sign in to comment.