Skip to content

Commit

Permalink
feat: make fn encode async
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel committed May 7, 2024
1 parent a932af7 commit 35b4f8f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions oid4vc-core/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ where
Ok(jsonwebtoken::decode::<T>(jwt, &key, &validation)?.claims)
}

pub fn encode<C, S>(signer: Arc<S>, header: Header, claims: C, subject_syntax_type: &str) -> Result<String>
pub async fn encode<C, S>(signer: Arc<S>, header: Header, claims: C, subject_syntax_type: &str) -> Result<String>
where
C: Serialize,
S: Sign + ?Sized,
{
let kid = signer
.key_id(subject_syntax_type)
.await
.ok_or(anyhow!("No key identifier found."))?;

let jwt = JsonWebToken::new(header, claims).kid(kid);

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

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

let verifier = MockVerifier::new();
let (kid, algorithm) = extract_header(&encoded).unwrap();
Expand Down

0 comments on commit 35b4f8f

Please sign in to comment.