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

Allow BYO http impl #377

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion bindings/web5_uniffi_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ license-file.workspace = true
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { version = "1.38.0", features = ["full"] }
web5 = { path = "../../crates/web5" }
web5 = { path = "../../crates/web5", features = ["http_reqwest"]}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class VerifiableCredentialTest {
VerifiableCredential.create(ISSUER, CREDENTIAL_SUBJECT, options)
}

assertTrue(exception.message.contains("failed to resolve status code 500"))
assertTrue(exception.message.contains("Failed to fetch credential schema"))

mockWebServer.shutdown()
}
Expand Down Expand Up @@ -1054,7 +1054,7 @@ class VerifiableCredentialTest {
VerifiableCredential.fromVcJwt(vcJwtAtPort, true)
}

assertTrue(exception.message.contains("failed to resolve status code 500"))
assertTrue(exception.message.contains("Failed to fetch credential schema"))

mockWebServer.shutdown()
}
Expand Down
10 changes: 9 additions & 1 deletion crates/web5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license-file.workspace = true
rust-version = "1.74.0"

[dependencies]
async-trait = "0.1.83"
base64 = { workspace = true }
byteorder = "1.5.0"
chrono = { workspace = true }
Expand All @@ -32,8 +33,15 @@ x25519-dalek = { version = "2.0.1", features = ["getrandom", "static_secrets"] }
zbase32 = "0.1.2"
lazy_static = { workspace = true }
flate2 = "1.0.33"
http-std = { path = "../http-std" }
reqwest = { version = "0.12", optional = true }
once_cell = "1.19.0"

[features]
default = []
http_reqwest = ["reqwest"]

[dev-dependencies]
mockito = "1.5.0"
tokio = { version = "1.38.0", features = ["macros", "test-util"] }
web5 = { path = ".", features = ["http_reqwest"] }

10 changes: 5 additions & 5 deletions crates/web5/src/credentials/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ mod tests {
.await;

match result {
Err(Web5Error::Http(err)) => {
Err(Web5Error::Network(err)) => {
assert!(err.to_string().contains("error sending request"))
}
_ => panic!(
"expected Web5Error::Http with specific message but got {:?}",
"expected Web5Error::Network with specific message but got {:?}",
result
),
};
Expand All @@ -636,11 +636,11 @@ mod tests {
.await;

match result {
Err(Web5Error::JsonSchema(err_msg)) => {
assert_eq!("failed to resolve status code 500", err_msg)
Err(Web5Error::Network(err_msg)) => {
assert!(err_msg.contains("Failed to fetch credential schema"))
}
_ => panic!(
"expected Web5Error::JsonSchema with specific message but got {:?}",
"expected Web5Error::Network with specific message but got {:?}",
result
),
}
Expand Down
20 changes: 18 additions & 2 deletions crates/web5/src/credentials/credential_schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use super::verifiable_credential_1_1::VerifiableCredential;
use crate::errors::{Result, Web5Error};
use crate::{errors::{Result, Web5Error}, http::get_http_client};
use jsonschema::{Draft, JSONSchema};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -28,7 +30,21 @@ pub(crate) async fn validate_credential_schema(

let url = &credential_schema.id;

let response = http_std::fetch(url, None).await?;
let headers: HashMap<String, String> = HashMap::from([
("Host".to_string(), "{}".to_string()),
("Connection".to_string(), "close".to_string()),
("Accept".to_string(), "application/json".to_string())
]);
let response = get_http_client().get(url, Some(headers))
.await
.map_err(|e| Web5Error::Network(format!("Failed to fetch credential schema: {}", e)))?;

if !(200..300).contains(&response.status_code) {
return Err(Web5Error::Network(format!(
"Failed to fetch credential schema: non-successful response code {}",
response.status_code
)));
}

if !(200..300).contains(&response.status_code) {
return Err(Web5Error::JsonSchema(format!(
Expand Down
10 changes: 5 additions & 5 deletions crates/web5/src/credentials/verifiable_credential_1_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,11 @@ mod tests {
let result = VerifiableCredential::from_vc_jwt(vc_jwt_with_invalid_url, true).await;

match result {
Err(Web5Error::Http(err)) => {
Err(Web5Error::Network(err)) => {
assert!(err.to_string().contains("error sending request"))
}
_ => panic!(
"expected Web5Error::Http with specific message but got {:?}",
"expected Web5Error::Network with specific message but got {:?}",
result
),
};
Expand All @@ -839,11 +839,11 @@ mod tests {

let result = VerifiableCredential::from_vc_jwt(vc_jwt_at_port, true).await;
match result {
Err(Web5Error::JsonSchema(err_msg)) => {
assert_eq!("failed to resolve status code 500", err_msg)
Err(Web5Error::Network(err_msg)) => {
assert!(err_msg.contains("Failed to fetch credential schema"))
}
_ => panic!(
"expected Web5Error::JsonSchema with specific message but got {:?}",
"expected Web5Error::Network with specific message but got {:?}",
result
),
}
Expand Down
42 changes: 18 additions & 24 deletions crates/web5/src/dids/methods/did_dht/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use crate::{
resolution_metadata::ResolutionMetadataError, resolution_result::ResolutionResult,
},
},
errors::{Result, Web5Error},
errors::{Result, Web5Error}, http::get_http_client,
};
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

mod bep44;
mod document_packet;
Expand Down Expand Up @@ -190,25 +190,16 @@ impl DidDht {
bearer_did.did.id.trim_start_matches('/')
);

let response = http_std::fetch(
&url,
Some(http_std::FetchOptions {
method: Some(http_std::Method::Put),
headers: Some(
[
(
"Content-Type".to_string(),
"application/octet-stream".to_string(),
),
("Content-Length".to_string(), body.len().to_string()),
]
.into_iter()
.collect(),
),
body: Some(body),
}),
)
.await?;
let headers: HashMap<String, String> = HashMap::from([
("Host".to_string(), "{}".to_string()),
("Connection".to_string(), "close".to_string()),
("Content-Length".to_string(), "{}".to_string()),
("Content-Type".to_string(), "application/octet-stream".to_string())
]);

let response = get_http_client().put(&url, Some(headers), &body)
.await
.map_err(|e| Web5Error::Network(format!("Failed to PUT did:dht: {}", e)))?;
if response.status_code != 200 {
return Err(Web5Error::Network(
"failed to PUT DID to mainline".to_string(),
Expand Down Expand Up @@ -265,9 +256,12 @@ impl DidDht {
did.id.trim_start_matches('/')
);

let response = http_std::fetch(&url, None)
.await // todo here
.map_err(|_| ResolutionMetadataError::InternalError)?;
let headers: HashMap<String, String> = HashMap::from([
("Host".to_string(), "{}".to_string()),
("Connection".to_string(), "close".to_string()),
("Accept".to_string(), "application/octet-stream".to_string())
]);
let response = get_http_client().get(&url, Some(headers)).await.map_err(|_| ResolutionMetadataError::InternalError)?;

if response.status_code == 404 {
return Err(ResolutionMetadataError::NotFound);
Expand Down
17 changes: 14 additions & 3 deletions crates/web5/src/dids/methods/did_web/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::dids::{
use std::collections::HashMap;

use crate::{dids::{
data_model::document::Document,
did::Did,
resolution::{
resolution_metadata::ResolutionMetadataError, resolution_result::ResolutionResult,
},
};
}, http::get_http_client};
use url::Url;

// PORT_SEP is the : character that separates the domain from the port in a URI.
Expand Down Expand Up @@ -43,10 +45,19 @@ impl Resolver {
}

pub async fn resolve(&self) -> Result<ResolutionResult, ResolutionMetadataError> {
let response = http_std::fetch(&self.http_url, None)
let headers: HashMap<String, String> = HashMap::from([
("Host".to_string(), "{}".to_string()),
("Connection".to_string(), "close".to_string()),
("Accept".to_string(), "application/json".to_string())
]);
let response = get_http_client().get(&self.http_url, Some(headers))
.await
.map_err(|_| ResolutionMetadataError::InternalError)?;

if !(200..300).contains(&response.status_code) {
return Err(ResolutionMetadataError::InternalError);
}

if response.status_code == 404 {
return Err(ResolutionMetadataError::NotFound);
} else if !(200..300).contains(&response.status_code) {
Expand Down
5 changes: 2 additions & 3 deletions crates/web5/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
credentials::VerificationError, dids::resolution::resolution_metadata::ResolutionMetadataError,
};
use base64::DecodeError;
use http_std::Error as HttpError;
use serde_json::Error as SerdeJsonError;
use std::sync::PoisonError;

Expand Down Expand Up @@ -30,9 +29,9 @@ pub enum Web5Error {
Network(String),
#[error("datetime error {0}")]
DateTime(String),
#[error("http error {0}")]
Http(String),

#[error(transparent)]
Http(#[from] HttpError),
#[error(transparent)]
Resolution(#[from] ResolutionMetadataError),
#[error(transparent)]
Expand Down
Loading
Loading