Skip to content

feat(utils): add key generation and token parsing utilities; update d… #21

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Christiantyemele
Copy link
Collaborator

@Christiantyemele Christiantyemele commented Mar 19, 2025

…ependencies
closes adorsys/didcomm-mediator-rs#348

@Christiantyemele Christiantyemele linked an issue Mar 19, 2025 that may be closed by this pull request
 into 18-create-function-for-jwt-or-cwt-generation
Copy link
Collaborator

@Blindspot22 Blindspot22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for more security measures, you can ensure the generated private key file is stored securely by verifying that KEY_STORAGE points to a secure location, such as a dedicated directory with limited access or explicitly set file permissions (fs::set_permissions) to restrict access.

i see what you mean here but this too is not enough security, i will prefer not to think the fully of security measure now, but this should be a future ticket

@Blindspot22
Copy link
Collaborator

Blindspot22 commented Mar 19, 2025

Why is the .lock file appearing here :)

@Christiantyemele Christiantyemele self-assigned this Mar 20, 2025
Copy link
Collaborator

@Hermann-Core Hermann-Core left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each PR should focus on a single independent feature or bug fix, rather than combining multiple changes in one, as seen here. This could have been split into two separate features.
Additionally, the title does not accurately reflect what is being implemented.

Can you review the comments I left below and address them?

jsonwebtoken = "9.3"
mongodb = "3.2"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the mongodb dep doing here? Are you using mongoDB?

@@ -0,0 +1,29 @@
use std::{env, fs, path::Path};

use rsa::{pkcs1::EncodeRsaPrivateKey, pkcs8::DecodePrivateKey, rand_core::OsRng, RsaPrivateKey};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use EC keys instead of RSA keys? They are shorter while providing the same level of security as RSA.

Ok(private_key)
} else {
tracing::info!("Generating new RSA key pair...");
let private_key = RsaPrivateKey::new(&mut OsRng, 2048)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you define a constant for the key size? so it will be easy to change it in the future if we want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing sensitive information, such as cryptographic keys, directly in the filesystem in plaintext is insecure. I recommend using the existing backend storage (the database) to store the keys and implementing a secure key management mechanism.


use crate::model::{StatusListToken, StatusType};

pub fn parse_token(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're encoding the token into the specified format, not parsing it. Consider choosing a more appropriate name for both the function and the module to reflect this.

) -> Result<String, Error> {
match status_type {
StatusType::JWT => {
let header = Header::default();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Status List token header is somewhat specialized, particularly the typ, alg, and possibly the x5c headers thus should not be populated with default values. Could you review it again?

Comment on lines +26 to +37
StatusType::CWT => {
// for feature implmentation
Ok(String::new())
// Serialize the token (claims) to CBOR bytes
// let claims_bytes = serde_cbor::to_vec(&token).map_err(|e| {
// tracing::error!("CBOR serialization failed: {}", e);
// std::io::Error::new(std::io::ErrorKind::Other, "failed to serialize CBOR claims")
// })?;

// // Return the CWT as a base64 or hex string if needed
// Ok(Base64Encoder::encode(&claims_bytes))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to complete the implementation before linking the PR? You've added dependencies for this feature that are not yet in use. I believe the codebase should remain clean and free of unused dependencies after each PR.

Comment on lines +122 to +131
impl FromStr for StatusType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"JWT" => Ok(Self::JWT),
"CWT" => Ok(Self::CWT),
_ => Err("Unknown status type".to_string()),
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused implementation.

@ndefokou
Copy link
Collaborator

@Christiantyemele, you should take a look at @Hermann-Core's reviews. They make a lot of sense, especially the ones concerning the keygen.

@Christiantyemele
Copy link
Collaborator Author

Christiantyemele commented Mar 24, 2025

i am converting this PR to draft now cause

  1. it contains too many fixes which are not related and i will prefer to split them down
  2. the scope it not too clear and needs to be reconsiderred

@Christiantyemele Christiantyemele marked this pull request as draft March 24, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create function for jwt or cwt generation Create function for jwt or/and cwt generation
4 participants