-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge https://github.com/juspay/tartarus into benches
- Loading branch information
Showing
11 changed files
with
619 additions
and
452 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,63 +1,50 @@ | ||
use crate::app::AppState; | ||
use crate::crypto::jw::JWEncryption; | ||
use crate::crypto::jw::{self, JWEncryption}; | ||
use crate::crypto::Encryption; | ||
use crate::error::{self, ContainerError, ResultContainerExt}; | ||
use axum::{ | ||
body::BoxBody, | ||
extract, | ||
http::{Request, Response}, | ||
middleware::Next, | ||
}; | ||
use error_stack::ResultExt; | ||
use hyper::body::HttpBody; | ||
use hyper::Body; | ||
use axum::body::Body; | ||
use axum::http::{request, response}; | ||
use axum::{extract, http::Request, middleware::Next}; | ||
|
||
use http_body_util::BodyExt; | ||
use josekit::jwe; | ||
|
||
/// Middleware providing implementation to perform JWE + JWS encryption and decryption around the | ||
/// card APIs | ||
pub async fn middleware( | ||
extract::State(state): extract::State<AppState>, | ||
request: Request<Body>, | ||
next: Next<Body>, | ||
) -> Result<Response<BoxBody>, ContainerError<error::ApiError>> { | ||
let (parts, body) = request.into_parts(); | ||
|
||
let request_body = | ||
hyper::body::to_bytes(body) | ||
.await | ||
.change_error(error::ApiError::RequestMiddlewareError( | ||
"Failed to read request body for jwe decryption", | ||
))?; | ||
|
||
parts: request::Parts, | ||
axum::Json(jwe_body): axum::Json<jw::JweBody>, | ||
next: Next, | ||
) -> Result<(response::Parts, axum::Json<jw::JweBody>), ContainerError<error::ApiError>> { | ||
let keys = JWEncryption { | ||
private_key: state.config.secrets.locker_private_key, | ||
public_key: state.config.secrets.tenant_public_key, | ||
encryption_algo: jwe::RSA_OAEP, | ||
decryption_algo: jwe::RSA_OAEP_256, | ||
}; | ||
|
||
let jwe_decrypted = keys.decrypt(request_body.to_vec())?; | ||
let jwe_decrypted = keys.decrypt(jwe_body)?; | ||
|
||
let next_layer_payload = Request::from_parts(parts, Body::from(jwe_decrypted)); | ||
|
||
let response = next.run(next_layer_payload).await; | ||
|
||
let (parts, body) = response.into_parts(); | ||
let (mut parts, body) = next.run(next_layer_payload).await.into_parts(); | ||
|
||
let response_body = hyper::body::to_bytes(body).await.change_error( | ||
error::ApiError::ResponseMiddlewareError("Failed to read response body for jws signing"), | ||
)?; | ||
let response_body = body | ||
.collect() | ||
.await | ||
.change_error(error::ApiError::ResponseMiddlewareError( | ||
"Failed to read response body for jws signing", | ||
))? | ||
.to_bytes(); | ||
|
||
let jws_signed = keys.encrypt(response_body.to_vec())?; | ||
let jwe_payload = keys.encrypt(response_body.to_vec())?; | ||
|
||
let jwt = String::from_utf8(jws_signed).change_error( | ||
error::ApiError::ResponseMiddlewareError("Could not convert to UTF-8"), | ||
)?; | ||
parts.headers = hyper::HeaderMap::new(); | ||
parts.headers.append( | ||
hyper::header::CONTENT_TYPE, | ||
axum::http::HeaderValue::from_static("application/json"), | ||
); | ||
|
||
Ok(axum::http::response::Builder::new() | ||
.status(parts.status) | ||
.body(jwt.map_err(axum::Error::new).boxed_unsync()) | ||
.change_context(error::ApiError::ResponseMiddlewareError( | ||
"failed while generating the response", | ||
))?) | ||
Ok((parts, axum::Json(jwe_payload))) | ||
} |
This file contains 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.