diff --git a/agent_application/src/main.rs b/agent_application/src/main.rs index 8a7f51fc..a299d40c 100644 --- a/agent_application/src/main.rs +++ b/agent_application/src/main.rs @@ -7,13 +7,13 @@ use agent_secret_manager::{secret_manager, subject::Subject}; use agent_shared::{ config::{config, LogFormat, SupportedDidMethod, ToggleOptions}, domain_linkage::create_did_configuration_resource, + from_jsonwebtoken_algorithm_to_jwsalgorithm, }; use agent_store::{in_memory, postgres, EventPublisher}; use agent_verification::services::VerificationServices; use axum::{routing::get, Json}; use identity_document::service::{Service, ServiceEndpoint}; -use identity_iota::verification::jws::JwsAlgorithm; -use std::{str::FromStr, sync::Arc}; +use std::sync::Arc; use tokio::{fs, io}; use tower_http::cors::CorsLayer; use tracing::info; @@ -87,13 +87,9 @@ async fn main() -> io::Result<()> { .produce_document( did_manager::DidMethod::Web, Some(did_manager::MethodSpecificParameters::Web { origin: url.origin() }), - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm( + &agent_shared::config::get_preferred_signing_algorithm(), + ), ) .await .unwrap(), diff --git a/agent_secret_manager/src/subject.rs b/agent_secret_manager/src/subject.rs index 5c4d19e7..f3267ac0 100644 --- a/agent_secret_manager/src/subject.rs +++ b/agent_secret_manager/src/subject.rs @@ -1,4 +1,4 @@ -use agent_shared::config::config; +use agent_shared::{config::config, from_jsonwebtoken_algorithm_to_jwsalgorithm}; use async_trait::async_trait; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use did_manager::{DidMethod, Resolver, SecretManager}; @@ -62,13 +62,9 @@ impl Sign for Subject { .produce_document( method, Some(did_manager::MethodSpecificParameters::Web { origin: origin() }), - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm( + &agent_shared::config::get_preferred_signing_algorithm(), + ), ) .await .ok() @@ -82,13 +78,7 @@ impl Sign for Subject { .produce_document( method, None, - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm(&agent_shared::config::get_preferred_signing_algorithm()), ) .await .ok() @@ -101,13 +91,7 @@ impl Sign for Subject { .secret_manager .sign( message.as_bytes(), - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm(&agent_shared::config::get_preferred_signing_algorithm()), ) .await?) } @@ -128,13 +112,9 @@ impl oid4vc_core::Subject for Subject { .produce_document( method, Some(did_manager::MethodSpecificParameters::Web { origin: origin() }), - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm( + &agent_shared::config::get_preferred_signing_algorithm(), + ), ) .await .map(|document| document.id().to_string())?); @@ -145,13 +125,7 @@ impl oid4vc_core::Subject for Subject { .produce_document( method, None, - JwsAlgorithm::from_str( - &serde_json::json!(agent_shared::config::get_preferred_signing_algorithm()) - .as_str() - .unwrap() - .to_string(), - ) - .unwrap(), + from_jsonwebtoken_algorithm_to_jwsalgorithm(&agent_shared::config::get_preferred_signing_algorithm()), ) .await .map(|document| document.id().to_string())?) diff --git a/agent_shared/src/lib.rs b/agent_shared/src/lib.rs index 578573e0..e678f5bf 100644 --- a/agent_shared/src/lib.rs +++ b/agent_shared/src/lib.rs @@ -7,6 +7,7 @@ pub mod handlers; pub mod url_utils; pub use ::config::ConfigError; +use identity_iota::verification::jws::JwsAlgorithm; use rand::Rng; pub use url_utils::UrlAppendHelpers; @@ -24,3 +25,21 @@ pub fn generate_random_string() -> String { random_string } + +/// Helper function that converts `jsonwebtoken::Algorithm` to `JwsAlgorithm`. +pub fn from_jsonwebtoken_algorithm_to_jwsalgorithm(algorithm: &jsonwebtoken::Algorithm) -> JwsAlgorithm { + match algorithm { + jsonwebtoken::Algorithm::HS256 => JwsAlgorithm::HS256, + jsonwebtoken::Algorithm::HS384 => JwsAlgorithm::HS384, + jsonwebtoken::Algorithm::HS512 => JwsAlgorithm::HS512, + jsonwebtoken::Algorithm::ES256 => JwsAlgorithm::ES256, + jsonwebtoken::Algorithm::ES384 => JwsAlgorithm::ES384, + jsonwebtoken::Algorithm::RS256 => JwsAlgorithm::RS256, + jsonwebtoken::Algorithm::RS384 => JwsAlgorithm::RS384, + jsonwebtoken::Algorithm::RS512 => JwsAlgorithm::RS512, + jsonwebtoken::Algorithm::PS256 => JwsAlgorithm::PS256, + jsonwebtoken::Algorithm::PS384 => JwsAlgorithm::PS384, + jsonwebtoken::Algorithm::PS512 => JwsAlgorithm::PS512, + jsonwebtoken::Algorithm::EdDSA => JwsAlgorithm::EdDSA, + } +}