Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
feat: random nonce. Encryption with AEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakowskiii committed Feb 7, 2023
1 parent e984d26 commit 37ae988
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
19 changes: 15 additions & 4 deletions src/handlers/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use {
KeyInit,
},
mongodb::bson::doc,
rand::{distributions::Uniform, prelude::Distribution, Rng},
rand_core::OsRng,
serde::{Deserialize, Serialize},
std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -72,6 +74,13 @@ pub struct JsonRpcPayload {
params: PublishParams,
}

#[derive(Serialize)]
struct Envelope<'a> {
envelope_type: u8,
iv: [u8; 12],
sealbox: &'a [u8],
}

// Change String to Account
// Change String to Error
#[derive(Serialize, Deserialize)]
Expand All @@ -82,15 +91,12 @@ struct Response {
}

pub async fn handler(
// headers: HeaderMap,
Path(project_id): Path<String>,
State(state): State<Arc<AppState>>,
Json(cast_args): Json<CastArgs>,
) -> Result<axum::response::Response, error::Error> {
// impl IntoResponse {
let db = state.example_store.clone();

// let project_id = headers.get("Auth").unwrap().to_str().unwrap();
let mut confirmed_sends = HashSet::new();
let mut failed_sends = HashSet::new();
let notification_json = serde_json::to_string(&cast_args.notification).unwrap();
Expand All @@ -111,6 +117,9 @@ pub async fn handler(

let mut clients = HashMap::<String, Vec<(String, String)>>::new();

let mut rng = OsRng {};
let uniform = Uniform::from(0u8..=255);

while let Some(data) = cursor.try_next().await.unwrap() {
not_found.remove(&data.id);

Expand All @@ -120,7 +129,9 @@ pub async fn handler(
chacha20poly1305::ChaCha20Poly1305::new(GenericArray::from_slice(&encryption_key));

// TODO: proper nonce
let nonce = GenericArray::<u8, U12>::default();
let nonce: GenericArray<u8, U12> =
GenericArray::from_iter(uniform.sample_iter(&mut rng).take(12));
// let nonce = GenericArray::<u8, U12>::default();
let json = notification_json.clone();

let payload = Payload {
Expand Down
15 changes: 1 addition & 14 deletions src/handlers/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,20 @@ use {

#[derive(Serialize, Deserialize, Debug)]
// TODO: rename all camel case
#[serde(rename_all = "camelCase")]
pub struct RegisterBody {
account: Account,
#[serde(rename = "relayUrl")]
#[serde(default = "default_relay_url")]
relay_url: String,
#[serde(rename = "symKey")]
sym_key: String,
}

pub async fn handler(
// headers: HeaderMap,
Path(project_id): Path<String>,
State(state): State<Arc<AppState>>,
Json(data): Json<RegisterBody>,
) -> Result<axum::response::Response, crate::error::Error> {
let db = state.example_store.clone();

// Verify that the url is proper url and starting with websocket
// match url::Url::parse(&data.relay_url) {
// Err(_) => return todo!(),
// Ok(url) => {
// if url.scheme() != "wss" {
// return todo!();
// }
// }
// };

if url::Url::parse(&data.relay_url)?.scheme() != "wss" {
return Ok((
StatusCode::BAD_REQUEST,
Expand Down

0 comments on commit 37ae988

Please sign in to comment.