Skip to content

Commit 75662e7

Browse files
committed
Propagate ParseError when retrieving ohttp parameter
1 parent 0aef52e commit 75662e7

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

payjoin/src/ohttp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl serde::Serialize for OhttpKeys {
237237

238238
#[derive(Debug)]
239239
pub enum ParseOhttpKeysError {
240+
MissingOhttpKeys,
240241
InvalidFormat,
241242
InvalidPublicKey,
242243
DecodeBech32(bech32::primitives::decode::CheckedHrpstringError),
@@ -246,6 +247,7 @@ pub enum ParseOhttpKeysError {
246247
impl std::fmt::Display for ParseOhttpKeysError {
247248
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
248249
match self {
250+
ParseOhttpKeysError::MissingOhttpKeys => write!(f, "Missing ohttp keys"),
249251
ParseOhttpKeysError::InvalidFormat => write!(f, "Invalid format"),
250252
ParseOhttpKeysError::InvalidPublicKey => write!(f, "Invalid public key"),
251253
ParseOhttpKeysError::DecodeBech32(e) => write!(f, "Failed to decode base64: {}", e),
@@ -260,7 +262,7 @@ impl std::error::Error for ParseOhttpKeysError {
260262
match self {
261263
ParseOhttpKeysError::DecodeBech32(e) => Some(e),
262264
ParseOhttpKeysError::DecodeKeyConfig(e) => Some(e),
263-
ParseOhttpKeysError::InvalidFormat | ParseOhttpKeysError::InvalidPublicKey => None,
265+
ParseOhttpKeysError::MissingOhttpKeys | ParseOhttpKeysError::InvalidFormat | ParseOhttpKeysError::InvalidPublicKey => None,
264266
}
265267
}
266268
}

payjoin/src/uri/url_ext.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use url::Url;
77

88
use super::error::ParseReceiverPubkeyError;
99
use crate::hpke::HpkePublicKey;
10-
use crate::OhttpKeys;
10+
use crate::ohttp::{OhttpKeys, ParseOhttpKeysError};
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, ParseOhttpKeysError>;
1717
fn set_ohttp(&mut self, ohttp: OhttpKeys);
1818
fn exp(&self) -> Option<std::time::SystemTime>;
1919
fn set_exp(&mut self, exp: std::time::SystemTime);
@@ -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, ParseOhttpKeysError> {
54+
let value = get_param(self, "OH1", |v| Some(v.to_owned()))
55+
.ok_or(ParseOhttpKeysError::MissingOhttpKeys)?;
56+
OhttpKeys::from_str(&value)
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(ParseOhttpKeysError::MissingOhttpKeys)));
154+
155+
let invalid_ohttp_url = Url::parse("https://example.com?ohttp=@@@@@@@").unwrap();
156+
assert!(matches!(invalid_ohttp_url.ohttp(), Err(ParseOhttpKeysError::InvalidFormat)));
146157
}
147158

148159
#[test]

0 commit comments

Comments
 (0)