Skip to content

Commit

Permalink
Propagate ParseError when retrieving ohttp parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
shinghim committed Dec 4, 2024
1 parent 0aef52e commit 75662e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion payjoin/src/ohttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl serde::Serialize for OhttpKeys {

#[derive(Debug)]
pub enum ParseOhttpKeysError {
MissingOhttpKeys,
InvalidFormat,
InvalidPublicKey,
DecodeBech32(bech32::primitives::decode::CheckedHrpstringError),
Expand All @@ -246,6 +247,7 @@ pub enum ParseOhttpKeysError {
impl std::fmt::Display for ParseOhttpKeysError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParseOhttpKeysError::MissingOhttpKeys => write!(f, "Missing ohttp keys"),
ParseOhttpKeysError::InvalidFormat => write!(f, "Invalid format"),
ParseOhttpKeysError::InvalidPublicKey => write!(f, "Invalid public key"),
ParseOhttpKeysError::DecodeBech32(e) => write!(f, "Failed to decode base64: {}", e),
Expand All @@ -260,7 +262,7 @@ impl std::error::Error for ParseOhttpKeysError {
match self {
ParseOhttpKeysError::DecodeBech32(e) => Some(e),
ParseOhttpKeysError::DecodeKeyConfig(e) => Some(e),
ParseOhttpKeysError::InvalidFormat | ParseOhttpKeysError::InvalidPublicKey => None,
ParseOhttpKeysError::MissingOhttpKeys | ParseOhttpKeysError::InvalidFormat | ParseOhttpKeysError::InvalidPublicKey => None,
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions payjoin/src/uri/url_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use url::Url;

use super::error::ParseReceiverPubkeyError;
use crate::hpke::HpkePublicKey;
use crate::OhttpKeys;
use crate::ohttp::{OhttpKeys, ParseOhttpKeysError};

/// Parse and set fragment parameters from `&pj=` URI parameter URLs
pub(crate) trait UrlExt {
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyError>;
fn set_receiver_pubkey(&mut self, exp: HpkePublicKey);
fn ohttp(&self) -> Option<OhttpKeys>;
fn ohttp(&self) -> Result<OhttpKeys, ParseOhttpKeysError>;
fn set_ohttp(&mut self, ohttp: OhttpKeys);
fn exp(&self) -> Option<std::time::SystemTime>;
fn set_exp(&mut self, exp: std::time::SystemTime);
Expand Down Expand Up @@ -50,8 +50,10 @@ impl UrlExt for Url {
}

/// Retrieve the ohttp parameter from the URL fragment
fn ohttp(&self) -> Option<OhttpKeys> {
get_param(self, "OH1", |value| OhttpKeys::from_str(value).ok())
fn ohttp(&self) -> Result<OhttpKeys, ParseOhttpKeysError> {
let value = get_param(self, "OH1", |v| Some(v.to_owned()))
.ok_or(ParseOhttpKeysError::MissingOhttpKeys)?;
OhttpKeys::from_str(&value)
}

/// Set the ohttp parameter in the URL fragment
Expand Down Expand Up @@ -142,7 +144,16 @@ mod tests {
url.set_ohttp(ohttp_keys.clone());

assert_eq!(url.fragment(), Some(serialized));
assert_eq!(url.ohttp(), Some(ohttp_keys));
assert_eq!(url.ohttp(), Ok(ohttp_keys));
}

#[test]
fn test_errors_when_parsing_ohttp() {
let missing_ohttp_url = Url::parse("https://example.com").unwrap();
assert!(matches!(missing_ohttp_url.ohttp(), Err(ParseOhttpKeysError::MissingOhttpKeys)));

let invalid_ohttp_url = Url::parse("https://example.com?ohttp=@@@@@@@").unwrap();
assert!(matches!(invalid_ohttp_url.ohttp(), Err(ParseOhttpKeysError::InvalidFormat)));
}

#[test]
Expand Down

0 comments on commit 75662e7

Please sign in to comment.