Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test JWT VC with single-element-array subject #313

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/one_or_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ impl<T> OneOrMany<T> {
}
}
}

pub fn to_single_mut(&mut self) -> Option<&mut T> {
match self {
Self::One(value) => Some(value),
Self::Many(values) => {
if values.len() == 1 {
Some(&mut values[0])
} else {
None
}
}
}
}
}

// consuming iterator
Expand Down
44 changes: 43 additions & 1 deletion src/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ impl Credential {
}
if let Some(sub) = claims.subject {
if let StringOrURI::URI(sub_uri) = sub {
if let OneOrMany::One(ref mut subject) = vc.credential_subject {
if let Some(ref mut subject) = vc.credential_subject.to_single_mut() {
subject.id = Some(sub_uri);
} else {
return Err(Error::InvalidSubject);
Expand Down Expand Up @@ -2334,6 +2334,48 @@ pub(crate) mod tests {
assert!(verification_result.errors.len() > 0);
}

#[async_std::test]
async fn decode_verify_jwt_single_array_subject() {
let key: JWK = serde_json::from_str(JWK_JSON).unwrap();

let vc_str = r###"{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"type": "VerifiableCredential",
"issuer": "did:example:foo",
"issuanceDate": "2021-09-28T19:58:30Z",
"credentialSubject": [{
"id": "did:example:a6c78986cc36418b95a22d7f736",
"spouse": "Example Person"
}]
}"###;

let vc = Credential {
expiration_date: Some(VCDateTime::from(Utc::now() + chrono::Duration::weeks(1))),
..serde_json::from_str(vc_str).unwrap()
};
let aud = "did:example:90336644520443d28ba78beb949".to_string();
let options = LinkedDataProofOptions {
domain: Some(aud),
checks: None,
created: None,
verification_method: Some(URI::String("did:example:foo#key1".to_string())),
..Default::default()
};
let signed_jwt = vc
.generate_jwt(Some(&key), &options, &DIDExample)
.await
.unwrap();
println!("{:?}", signed_jwt);

let (vc1_opt, verification_result) =
Credential::decode_verify_jwt(&signed_jwt, Some(options.clone()), &DIDExample).await;
println!("{:#?}", verification_result);
assert!(verification_result.errors.is_empty());
}

#[async_std::test]
async fn credential_issue_verify() {
let vc_str = r###"{
Expand Down