Skip to content

Commit

Permalink
adjust things to satisfy clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Monadic-Cat committed Jan 19, 2024
1 parent 6366b39 commit ff22ff0
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions twilight-util/src/signature_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ use twilight_model::application::interaction::Interaction;

/// Parsing a hexadecimal string failed.
#[derive(Debug)]
pub struct FromHexError(hex::FromHexError);
pub struct FromHexError(#[allow(dead_code)] hex::FromHexError);

impl std::fmt::Display for FromHexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}

impl std::error::Error for FromHexError {}

/// A signature or public key was invalid.
#[derive(Debug)]
Expand All @@ -20,15 +28,15 @@ pub struct SigError(SignatureError);
/// you received an invalid request.
#[derive(Debug)]
pub enum SignatureValidationFailure {
/// Parsing a hexadecimal string failed.
/// The request signature was invalid hexadecimal.
Hex(FromHexError),
/// Request had invalid signature for the given public key.
InvalidSignature(SigError),
}

impl std::fmt::Display for SignatureValidationFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand All @@ -45,7 +53,7 @@ pub enum KeyError {

impl std::fmt::Display for KeyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand All @@ -59,12 +67,16 @@ impl Key {
/// This function consumes the hexadecimal string which Discord
/// provides public keys in. Use `.as_bytes()` on a `&str`, or otherwise
/// obtain a byte-string of that text, to use with this function.
///
/// # Errors
/// This will fail if given invalid hexadecimal, or if the public key fails to
/// meet mathematical requirements.
pub fn from_hex(pub_key: &[u8]) -> Result<Self, KeyError> {
let mut key = [0; 32];
hex::decode_to_slice(pub_key, &mut key).map_err(|e| KeyError::Hex(FromHexError(e)))?;
VerifyingKey::from_bytes(&key)
.map(|key| Self(key))
.map_err(|e| KeyError::MalformedKey(e))
.map(Self)
.map_err(KeyError::MalformedKey)
}
}

Expand All @@ -74,6 +86,9 @@ pub const SIGNATURE_HEADER: &str = "x-signature-ed25519";
pub const TIMESTAMP_HEADER: &str = "x-signature-timestamp";

/// Validates that a signature is valid for a given message body, timestamp, and signing key.
///
/// # Errors
/// This will fail if the request being validated has an invalid signature.
pub fn check_signature(
sig: &[u8],
timestamp: &[u8],
Expand Down Expand Up @@ -114,6 +129,9 @@ impl From<serde_json::Error> for ExtractFailure {
}

/// Validate an Interaction's signature, and deserialize it from JSON.
///
/// # Errors
/// This will fail if the request being validated has an invalid signature.
pub fn extract_interaction(
sig: &[u8],
timestamp: &[u8],
Expand Down

0 comments on commit ff22ff0

Please sign in to comment.