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

Fix the presentation submission in the oid4vp-rs e2e test. #8

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
10611a8
wip: use ssi 0.8.1, debugging e2e test using did resolver
Ryanmtate Aug 8, 2024
7ea4c50
wip: use VerificationMethodDIDResolver for DIDClient constructor
Ryanmtate Aug 8, 2024
7b3ced0
remove unused imports
Ryanmtate Aug 8, 2024
4a77347
wip: debugging did resolver jwk not found in e2e flow
Ryanmtate Aug 9, 2024
48e152e
feat/improve-presentation-exchange-support-in-oid4vp-rs
Ryanmtate Aug 9, 2024
26e62f1
add getter methods for presentation definition member fields
Ryanmtate Aug 9, 2024
8c47808
fix broken links in documentation
Ryanmtate Aug 9, 2024
de5db7d
update presentation submission implementation
Ryanmtate Aug 10, 2024
24845c7
fix test cases. todo: update test cases to use newly created interfac…
Ryanmtate Aug 10, 2024
5036a69
update json schema validator to use anyhow result type
Ryanmtate Aug 10, 2024
ac8e904
merge presentation exchange updates
Ryanmtate Aug 11, 2024
6fe4d46
wip: use latest implementation changes, update tests
Ryanmtate Aug 12, 2024
6d29442
fix verification method did resolver tests
Ryanmtate Aug 12, 2024
e885a4c
wip: remove unused imports
Ryanmtate Aug 12, 2024
3996946
add ClaimFormat type
Ryanmtate Aug 12, 2024
4e841cc
add regex support for string pattern matching
Ryanmtate Aug 12, 2024
edcc8a2
revert uuid presentation definition id type to string
Ryanmtate Aug 12, 2024
23e589a
fix: ensure negation of regex pattern match for error
Ryanmtate Aug 12, 2024
1527933
add 'other' variante to claim format type
Ryanmtate Aug 12, 2024
8ed8828
remove commented out code
Ryanmtate Aug 12, 2024
97b3462
merge presentation exchange updates
Ryanmtate Aug 12, 2024
dcdde5c
use ssi sub-crates instead of main ssi dependency
Ryanmtate Aug 12, 2024
1a2af67
add jwt_vc_json and jwt_vp_json claim formats
Ryanmtate Aug 12, 2024
61043a9
wip: construct verifiable presentation for e2e test
Ryanmtate Aug 13, 2024
24497dd
wip: perform validation on presentation submission
Ryanmtate Aug 13, 2024
3423d13
ensure range exclusive values are checked; fix inclusive range values
Ryanmtate Aug 13, 2024
7bd0660
ensure enum values are parsed in alpha descending order
Ryanmtate Aug 13, 2024
ce60c03
wip: verify authorized response presentation submission
Ryanmtate Aug 14, 2024
cd22624
ensure json schema validator adheres to the specification
Ryanmtate Aug 14, 2024
26dda42
ensure json schema validator adheres to the specification
Ryanmtate Aug 14, 2024
d188434
add unit tests for schema validator
Ryanmtate Aug 14, 2024
89db3b2
use serde default value for constraints field if not found during des…
Ryanmtate Aug 14, 2024
0ec29b3
merge schema validator changes
Ryanmtate Aug 14, 2024
d478103
remove unused imports
Ryanmtate Aug 14, 2024
5e65c4e
Merge branch 'main' into fix/fix-the-presentation-submission-in-the-o…
Ryanmtate Aug 14, 2024
e276d6a
remove unsed imports in test files
Ryanmtate Aug 14, 2024
3d93a0e
update vp token
Ryanmtate Aug 14, 2024
0843f66
rebase with main
Ryanmtate Aug 14, 2024
b7d8083
resolve merge conflict; remove schema validator and replace with json…
Ryanmtate Aug 16, 2024
096d383
remove unused dependencies
Ryanmtate Aug 16, 2024
f82fa26
add rand crate and provide random nonce method using Rng trait
Ryanmtate Aug 20, 2024
b07b715
remove todos and update comments, use JWKResolver instead of Verifica…
Ryanmtate Aug 20, 2024
fcb4ed9
fix clippy warnings
Ryanmtate Aug 20, 2024
1398cad
verify jwt in validate_authorization_response presentation definition…
Ryanmtate Aug 20, 2024
be2bbed
Update tests/e2e.rs
Ryanmtate Aug 20, 2024
32a49b7
update descriptor map nested path in e2e example
Ryanmtate Aug 20, 2024
d050355
remove dependency patches
Ryanmtate Aug 20, 2024
4a0217b
add paths to example for input descriptor constraints field
Ryanmtate Aug 20, 2024
9997486
use top level json path for jwt_vp_json
Ryanmtate Aug 20, 2024
2a93750
Update src/verifier/client.rs
Ryanmtate Aug 20, 2024
f490b8b
rebase
Ryanmtate Aug 20, 2024
347259d
revert validation function async signature to use boxed pin future
Ryanmtate Aug 20, 2024
05d550e
add helper methods
Ryanmtate Aug 22, 2024
129d22c
debug: jwt claim signing does not include public key
Ryanmtate Aug 22, 2024
a3bf215
make request signer methods return a result
Ryanmtate Aug 22, 2024
7733f65
refactor presentation exchange file into smaller modules
Ryanmtate Aug 23, 2024
54c0f79
fix clippy warnings
Ryanmtate Aug 23, 2024
efd5773
Merge branch 'main' into fix/fix-the-presentation-submission-in-the-o…
Ryanmtate Aug 23, 2024
a7aed11
fix outcome error cause
Ryanmtate Aug 23, 2024
2e453ca
add credential format and add requested fields helper method to input…
Ryanmtate Aug 26, 2024
a3bfdcb
Update src/core/presentation_definition.rs
Ryanmtate Aug 27, 2024
7a337ae
Update src/core/presentation_definition.rs
Ryanmtate Aug 27, 2024
76e3a3a
wip: add notes on required fields parsing
Ryanmtate Aug 27, 2024
51e11e8
update vp token base64 encoding and check for multiple vp payloads
Ryanmtate Aug 28, 2024
65e1b11
move validation to auth response impl instead of presentation definit…
Ryanmtate Aug 28, 2024
d0c7439
remove feature gated non-optional deps
Ryanmtate Aug 28, 2024
033fd5e
remove cfg features
Ryanmtate Aug 28, 2024
d98d2a4
wip: handle groups in presentation definition, input descriptor tests
Ryanmtate Aug 28, 2024
915b922
remove cfg feature tags
Ryanmtate Aug 28, 2024
3d7b1f5
remove extraneous metadata helper methods; use UntypedObject for dere…
Ryanmtate Aug 28, 2024
e9b33df
add submission requirement check for presentation validation
Ryanmtate Aug 29, 2024
64f87eb
add validate method to vp token; ensure submission requirement all ru…
Ryanmtate Aug 29, 2024
d3dce7f
add vp token validate unencoded method. fix minor todos.
Ryanmtate Aug 29, 2024
458ff5f
Update Cargo.toml
Ryanmtate Sep 12, 2024
165b1c7
fix other claim format serde
Ryanmtate Sep 12, 2024
0e0f985
use Vec::is_empty versus Option::is_none for various serialization fi…
Ryanmtate Sep 13, 2024
b8e9141
revert to use of ClaimFormatMap to pass presentation defintion test s…
Ryanmtate Sep 13, 2024
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ async-trait = "0.1.73"
base64 = "0.21.4"
did-web = "0.2.2"
http = "1.1.0"
jsonpath_lib = "0.3.0"
p256 = { version = "0.13.2", features = ["jwk"], optional = true }
reqwest = { version = "0.12.5", features = ["rustls-tls"], optional = true }
serde = "1.0.188"
serde_cbor = "0.11.2"
serde_json = "1.0.107"
serde_qs = "0.12.0"
serde_urlencoded = "0.7.1"
ssi = "0.7.0"
ssi = "0.8.1"
Ryanmtate marked this conversation as resolved.
Show resolved Hide resolved
# ssi-claims = "0.1.0"
# ssi-dids-core = "0.1.0"
# ssi-jwk = "0.2.1"
thiserror = "1.0.49"
tokio = "1.32.0"
tracing = "0.1.37"
Expand Down
25 changes: 19 additions & 6 deletions src/core/authorization_request/verification/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ use crate::core::{
use anyhow::{bail, Context, Result};
use base64::prelude::*;
use serde_json::{Map, Value as Json};
use ssi::did_resolve::{resolve_key, DIDResolver};
use ssi::{
dids::{DIDResolver, VerificationMethodDIDResolver},
jwk::JWKResolver,
verification_methods::{
GenericVerificationMethod, InvalidVerificationMethod, MaybeJwkVerificationMethod,
VerificationMethodSet,
},
};

/// Default implementation of request validation for `client_id_scheme` `did`.
pub async fn verify_with_resolver(
wallet_metadata: &WalletMetadata,
request_object: &AuthorizationRequestObject,
request_jwt: String,
trusted_dids: Option<&[String]>,
resolver: &dyn DIDResolver,
resolver: &VerificationMethodDIDResolver<
impl DIDResolver,
impl MaybeJwkVerificationMethod
+ VerificationMethodSet
+ TryFrom<GenericVerificationMethod, Error = InvalidVerificationMethod>,
>,
) -> Result<()> {
let (headers_b64, _, _) = ssi::jws::split_jws(&request_jwt)?;
let (headers_b64, _, _) = ssi::claims::jws::split_jws(&request_jwt)?;

let headers_json_bytes = BASE64_URL_SAFE_NO_PAD
.decode(headers_b64)
Expand Down Expand Up @@ -64,11 +76,12 @@ pub async fn verify_with_resolver(
}
}

let jwk = resolve_key(&kid, resolver)
let jwk = resolver
.fetch_public_jwk(Some(&kid))
.await
.context("unable to resolve verification method from 'kid' header")?;
.context("unable to resolve key from verification method")?;

let _: Json = ssi::jwt::decode_verify(&request_jwt, &jwk)
let _: Json = ssi::claims::jwt::decode_verify(&request_jwt, &jwk)
.context("request signature could not be verified")?;

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions src/core/authorization_request/verification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ pub(crate) async fn verify_request<W: Wallet + ?Sized>(
wallet: &W,
jwt: String,
) -> Result<AuthorizationRequestObject> {
let request: AuthorizationRequestObject = ssi::jwt::decode_unverified::<UntypedObject>(&jwt)
.context("unable to decode Authorization Request Object JWT")?
.try_into()?;
let request: AuthorizationRequestObject =
ssi::claims::jwt::decode_unverified::<UntypedObject>(&jwt)
.context("unable to decode Authorization Request Object JWT")?
.try_into()?;

validate_request_against_metadata(wallet, &request).await?;

Expand Down
2 changes: 1 addition & 1 deletion src/core/authorization_request/verification/x509_san.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn validate<V: Verifier>(
trusted_roots: Option<&[Certificate]>,
) -> Result<()> {
let client_id = request_object.client_id().0.as_str();
let (headers_b64, body_b64, sig_b64) = ssi::jws::split_jws(&request_jwt)?;
let (headers_b64, body_b64, sig_b64) = ssi::claims::jws::split_jws(&request_jwt)?;

let headers_json_bytes = BASE64_URL_SAFE_NO_PAD
.decode(headers_b64)
Expand Down
6 changes: 3 additions & 3 deletions src/core/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ mod test {
let object: UntypedObject = serde_json::from_value(json!(
{
"presentation_submission": {
"id": "id",
"definition_id": "definition_id",
"id": "d05a7f51-ac09-43af-8864-e00f0175f2c7",
"definition_id": "f619e64a-8f80-4b71-8373-30cf07b1e4f2",
"descriptor_map": []
},
"vp_token": "string"
Expand All @@ -119,7 +119,7 @@ mod test {
let response = UnencodedAuthorizationResponse::try_from(object).unwrap();
assert_eq!(
response.into_x_www_form_urlencoded().unwrap(),
"presentation_submission=%7B%22definition_id%22%3A%22definition_id%22%2C%22descriptor_map%22%3A%5B%5D%2C%22id%22%3A%22id%22%7D&vp_token=string",
"presentation_submission=%7B%22id%22%3A%22d05a7f51-ac09-43af-8864-e00f0175f2c7%22%2C%22definition_id%22%3A%22f619e64a-8f80-4b71-8373-30cf07b1e4f2%22%2C%22descriptor_map%22%3A%5B%5D%7D&vp_token=string",
)
}
}
204 changes: 204 additions & 0 deletions src/json_schema_validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

// TODO: Consider using `Value` type from `serde_json`
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SchemaType {
String,
Number,
Integer,
Boolean,
Array,
Object,
}

/// Schema Validator is a JSON Schema descriptor used to evaluate the return value of a JsonPath
/// expression, used by the presentation definition constraints field to ensure the property value
/// meets the expected schema.
///
/// For more information, see the field constraints filter property:
///
/// https://identity.foundation/presentation-exchange/spec/v2.0.0/#input-descriptor-object
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SchemaValidator {
Ryanmtate marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "type")]
schema_type: SchemaType,
#[serde(skip_serializing_if = "Option::is_none")]
min_length: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
max_length: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pattern: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
minimum: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
maximum: Option<f64>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
required: Vec<String>,
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
properties: HashMap<String, Box<SchemaValidator>>,
#[serde(skip_serializing_if = "Option::is_none")]
items: Option<Box<SchemaValidator>>,
}

impl PartialEq for SchemaValidator {
fn eq(&self, other: &Self) -> bool {
self.schema_type == other.schema_type
&& self.min_length == other.min_length
&& self.max_length == other.max_length
&& self.pattern == other.pattern
&& self.minimum == other.minimum
&& self.maximum == other.maximum
&& self.required == other.required
&& self.properties == other.properties
&& self.items == other.items
}
}

impl Eq for SchemaValidator {}

impl SchemaValidator {
pub fn validate(&self, value: &Value) -> Result<()> {
match self.schema_type {
SchemaType::String => self.validate_string(value),
SchemaType::Number => self.validate_number(value),
SchemaType::Integer => self.validate_integer(value),
SchemaType::Boolean => self.validate_boolean(value),
SchemaType::Array => self.validate_array(value),
SchemaType::Object => self.validate_object(value),
}
}

pub fn validate_string(&self, value: &Value) -> Result<()> {
let s = value.as_str().context("Expected a string")?;

if let Some(min_length) = self.min_length {
if s.len() < min_length {
bail!(
"String length {} is less than minimum {}",
s.len(),
min_length
);
}
}

if let Some(max_length) = self.max_length {
if s.len() > max_length {
bail!(
"String length {} is greater than maximum {}",
s.len(),
max_length
);
}
}

if let Some(pattern) = &self.pattern {
// Note: In a real implementation, you'd use a regex library here
Ryanmtate marked this conversation as resolved.
Show resolved Hide resolved
if !s.contains(pattern) {
bail!("String does not match pattern: {}", pattern);
}
}

Ok(())
}

pub fn validate_number(&self, value: &Value) -> Result<()> {
let n = value.as_f64().context("Expected a number")?;

if let Some(minimum) = self.minimum {
if n < minimum {
bail!("Number {} is less than minimum {}", n, minimum);
}
}

if let Some(maximum) = self.maximum {
if n > maximum {
bail!("Number {} is greater than maximum {}", n, maximum);
}
}

Ok(())
}

pub fn validate_integer(&self, value: &Value) -> Result<()> {
let n = value.as_i64().context("Expected an integer")?;

if let Some(minimum) = self.minimum {
if (n as f64) < minimum {
bail!("Integer {} is less than minimum {}", n, minimum);
}
}

if let Some(maximum) = self.maximum {
if n as f64 > maximum {
bail!("Integer {} is greater than maximum {}", n, maximum);
}
}

Ok(())
}

pub fn validate_boolean(&self, value: &Value) -> Result<()> {
if !value.is_boolean() {
bail!("Expected a boolean".to_string());
}
Ok(())
}

pub fn validate_array(&self, value: &Value) -> Result<()> {
let arr = value.as_array().context("Expected an array")?;

if let Some(min_length) = self.min_length {
if arr.len() < min_length {
bail!(
"Array length {} is less than minimum {}",
arr.len(),
min_length
);
}
}

if let Some(max_length) = self.max_length {
if arr.len() > max_length {
bail!(
"Array length {} is greater than maximum {}",
arr.len(),
max_length
);
}
}

if let Some(item_validator) = &self.items {
for (index, item) in arr.iter().enumerate() {
item_validator
.validate(item)
.context(format!("Error in array item {}", index))?;
}
}

Ok(())
}

pub fn validate_object(&self, value: &Value) -> Result<()> {
let obj = value.as_object().context("Expected an object")?;

for required_prop in &self.required {
if !obj.contains_key(required_prop) {
bail!("Missing required property: {}", required_prop);
}
}

for (prop_name, prop_validator) in &self.properties {
if let Some(prop_value) = obj.get(prop_name) {
prop_validator
.validate(prop_value)
.context(format!("Error in property {}", prop_name))?;
}
}

Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod core;
mod json_schema_validation;
pub mod presentation_exchange;
mod utils;
pub mod verifier;
Expand Down
Loading