From d3f3f85a4d63b875dbd3c9c384bdd84ab21e211a Mon Sep 17 00:00:00 2001 From: Monadic Cat Date: Wed, 17 Apr 2024 19:44:37 -0500 Subject: [PATCH] change example to use twilight_util::signature_validation --- examples/Cargo.toml | 3 +-- examples/model-webhook-slash.rs | 18 +++++------------- twilight-util/Cargo.toml | 2 +- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 0f7642470a2..75ed6b3ad2b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -7,10 +7,8 @@ version = "0.0.0" [dev-dependencies] anyhow = { default-features = false, features = ["std"], version = "1" } -ed25519-dalek = "2" futures-util = { default-features = false, version = "0.3" } tokio-stream = { default-features = false, version = "0.1" } -hex = "0.4" http-body-util = "0.1" hyper = { features = ["server"], version = "1" } hyper-util = { features = ["http1", "client-legacy"], version = "0.1" } @@ -27,6 +25,7 @@ twilight-http = { path = "../twilight-http" } twilight-lavalink = { path = "../twilight-lavalink" } twilight-model = { path = "../twilight-model" } twilight-standby = { path = "../twilight-standby" } +twilight-util = { path = "../twilight-util", features = ["signature-validation"] } [[example]] name = "cache-optimization" diff --git a/examples/model-webhook-slash.rs b/examples/model-webhook-slash.rs index 96a9a42ba39..a96077ba193 100644 --- a/examples/model-webhook-slash.rs +++ b/examples/model-webhook-slash.rs @@ -1,5 +1,3 @@ -use ed25519_dalek::{Verifier, VerifyingKey, PUBLIC_KEY_LENGTH}; -use hex::FromHex; use http_body_util::{BodyExt, Full}; use hyper::{ body::{Bytes, Incoming}, @@ -19,12 +17,10 @@ use twilight_model::{ }, http::interaction::{InteractionResponse, InteractionResponseData, InteractionResponseType}, }; +use twilight_util::signature_validation::Key; /// Public key given from Discord. -static PUB_KEY: Lazy = Lazy::new(|| { - VerifyingKey::from_bytes(&<[u8; PUBLIC_KEY_LENGTH] as FromHex>::from_hex("PUBLIC_KEY").unwrap()) - .unwrap() -}); +static PUB_KEY: Lazy = Lazy::new(|| Key::from_hex("PUBLIC_KEY".as_bytes()).unwrap()); /// Main request handler which will handle checking the signature. /// @@ -66,10 +62,9 @@ where // Extract the signature to check against. let signature = if let Some(hex_sig) = req .headers() - .get("x-signature-ed25519") - .and_then(|v| v.to_str().ok()) + .get(twilight_util::signature_validation::SIGNATURE_HEADER) { - hex_sig.parse().unwrap() + hex_sig.to_owned() } else { return Ok(Response::builder() .status(StatusCode::BAD_REQUEST) @@ -82,10 +77,7 @@ where // Check if the signature matches and else return a error response. if PUB_KEY - .verify( - [timestamp.as_bytes(), &whole_body].concat().as_ref(), - &signature, - ) + .verify(signature.as_bytes(), timestamp.as_bytes(), &whole_body) .is_err() { return Ok(Response::builder() diff --git a/twilight-util/Cargo.toml b/twilight-util/Cargo.toml index 00e9c5926a8..468c761acda 100644 --- a/twilight-util/Cargo.toml +++ b/twilight-util/Cargo.toml @@ -17,7 +17,7 @@ twilight-model = { default-features = false, optional = true, path = "../twiligh twilight-validate = { default-features = false, optional = true, path = "../twilight-validate", version = "0.16.0-rc.1" } # Signature validation -ed25519-dalek = { version = "2.1.0", optional = true, default-features = false } +ed25519-dalek = { version = "2.1.0", optional = true, default-features = false, features = ["std"] } hex = { version = "0.4.3", optional = true, default-features = false, features = ["std"] } serde_json = { optional = true, version = "1.0.96", default-features = false, features = ["std"] }