Skip to content

Commit

Permalink
fix: deprecate async-trait for the Sign trait (#49)
Browse files Browse the repository at this point in the history
* fix: quickfix

* fix: add feature flags

* fix: uncomment identity_core

* fix: deprecate async-trait for Sign trait

* fix: clean

* clean

* fix: async to sync

* fix: fix tests
  • Loading branch information
nanderstabel committed Jul 20, 2023
1 parent f1e7caf commit c708463
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde_json = "1.0"
serde_with = "3.0"
url = { version = "2.3", features = ["serde"] }
getset = "0.1"
identity_credential = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434", default-features = false, features = ["validator", "credential", "presentation"] }

# TODO: Fix these dependencies once publishing to crates.io is automated.
[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion oid4vc-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ url = { version = "2.3.1", features = ["serde"] }
is_empty = "0.2.0"
serde_urlencoded = "0.7.1"
derive_more = "0.99.16"
identity_credential = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434" }
identity_credential.workspace = true
futures = "0.3"

[dev-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions oid4vc-core/src/authentication/sign.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use anyhow::Result;
use async_trait::async_trait;

#[async_trait]
pub trait Sign: Send + Sync {
// TODO: add this?
// fn jwt_alg_name() -> &'static str;
fn key_id(&self) -> Option<String>;
async fn sign(&self, message: &str) -> Result<Vec<u8>>;
fn sign(&self, message: &str) -> Result<Vec<u8>>;
}
6 changes: 3 additions & 3 deletions oid4vc-core/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
Ok(jsonwebtoken::decode::<T>(jwt, &key, &Validation::new(algorithm))?.claims)
}

pub async fn encode<C, S>(signer: Arc<S>, claims: C) -> Result<String>
pub fn encode<C, S>(signer: Arc<S>, claims: C) -> Result<String>
where
C: Serialize + Send,
S: Sign + ?Sized,
Expand All @@ -62,7 +62,7 @@ where

let message = [base64_url_encode(&jwt.header)?, base64_url_encode(&jwt.payload)?].join(".");

let proof_value = signer.sign(&message).await?;
let proof_value = signer.sign(&message)?;
let signature = base64_url::encode(proof_value.as_slice());
let message = [message, signature].join(".");
Ok(message)
Expand Down Expand Up @@ -95,7 +95,7 @@ mod tests {
"nonce": "nonce",
});
let subject = TestSubject::new("did:test:123".to_string(), "key_id".to_string()).unwrap();
let encoded = encode(Arc::new(subject), claims).await.unwrap();
let encoded = encode(Arc::new(subject), claims).unwrap();

let verifier = MockVerifier::new();
let (kid, algorithm) = extract_header(&encoded).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions oid4vc-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ impl TestSubject {
}
}

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

async fn sign(&self, message: &str) -> Result<Vec<u8>> {
fn sign(&self, message: &str) -> Result<Vec<u8>> {
let signature: Signature = TEST_KEYPAIR.sign(message.as_bytes());
Ok(signature.to_bytes().to_vec())
}
Expand Down
3 changes: 2 additions & 1 deletion oid4vc-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ serde_urlencoded = "0.7"
did-key = "0.2"
identity_iota = "0.6"
identity_core = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434" }
identity_credential = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434" }
identity_credential.workspace = true
futures = "0.3"

[dev-dependencies]
ed25519-dalek = "1.0.1"
Expand Down
3 changes: 1 addition & 2 deletions oid4vc-manager/src/managers/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ProviderManager {
.await
}

pub async fn generate_response(
pub fn generate_response(
&self,
request: AuthorizationRequest,
user_claims: StandardClaimsValues,
Expand All @@ -34,7 +34,6 @@ impl ProviderManager {
) -> Result<AuthorizationResponse> {
self.provider
.generate_response(request, user_claims, verifiable_presentation, presentation_submission)
.await
}

pub async fn send_response(&self, response: AuthorizationResponse) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions oid4vc-manager/src/managers/relying_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl RelyingPartyManager {
})
}

pub async fn encode(&self, request: &AuthorizationRequest) -> Result<String> {
self.relying_party.encode(request).await
pub fn encode(&self, request: &AuthorizationRequest) -> Result<String> {
self.relying_party.encode(request)
}

pub async fn validate_response(&self, response: &AuthorizationResponse) -> Result<ResponseItems> {
Expand Down
15 changes: 7 additions & 8 deletions oid4vc-manager/src/methods/iota_method.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures::executor::block_on;
use identity_iota::{
account::{Account, IdentitySetup, MethodContent},
account_storage::KeyLocation,
Expand All @@ -18,21 +19,20 @@ where
pub account: Account<C>,
}

#[async_trait]
impl Sign for IotaSubject {
async fn sign(&self, message: &str) -> Result<Vec<u8>> {
fn sign(&self, message: &str) -> Result<Vec<u8>> {
// Get the verification method for authentication from the DID document.
let method = self
.authentication_method()
.ok_or_else(|| anyhow!("No authentication method found."))?;

let key_location = KeyLocation::from_verification_method(method)?;

let proof_value = self
.account
.storage()
.key_sign(self.account.did(), &key_location, message.as_bytes().to_vec())
.await?;
let proof_value = block_on(self.account.storage().key_sign(
self.account.did(),
&key_location,
message.as_bytes().to_vec(),
))?;

Ok(proof_value.as_bytes().to_vec())
}
Expand Down Expand Up @@ -228,7 +228,6 @@ mod tests {
// The provider generates a signed SIOP response from the new SIOP request.
let response = provider_manager
.generate_response(request, StandardClaimsValues::default(), None, None)
.await
.unwrap();
println!("Generated SIOP response based on the SIOP request: {:#?}", response);

Expand Down
3 changes: 1 addition & 2 deletions oid4vc-manager/src/methods/key_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Sign for KeySubject {
.and_then(|authentication_methods| authentication_methods.get(0).cloned())
}

async fn sign(&self, message: &str) -> Result<Vec<u8>> {
fn sign(&self, message: &str) -> Result<Vec<u8>> {
Ok(self.keypair.sign(message.as_bytes()).to_vec())
}
}
Expand Down Expand Up @@ -126,7 +126,6 @@ mod tests {
// Test whether the provider manager can generate a response for the request succesfully.
let response = provider_manager
.generate_response(request, Default::default(), None, None)
.await
.unwrap();

// Let the relying party validate the response.
Expand Down
2 changes: 1 addition & 1 deletion oid4vc-manager/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Sign for TestSubject {
Some(self.key_id.clone())
}

async fn sign(&self, message: &str) -> Result<Vec<u8>> {
fn sign(&self, message: &str) -> Result<Vec<u8>> {
let signature: Signature = TEST_KEYPAIR.sign(message.as_bytes());
Ok(signature.to_bytes().to_vec())
}
Expand Down
3 changes: 1 addition & 2 deletions oid4vc-manager/tests/siopv2/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn test_implicit_flow() {
// Create a new `request_uri` endpoint on the mock server and load it with the JWT encoded `AuthorizationRequest`.
Mock::given(method("GET"))
.and(path("/request_uri"))
.respond_with(ResponseTemplate::new(200).set_body_string(relying_party_manager.encode(&request).await.unwrap()))
.respond_with(ResponseTemplate::new(200).set_body_string(relying_party_manager.encode(&request).unwrap()))
.mount(&mock_server)
.await;

Expand Down Expand Up @@ -155,7 +155,6 @@ async fn test_implicit_flow() {
// encoded as a JWT.
let response = provider_manager
.generate_response(request, response_claims, None, None)
.await
.unwrap();

// The provider manager sends it's response to the mock server's `redirect_uri` endpoint.
Expand Down
3 changes: 1 addition & 2 deletions oid4vc-manager/tests/siopv2_oid4vp/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn test_implicit_flow() {
.unwrap();

// Encode the verifiable credential as a JWT.
let jwt = jwt::encode(Arc::new(issuer), &verifiable_credential).await.unwrap();
let jwt = jwt::encode(Arc::new(issuer), &verifiable_credential).unwrap();

// Create a verifiable presentation using the JWT.
let verifiable_presentation = JwtPresentation::builder(Url::parse(subject_did).unwrap(), Object::new())
Expand All @@ -152,7 +152,6 @@ async fn test_implicit_flow() {
Some(verifiable_presentation),
Some(presentation_submission),
)
.await
.unwrap();

// Validate the response.
Expand Down
2 changes: 1 addition & 1 deletion oid4vp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
getset.workspace = true
identity_credential = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434" }
identity_credential.workspace = true
anyhow = "1.0"
futures = "0.3"
2 changes: 1 addition & 1 deletion siopv2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ url = { version = "2.3.1", features = ["serde"] }
is_empty = "0.2.0"
serde_urlencoded = "0.7.1"
derive_more = "0.99.16"
identity_credential = { git = "https://git@github.com/iotaledger/identity.rs", rev = "4f27434" }
identity_credential.workspace = true
futures = "0.3"

[dev-dependencies]
Expand Down
9 changes: 4 additions & 5 deletions siopv2/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Provider {

/// Generates a [`AuthorizationResponse`] in response to a [`AuthorizationRequest`] and the user's claims. The [`AuthorizationResponse`]
/// contains an [`IdToken`], which is signed by the [`Subject`] of the [`Provider`].
pub async fn generate_response(
pub fn generate_response(
&self,
request: AuthorizationRequest,
user_claims: StandardClaimsValues,
Expand All @@ -76,7 +76,7 @@ impl Provider {
.claims(user_claims)
.build()?;

let jwt = jwt::encode(self.subject.clone(), id_token).await?;
let jwt = jwt::encode(self.subject.clone(), id_token)?;
builder = builder.id_token(jwt);
}
ResponseType::IdTokenVpToken => {
Expand All @@ -90,7 +90,7 @@ impl Provider {
.claims(user_claims)
.build()?;

let jwt = jwt::encode(self.subject.clone(), id_token).await?;
let jwt = jwt::encode(self.subject.clone(), id_token)?;
builder = builder.id_token(jwt);

if let (Some(verifiable_presentation), Some(presentation_submission)) =
Expand All @@ -106,7 +106,7 @@ impl Provider {
.verifiable_presentation(verifiable_presentation)
.build()?;

let jwt = jwt::encode(self.subject.clone(), vp_token).await?;
let jwt = jwt::encode(self.subject.clone(), vp_token)?;
builder = builder.vp_token(jwt).presentation_submission(presentation_submission);
} else {
anyhow::bail!("Verifiable presentation is required for this response type.");
Expand Down Expand Up @@ -173,7 +173,6 @@ mod tests {
// Test whether the provider can generate a response for the request succesfully.
assert!(provider
.generate_response(request, Default::default(), None, None)
.await
.is_ok());
}
}
4 changes: 2 additions & 2 deletions siopv2/src/relying_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl RelyingParty {
})
}

pub async fn encode(&self, request: &AuthorizationRequest) -> Result<String> {
jwt::encode(self.subject.clone(), request).await
pub fn encode(&self, request: &AuthorizationRequest) -> Result<String> {
jwt::encode(self.subject.clone(), request)
}

/// Validates a [`AuthorizationResponse`] by decoding the header of the id_token, fetching the public key corresponding to
Expand Down
3 changes: 1 addition & 2 deletions siopv2/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ impl TestSubject {
}
}

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

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

0 comments on commit c708463

Please sign in to comment.