-
Notifications
You must be signed in to change notification settings - Fork 79
feat: implement ohttp gateway middleware #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Harshdev098
wants to merge
3
commits into
payjoin:master
Choose a base branch
from
Harshdev098:ohttpGateway-middleware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use std::io::Cursor; | ||
|
|
||
| pub const CHACHA20_POLY1305_NONCE_LEN: usize = 32; | ||
| pub const POLY1305_TAG_SIZE: usize = 16; | ||
| pub const OHTTP_OVERHEAD: usize = CHACHA20_POLY1305_NONCE_LEN + POLY1305_TAG_SIZE; | ||
| pub const ENCAPSULATED_MESSAGE_BYTES: usize = 8192; | ||
| pub const BHTTP_REQ_BYTES: usize = ENCAPSULATED_MESSAGE_BYTES - OHTTP_OVERHEAD; | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum GatewayError { | ||
| BadRequest(String), | ||
| OhttpKeyRejection(String), | ||
| InternalServerError(String), | ||
| } | ||
|
|
||
| impl std::fmt::Display for GatewayError { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| GatewayError::BadRequest(msg) => write!(f, "Bad request: {}", msg), | ||
| GatewayError::OhttpKeyRejection(msg) => write!(f, "OHTTP key rejection: {}", msg), | ||
| GatewayError::InternalServerError(msg) => write!(f, "Internal server error: {}", msg), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for GatewayError {} | ||
|
|
||
| pub struct DecapsulatedRequest { | ||
| pub method: String, | ||
| pub uri: String, | ||
| pub headers: Vec<(String, String)>, | ||
| pub body: Vec<u8>, | ||
| } | ||
|
|
||
| pub fn decapsulate_ohttp_request( | ||
| ohttp_body: &[u8], | ||
| ohttp_server: &ohttp::Server, | ||
| ) -> Result<(DecapsulatedRequest, ohttp::ServerResponse), GatewayError> { | ||
| let (bhttp_req, res_ctx) = ohttp_server.decapsulate(ohttp_body).map_err(|e| { | ||
| GatewayError::OhttpKeyRejection(format!("OHTTP decapsulation failed: {}", e)) | ||
| })?; | ||
|
|
||
| let mut cursor = Cursor::new(bhttp_req); | ||
| let bhttp_msg = bhttp::Message::read_bhttp(&mut cursor) | ||
| .map_err(|e| GatewayError::BadRequest(format!("Invalid BHTTP: {}", e)))?; | ||
|
|
||
| let method = String::from_utf8(bhttp_msg.control().method().unwrap_or_default().to_vec()) | ||
| .unwrap_or_else(|_| "GET".to_string()); | ||
|
|
||
| let uri = format!( | ||
| "{}://{}{}", | ||
| std::str::from_utf8(bhttp_msg.control().scheme().unwrap_or_default()).unwrap_or("https"), | ||
| std::str::from_utf8(bhttp_msg.control().authority().unwrap_or_default()) | ||
| .unwrap_or("localhost"), | ||
| std::str::from_utf8(bhttp_msg.control().path().unwrap_or_default()).unwrap_or("/") | ||
| ); | ||
|
|
||
| let mut headers = Vec::new(); | ||
| for field in bhttp_msg.header().fields() { | ||
| let name = String::from_utf8_lossy(field.name()).to_string(); | ||
| let value = String::from_utf8_lossy(field.value()).to_string(); | ||
| headers.push((name, value)); | ||
| } | ||
|
|
||
| let body = bhttp_msg.content().to_vec(); | ||
|
|
||
| Ok((DecapsulatedRequest { method, uri, headers, body }, res_ctx)) | ||
| } | ||
|
|
||
| pub fn encapsulate_ohttp_response( | ||
| status_code: u16, | ||
| headers: Vec<(String, String)>, | ||
| body: Vec<u8>, | ||
| res_ctx: ohttp::ServerResponse, | ||
| ) -> Result<Vec<u8>, GatewayError> { | ||
| let bhttp_status = bhttp::StatusCode::try_from(status_code) | ||
| .map_err(|e| GatewayError::InternalServerError(format!("Invalid status code: {}", e)))?; | ||
|
|
||
| let mut bhttp_res = bhttp::Message::response(bhttp_status); | ||
|
|
||
| for (name, value) in &headers { | ||
| bhttp_res.put_header(name.as_str(), value.as_str()); | ||
| } | ||
|
|
||
| bhttp_res.write_content(&body); | ||
|
|
||
| let mut bhttp_bytes = Vec::new(); | ||
| bhttp_res.write_bhttp(bhttp::Mode::KnownLength, &mut bhttp_bytes).map_err(|e| { | ||
| GatewayError::InternalServerError(format!("BHTTP serialization failed: {}", e)) | ||
| })?; | ||
|
|
||
| if bhttp_bytes.len() > BHTTP_REQ_BYTES { | ||
| return Err(GatewayError::InternalServerError(format!( | ||
| "BHTTP response too large: {} > {}", | ||
| bhttp_bytes.len(), | ||
| BHTTP_REQ_BYTES | ||
| ))); | ||
| } | ||
|
|
||
| bhttp_bytes.resize(BHTTP_REQ_BYTES, 0); | ||
|
|
||
| let ohttp_res = res_ctx.encapsulate(&bhttp_bytes).map_err(|e| { | ||
| GatewayError::InternalServerError(format!("OHTTP encapsulation failed: {}", e)) | ||
| })?; | ||
|
|
||
| if ohttp_res.len() != ENCAPSULATED_MESSAGE_BYTES { | ||
| return Err(GatewayError::InternalServerError(format!( | ||
| "Unexpected OHTTP response size: {} != {}", | ||
| ohttp_res.len(), | ||
| ENCAPSULATED_MESSAGE_BYTES | ||
| ))); | ||
| } | ||
|
|
||
| Ok(ohttp_res) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had my reservations about the error variant returned but then again the primary reason decapsulation fails most times its due to invalid keys , so maybe this can stay this way