Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel committed Jul 21, 2023
1 parent 606ebd7 commit 5a75380
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 89 deletions.
6 changes: 6 additions & 0 deletions oid4vc-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ serde_urlencoded = "0.7.1"
derive_more = "0.99.16"
identity_credential.workspace = true
futures = "0.3"

[dev-dependencies]
ed25519-dalek = "1.0.1"
rand = "0.7"
lazy_static = "1.4.0"
derivative = "2.2.0"
55 changes: 25 additions & 30 deletions oid4vc-core/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ where
Ok(base64_url::encode(serde_json::to_vec(value)?.as_slice()))
}

// #[cfg(test)]
// mod tests {
// use super::*;
// use crate::{
// test_utils::{MockSubject, MockVerifier},
// IdToken, Verify,
// };
// use serde_json::json;
#[cfg(test)]
mod tests {
use super::*;
use crate::{
test_utils::{MockVerifier, TestSubject},
Verify,
};
use serde_json::{json, Value};

#[tokio::test]
async fn test_encode() {
Expand All @@ -97,26 +97,21 @@ where
let subject = TestSubject::new("did:test:123".to_string(), "key_id".to_string()).unwrap();
let encoded = encode(Arc::new(subject), claims).unwrap();

// });
// let subject = MockSubject::new("did:mock:123".to_string(), "key_id".to_string()).unwrap();
// let encoded = encode(Arc::new(subject), claims).await.unwrap();
let verifier = MockVerifier::new();
let (kid, algorithm) = extract_header(&encoded).unwrap();
let public_key = verifier.public_key(&kid).await.unwrap();
let decoded: Value = decode(&encoded, public_key, algorithm).unwrap();

// let verifier = MockVerifier::new();
// let (kid, algorithm) = extract_header(&encoded).unwrap();
// let public_key = verifier.public_key(&kid).await.unwrap();
// let decoded: IdToken = decode(&encoded, public_key, algorithm).unwrap();

// assert_eq!(
// decoded,
// IdToken::builder()
// .iss("did:example:123".to_string())
// .sub("did:example:123".to_string())
// .aud("did:example:456".to_string())
// .exp(9223372036854775807i64)
// .iat(1593436422)
// .nonce("nonce".to_string())
// .build()
// .unwrap()
// )
// }
// }
assert_eq!(
decoded,
json!({
"iss": "did:example:123",
"sub": "did:example:123",
"aud": "did:example:456",
"exp": 9223372036854775807i64,
"iat": 1593436422,
"nonce": "nonce",
})
)
}
}
3 changes: 3 additions & 0 deletions oid4vc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use rfc7519_claims::RFC7519Claims;
use serde::Serialize;
pub use subject_syntax_type::{DidMethod, SubjectSyntaxType};

#[cfg(test)]
mod test_utils;

#[macro_export]
macro_rules! builder_fn {
($name:ident, $ty:ty) => {
Expand Down
68 changes: 68 additions & 0 deletions oid4vc-core/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{Sign, Subject, Verify};
use anyhow::Result;
use async_trait::async_trait;
use derivative::{self, Derivative};
use ed25519_dalek::{Keypair, Signature, Signer};
use lazy_static::lazy_static;
use rand::rngs::OsRng;

// Keypair for mocking purposes.
lazy_static! {
pub static ref TEST_KEYPAIR: Keypair = Keypair::generate(&mut OsRng);
}

#[derive(Derivative)]
#[derivative(Default)]
pub struct TestSubject {
#[derivative(Default(value = "did_url::DID::parse(\"did:test:123\").unwrap()"))]
pub did: did_url::DID,
pub key_id: String,
}

impl TestSubject {
pub fn new(did: String, key_id: String) -> Result<Self> {
Ok(TestSubject {
did: did_url::DID::parse(did)?,
key_id,
})
}
}

impl Sign for TestSubject {
fn key_id(&self) -> Option<String> {
Some(self.key_id.clone())
}

fn sign(&self, message: &str) -> Result<Vec<u8>> {
let signature: Signature = TEST_KEYPAIR.sign(message.as_bytes());
Ok(signature.to_bytes().to_vec())
}
}

#[async_trait]
impl Verify for TestSubject {
async fn public_key(&self, _kid: &str) -> Result<Vec<u8>> {
Ok(TEST_KEYPAIR.public.to_bytes().to_vec())
}
}

impl Subject for TestSubject {
fn identifier(&self) -> Result<String> {
Ok(self.did.to_string())
}
}

pub struct MockVerifier;

impl MockVerifier {
pub fn new() -> Self {
MockVerifier {}
}
}

#[async_trait]
impl Verify for MockVerifier {
async fn public_key(&self, _kid: &str) -> Result<Vec<u8>> {
Ok(TEST_KEYPAIR.public.to_bytes().to_vec())
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/credentials/jwt_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/request/pd_ac_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
}
}
]
}
}
76 changes: 38 additions & 38 deletions oid4vp/tests/examples/request/pd_ac_vc_sd.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
{
"id": "example_vc_ac_sd",
"input_descriptors": [
{
"id": "id_credential",
"format": {
"ac_vc": {
"proof_type": [
"CLSignature2019"
]
}
},
"constraints": {
"limit_disclosure": "required",
"fields": [
{
"path": [
"$.schema_id"
],
"filter": {
"type": "string",
"const": "did:indy:idu:test:3QowxFtwciWceMFr7WbwnM:2:BasicScheme:0\\.1"
}
},
{
"path": [
"$.values.first_name"
]
},
{
"path": [
"$.values.last_name"
]
}
]
}
}
]
}
"id": "example_vc_ac_sd",
"input_descriptors": [
{
"id": "id_credential",
"format": {
"ac_vc": {
"proof_type": [
"CLSignature2019"
]
}
},
"constraints": {
"limit_disclosure": "required",
"fields": [
{
"path": [
"$.schema_id"
],
"filter": {
"type": "string",
"const": "did:indy:idu:test:3QowxFtwciWceMFr7WbwnM:2:BasicScheme:0\\.1"
}
},
{
"path": [
"$.values.first_name"
]
},
{
"path": [
"$.values.last_name"
]
}
]
}
}
]
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/request/pd_jwt_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/request/pd_ldp_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/request/pd_mdl_iso_cbor.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/request/vp_token_type_only.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/response/ps_ac_vc_sd.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
}
}
]
}
}
18 changes: 9 additions & 9 deletions oid4vp/tests/examples/response/ps_jwt_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"definition_id": "example_jwt_vc",
"id": "example_jwt_vc_presentation_submission",
"descriptor_map": [
{
"id": "id_credential",
"path": "$",
"format": "jwt_vp_json",
"path_nested": {
"path": "$.vp.verifiableCredential[0]",
"format": "jwt_vc_json"
{
"id": "id_credential",
"path": "$",
"format": "jwt_vp_json",
"path_nested": {
"path": "$.vp.verifiableCredential[0]",
"format": "jwt_vc_json"
}
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/response/ps_ldp_vc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion oid4vp/tests/examples/response/ps_mdl_iso_cbor.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"path": "$"
}
]
}
}

0 comments on commit 5a75380

Please sign in to comment.