Skip to content

Commit

Permalink
feat: add from_jsonwebtoken_algorithm_to_jwsalgorithm helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel committed Jul 29, 2024
1 parent e9a468a commit 6cd81b0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
14 changes: 5 additions & 9 deletions agent_application/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
46 changes: 10 additions & 36 deletions agent_secret_manager/src/subject.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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?)
}
Expand All @@ -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())?);
Expand All @@ -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())?)
Expand Down
19 changes: 19 additions & 0 deletions agent_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
}
}

0 comments on commit 6cd81b0

Please sign in to comment.