Skip to content

Commit

Permalink
borrow xqr when decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tizz98 committed Aug 12, 2023
1 parent 7a5c005 commit 6e95d34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- **Breaking change**: `decode` now borrows the XQR instead of taking ownership of it
- This allows the XQR to be reused after decoding

## [0.3.0] - 2023-08-11
### Added
- Add `CONTRIBUTING.md` and `CODE_OF_CONDUCT.md` for community guidelines
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ pub fn encode(
/// # Returns
///
/// A Result containing the decoded value as a String or an error if the operation fails.
pub fn decode(public_key_pem: &str, xqr: XQR) -> Result<String, Error> {
pub fn decode(public_key_pem: &str, xqr: &XQR) -> Result<String, Error> {
let public_key = ES256PublicKey::from_pem(public_key_pem)?;
let token = xqr.token;
let claims = public_key.verify_token::<XQRClaims>(&token, None)?.custom;
let claims = public_key
.verify_token::<XQRClaims>(&xqr.token, None)?
.custom;

Ok(claims.value)
}
Expand Down Expand Up @@ -159,10 +160,9 @@ mod tests {
let private_key = key_pair.to_pem().unwrap();
let public_key = key_pair.public_key().to_pem().unwrap();
let value = "value";
let kid = "example.com#123";

let encoded_xqr = encode(&private_key, value, kid, None).unwrap();
let decoded_value = decode(&public_key, encoded_xqr).unwrap();
let encoded_xqr = encode(&private_key, value, "example.com#123", None).unwrap();
let decoded_value = decode(&public_key, &encoded_xqr).unwrap();

assert_eq!(decoded_value, value);
}
Expand All @@ -176,7 +176,7 @@ mod tests {
let kid = "example.com#123";

let encoded_xqr = encode(&private_key, value, kid, None).unwrap();
let decoded_value = decode(&public_key, encoded_xqr);
let decoded_value = decode(&public_key, &encoded_xqr);

assert!(decoded_value.is_err());
}
Expand Down

0 comments on commit 6e95d34

Please sign in to comment.