You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been looking in to implementing some HMAC based authentication with Rocket, the main stickler being access to the unadulterated body content at the same time as making use of the json/msgpack formatters.
In this example a basic PKI is used to authenticate requests as originating from someone with a public key signed by a specific certificate authority.
The intention is to provoke discussion rather than present a solution - adding a 'meta' field to the Request struct seems kludgy.
The change enables the following fiaring:
#[rocket::async_trait]implFairingforChecksumFairing{fninfo(&self) -> Info{Info{name:"Data Peeker",kind:Kind::Request,}}asyncfnon_request(&self,req:&mutRequest<'_>,data:&mutData<'_>){// Guards for content-type (application/json) and content-length (less than 1 MiB)if req.content_type().is_none(){return;}ifletSome(content_type) = req.content_type(){if*content_type != rocket::http::ContentType::JSON{return;}}if req.headers().contains("Content-Length") == false{return;}ifletSome(length) = req.headers().get_one("Content-Length"){if length.parse::<u64>().unwrap_or(1*1024*1024) >= 1*1024*1024{return;}}// Create an empty Data object and swap with borrowed data referenceletmut swap_data = rocket::data::Data::local(vec![]);
std::mem::swap(data,&mut swap_data);// Get the message contentlet request_content = swap_data
.open(1.megabytes()).into_bytes().await.unwrap().value;let md5_digest = md5::compute(&request_content).0;
req.meta.insert("md5".into(), general_purpose::STANDARD.encode(&md5_digest));letmut sha256_hasher = Sha256::new();
sha256_hasher.update(request_content.as_slice());let sha256_digest = sha256_hasher.finalize();
req.meta.insert("sha256".into(),
general_purpose::STANDARD.encode(&sha256_digest),);// Put the data in to a new Data object and swap it back in with the borrowed data referenceletmut new_data = rocket::data::Data::local(request_content);
std::mem::swap(data,&mut new_data);}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have been looking in to implementing some HMAC based authentication with Rocket, the main stickler being access to the unadulterated body content at the same time as making use of the json/msgpack formatters.
Following the discussions here #775, I have created a very draft PR here #2575 and a project using it here https://github.com/martynp/rocket_hmac.
In this example a basic PKI is used to authenticate requests as originating from someone with a public key signed by a specific certificate authority.
The intention is to provoke discussion rather than present a solution - adding a 'meta' field to the Request struct seems kludgy.
The change enables the following fiaring:
Beta Was this translation helpful? Give feedback.
All reactions