Skip to content

Commit

Permalink
deps!: migrate to ed25519-dalek
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Dec 20, 2023
1 parent 44b1fdd commit cff01bd
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 9 deletions.
138 changes: 137 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ edition = "2021"
[dependencies]
dashmap = { version = "5.5", default-features = false }
db = { path = "../db", package = "quizzo-db" }
ed25519-dalek = "2.1"
hex = { version = "0.4", default-features = false }
http-body-util = "0.1.0"
hyper = { version = "1", default-features = false }
log = "0.4"
ring = { version = "0.17", default-features = false }
serde_json = "1"
twilight-model = "0.15"

Expand Down
11 changes: 6 additions & 5 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ use hyper::{
body::{Bytes, Incoming},
Request, Response, StatusCode,
};
use ring::signature::UnparsedPublicKey;

pub use db::{Client, Config, Database, NoTls};
pub use ed25519_dalek::VerifyingKey;

pub struct App {
/// Command handler.
bot: Bot,
/// Ed25519 public key.
public: UnparsedPublicKey<Box<[u8]>>,
public: VerifyingKey,
}

impl App {
pub fn new(db: Database, id: NonZeroU64, token: String, public: Box<[u8]>) -> Self {
Self { bot: Bot::new(db, id, token), public: UnparsedPublicKey::new(&ring::signature::ED25519, public) }
pub fn new(db: Database, id: NonZeroU64, token: String, public: VerifyingKey) -> Self {
Self { bot: Bot::new(db, id, token), public }
}

pub async fn try_respond(&self, req: Request<Incoming>) -> Result<Response<Full<Bytes>>, StatusCode> {
Expand Down Expand Up @@ -59,6 +59,7 @@ impl App {
let (sig, timestamp) = signature.zip(timestamp).ok_or(StatusCode::UNAUTHORIZED)?;
let mut signature = [0; 64];
hex::decode_to_slice(sig, &mut signature).map_err(|_| StatusCode::BAD_REQUEST)?;
let signature = ed25519_dalek::Signature::from_bytes(&signature);

// Append body after the timestamp
use http_body_util::BodyExt;
Expand All @@ -79,7 +80,7 @@ impl App {
log::debug!("Fully received payload body.");

// Validate the challenge
self.public.verify(&message, &signature).map_err(|_| StatusCode::UNAUTHORIZED)?;
self.public.verify_strict(&message, &signature).map_err(|_| StatusCode::UNAUTHORIZED)?;

// Parse incoming interaction
let payload = message.get(start..).ok_or(StatusCode::BAD_REQUEST)?;
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ fn main() -> anyhow::Result<()> {

// Retrieve the public key
use std::env::{var, VarError};
let key = var("PUB_KEY")?;
let pub_key = hex::decode(key)?.into_boxed_slice();
let pub_key = var("PUB_KEY")?.into_bytes();
let mut pub_bytes = [0; 32];
hex::decode_to_slice(pub_key, &mut pub_bytes)?;
let pub_key = api::VerifyingKey::from_bytes(&pub_bytes)?;

// Set up Postgres driver configuration
let app_port = var("PORT")?.parse()?;
Expand Down

0 comments on commit cff01bd

Please sign in to comment.