Skip to content

Commit 69fda24

Browse files
committed
Propagate ParseError when retrieving ohttp parameter
1 parent 0aef52e commit 69fda24

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

payjoin/src/uri/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ pub(crate) enum ParseReceiverPubkeyError {
2020
InvalidPubkey(crate::hpke::HpkeError),
2121
}
2222

23+
#[cfg(feature = "v2")]
24+
#[derive(Debug)]
25+
pub(crate) enum ParseOhttpError {
26+
MissingOhttp,
27+
InvalidOhttpKeys
28+
}
29+
2330
#[cfg(feature = "v2")]
2431
impl std::fmt::Display for ParseReceiverPubkeyError {
2532
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {

payjoin/src/uri/url_ext.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use bitcoin::consensus::encode::Decodable;
55
use bitcoin::consensus::Encodable;
66
use url::Url;
77

8-
use super::error::ParseReceiverPubkeyError;
8+
use super::error::{ParseExpError, ParseOhttpError, ParseReceiverPubkeyError};
99
use crate::hpke::HpkePublicKey;
1010
use crate::OhttpKeys;
1111

1212
/// Parse and set fragment parameters from `&pj=` URI parameter URLs
1313
pub(crate) trait UrlExt {
1414
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyError>;
1515
fn set_receiver_pubkey(&mut self, exp: HpkePublicKey);
16-
fn ohttp(&self) -> Option<OhttpKeys>;
16+
fn ohttp(&self) ->Result<OhttpKeys, ParseOhttpError>;
1717
fn set_ohttp(&mut self, ohttp: OhttpKeys);
18-
fn exp(&self) -> Option<std::time::SystemTime>;
18+
fn exp(&self) -> Result<std::time::SystemTime, ParseExpError>;
1919
fn set_exp(&mut self, exp: std::time::SystemTime);
2020
}
2121

@@ -50,8 +50,10 @@ impl UrlExt for Url {
5050
}
5151

5252
/// Retrieve the ohttp parameter from the URL fragment
53-
fn ohttp(&self) -> Option<OhttpKeys> {
54-
get_param(self, "OH1", |value| OhttpKeys::from_str(value).ok())
53+
fn ohttp(&self) -> Result<OhttpKeys, ParseOhttpError> {
54+
let value = get_param(self, "ohttp=", |v| Some(v.to_owned()))
55+
.ok_or(ParseOhttpError::MissingOhttp)?;
56+
OhttpKeys::from_str(&value).ok_or(ParseOhttpError::InvalidOhttpKeys)
5557
}
5658

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

144146
assert_eq!(url.fragment(), Some(serialized));
145-
assert_eq!(url.ohttp(), Some(ohttp_keys));
147+
assert_eq!(url.ohttp(), Ok(ohttp_keys));
148+
}
149+
150+
#[test]
151+
fn test_errors_when_parsing_ohttp() {
152+
let missing_ohttp_url = Url::parse("https://example.com").unwrap();
153+
assert!(matches!(missing_ohttp_url.ohttp(), Err(ParseOhttpError::MissingOhttp)));
154+
155+
let invalid_ohttp_url = Url::parse("https://example.com?ohttp=@@@@@@@").unwrap();
156+
assert!(matches!(invalid_ohttp_url.ohttp(), Err(ParseOhttpError::InvalidOhttpKeys)));
146157
}
147158

148159
#[test]

0 commit comments

Comments
 (0)