mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
standardize examples
This commit is contained in:
@ -9,7 +9,7 @@ actix-web.workspace = true
|
||||
actix-web-lab.workspace = true
|
||||
|
||||
aes-gcm-siv = "0.11"
|
||||
base64 = "0.20"
|
||||
base64 = "0.21"
|
||||
env_logger.workspace = true
|
||||
futures-util.workspace = true
|
||||
log.workspace = true
|
||||
|
@ -14,10 +14,11 @@ All this to say, it provides a (fairly brittle) way to have handlers not need to
|
||||
|
||||
## Usage
|
||||
|
||||
Using [HTTPie] throughout for simplicity.
|
||||
|
||||
```console
|
||||
$ http POST :8080/encrypt id:=1234 data=abcde > tmp.json
|
||||
POST /reverse HTTP/1.1
|
||||
Content-Length: 76
|
||||
HTTP/1.1 200 OK
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
@ -28,7 +29,6 @@ Content-Type: application/json
|
||||
|
||||
$ cat tmp.json | http -v POST :8080/reverse
|
||||
HTTP/1.1 200 OK
|
||||
Content-Length: 66
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
@ -49,3 +49,5 @@ The server logs would look something like
|
||||
[INFO middleware_encrypted_payloads] encrypting response 1234
|
||||
[INFO actix_web::middleware::logger] 127.0.0.1 "POST /reverse HTTP/1.1" 200 66 "-" "-" 0.000425
|
||||
```
|
||||
|
||||
[httpie]: https://httpie.io/cli
|
||||
|
@ -7,6 +7,7 @@ use actix_web::{
|
||||
};
|
||||
use actix_web_lab::middleware::{from_fn, Next};
|
||||
use aes_gcm_siv::{aead::Aead as _, Aes256GcmSiv, KeyInit as _, Nonce};
|
||||
use base64::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@ -34,10 +35,10 @@ async fn make_encrypted(
|
||||
|
||||
// this nonce should actually be unique per message in a production environment
|
||||
let nonce = Nonce::from_slice(b"unique nonce");
|
||||
let nonce_b64 = Some(base64::encode(nonce));
|
||||
let nonce_b64 = Some(BASE64_STANDARD.encode(nonce));
|
||||
|
||||
let data_enc = cipher.encrypt(nonce, data.as_bytes()).unwrap();
|
||||
let data_enc = base64::encode(data_enc);
|
||||
let data_enc = BASE64_STANDARD.encode(data_enc);
|
||||
|
||||
web::Json(Req {
|
||||
id,
|
||||
@ -98,11 +99,11 @@ async fn encrypt_payloads(
|
||||
log::info!("decrypting request {id:?}");
|
||||
|
||||
// decode nonce from payload
|
||||
let nonce = base64::decode(nonce.unwrap()).unwrap();
|
||||
let nonce = BASE64_STANDARD.decode(nonce.unwrap()).unwrap();
|
||||
let nonce = Nonce::from_slice(&nonce);
|
||||
|
||||
// decode and decrypt data field
|
||||
let data_enc = base64::decode(&data).unwrap();
|
||||
let data_enc = BASE64_STANDARD.decode(&data).unwrap();
|
||||
let data = cipher.decrypt(nonce, data_enc.as_slice()).unwrap();
|
||||
|
||||
// construct request body format with plaintext data
|
||||
@ -136,11 +137,11 @@ async fn encrypt_payloads(
|
||||
|
||||
// generate and encode nonce for later
|
||||
let nonce = Nonce::from_slice(b"unique nonce");
|
||||
let nonce_b64 = Some(base64::encode(nonce));
|
||||
let nonce_b64 = Some(BASE64_STANDARD.encode(nonce));
|
||||
|
||||
// encrypt and encode data field
|
||||
let data_enc = cipher.encrypt(nonce, data.as_bytes()).unwrap();
|
||||
let data_enc = base64::encode(data_enc);
|
||||
let data_enc = BASE64_STANDARD.encode(data_enc);
|
||||
|
||||
// re-pack response into JSON format
|
||||
let res_body = Res {
|
||||
|
Reference in New Issue
Block a user