Skip to content

Commit

Permalink
change example to use twilight_util::signature_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Monadic-Cat committed Apr 18, 2024
1 parent c8f39c6 commit d3f3f85
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
3 changes: 1 addition & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"
Expand Down
18 changes: 5 additions & 13 deletions examples/model-webhook-slash.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<VerifyingKey> = Lazy::new(|| {
VerifyingKey::from_bytes(&<[u8; PUBLIC_KEY_LENGTH] as FromHex>::from_hex("PUBLIC_KEY").unwrap())
.unwrap()
});
static PUB_KEY: Lazy<Key> = Lazy::new(|| Key::from_hex("PUBLIC_KEY".as_bytes()).unwrap());

/// Main request handler which will handle checking the signature.
///
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion twilight-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down

0 comments on commit d3f3f85

Please sign in to comment.