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 byob http behind feature flag #368

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = [
"crates/web5",
members = [
"crates/web5",
"crates/web5_cli",
"bindings/web5_uniffi",
"bindings/web5_uniffi_wrapper",
Expand Down
6 changes: 6 additions & 0 deletions crates/web5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ lazy_static = "1.5.0"
flate2 = "1.0.33"
rustls = { version = "0.23.13", default-features = false, features = ["std", "tls12"] }
rustls-native-certs = "0.8.0"
reqwest = { version = "0.12", optional = true, features = ["blocking"]}
once_cell = "1.19.0"

[features]
default = ["default_http_client"]
default_http_client = ["reqwest"]

[dev-dependencies]
mockito = "1.5.0"
Expand Down
6 changes: 3 additions & 3 deletions crates/web5/src/credentials/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ mod tests {
match result {
Err(Web5Error::Network(err_msg)) => {
assert!(
err_msg.contains("failed to connect to host"),
err_msg.contains("Failed to fetch credential schema"),
"Error message is: {}",
err_msg
)
Expand Down Expand Up @@ -605,10 +605,10 @@ mod tests {

match result {
Err(Web5Error::Http(err_msg)) => {
assert_eq!("non-successful response code 500", err_msg)
assert!(err_msg.contains("non-successful response code 500"))
}
_ => panic!(
"expected Web5Error::JsonSchema with specific message but got {:?}",
"expected Web5Error::Http with specific message but got {:?}",
result
),
}
Expand Down
23 changes: 21 additions & 2 deletions crates/web5/src/credentials/credential_schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;

use super::verifiable_credential_1_1::VerifiableCredential;
use crate::{
errors::{Result, Web5Error},
http::get_json,
http::get_http_client,
};
use jsonschema::{Draft, JSONSchema};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -30,7 +32,24 @@ pub(crate) fn validate_credential_schema(
}

let url = &credential_schema.id;
let json_schema = get_json::<serde_json::Value>(url)?;

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))
.map_err(|e| Web5Error::Network(format!("Failed to fetch credential schema: {}", e)))?;
if !(200..300).contains(&response.status_code) {
return Err(Web5Error::Http(format!(
"Failed to fetch credential schema: non-successful response code {}",
response.status_code
)));
}

let json_schema = serde_json::from_slice::<serde_json::Value>(&response.body)
.map_err(|err| Web5Error::Http(format!("unable to parse json response body {}", err)))?;

let compiled_schema = JSONSchema::options().compile(&json_schema).map_err(|err| {
Web5Error::JsonSchema(format!("unable to compile json schema {} {}", url, err))
})?;
Expand Down
4 changes: 2 additions & 2 deletions crates/web5/src/credentials/verifiable_credential_1_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ mod tests {
match result {
Err(Web5Error::Network(err_msg)) => {
assert!(
err_msg.contains("failed to connect to host"),
err_msg.contains("Failed to fetch credential schema"),
"Error message is: {}",
err_msg
)
Expand Down Expand Up @@ -827,7 +827,7 @@ mod tests {
let result = VerifiableCredential::from_vc_jwt(vc_jwt_at_port, true);
match result {
Err(Web5Error::Http(err_msg)) => {
assert_eq!("non-successful response code 500", err_msg)
assert!(err_msg.contains("non-successful response code 500"))
}
_ => panic!(
"expected Web5Error::JsonSchema with specific message but got {:?}",
Expand Down
22 changes: 18 additions & 4 deletions crates/web5/src/dids/methods/did_dht/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::{
},
},
errors::{Result, Web5Error},
http::{get, put},
http::get_http_client,
};
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

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

let response = put(&url, &body)?;
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)
.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 @@ -248,7 +257,12 @@ impl DidDht {
did.id.trim_start_matches('/')
);

let response = get(&url).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)).map_err(|_| ResolutionMetadataError::InternalError)?;

if response.status_code == 404 {
return Err(ResolutionMetadataError::NotFound);
Expand Down
21 changes: 15 additions & 6 deletions crates/web5/src/dids/methods/did_web/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use crate::{
resolution::{
resolution_metadata::ResolutionMetadataError, resolution_result::ResolutionResult,
},
},
http::get_json,
}, http::get_http_client,
};
use std::{
future::{Future, IntoFuture},
pin::Pin,
collections::HashMap, future::{Future, IntoFuture}, pin::Pin
};
use url::Url;

Expand Down Expand Up @@ -50,8 +48,19 @@ impl Resolver {
}

async fn resolve(url: String) -> Result<ResolutionResult, ResolutionMetadataError> {
let document =
get_json::<Document>(&url).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/json".to_string())
]);
let response = get_http_client().get(&url, Some(headers))
.map_err(|_| ResolutionMetadataError::InternalError)?;
if !(200..300).contains(&response.status_code) {
return Err(ResolutionMetadataError::InternalError);
}

let document = serde_json::from_slice::<Document>(&response.body)
.map_err(|_| ResolutionMetadataError::InternalError)?;
Ok(ResolutionResult {
document: Some(document),
..Default::default()
Expand Down
Loading
Loading