Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ApiKey newtype to ensure key is always valid format #835

Merged
merged 10 commits into from
May 4, 2023
Prev Previous commit
Next Next commit
refactor: clean up tests
  • Loading branch information
oddgrd committed May 1, 2023
commit a475913600a21df316d38936f860e5353820d377
8 changes: 2 additions & 6 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ rustrict = { version = "0.7.4", optional = true }
serde = { workspace = true, features = ["derive", "std"] }
serde_json = { workspace = true }
strum = { workspace = true, features = ["derive"] }
sqlx = { workspace = true, optional = true, features = [
"sqlite",
"json",
"runtime-tokio-native-tls",
] }
sqlx = { workspace = true, optional = true, features = ["runtime-tokio-native-tls"] }
thiserror = { workspace = true, optional = true }
tonic = { workspace = true, optional = true }
tower = { workspace = true, optional = true }
Expand Down Expand Up @@ -80,7 +76,7 @@ display = ["chrono/clock", "comfy-table", "crossterm"]
error = ["prost-types", "thiserror", "uuid"]
openapi = ["utoipa/chrono", "utoipa/uuid"]
models = ["anyhow", "async-trait", "display", "http", "reqwest", "service"]
persist = ["sqlx", "rand"]
persist = ["sqlx/sqlite", "rand"]
service = ["chrono/serde", "once_cell", "rustrict", "serde/derive", "uuid"]
tracing = []
wasm = [
Expand Down
17 changes: 12 additions & 5 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ use anyhow::bail;
pub use log::Item as LogItem;
#[cfg(feature = "service")]
pub use log::STATE_MESSAGE;
#[cfg(feature = "persist")]
use rand::distributions::{Alphanumeric, DistString};
use serde::{Deserialize, Serialize};
#[cfg(feature = "service")]
use uuid::Uuid;
Expand Down Expand Up @@ -72,6 +70,8 @@ impl ApiKey {

#[cfg(feature = "persist")]
pub fn generate() -> Self {
use rand::distributions::{Alphanumeric, DistString};

Self(Alphanumeric.sample_string(&mut rand::thread_rng(), 16))
}
}
Expand Down Expand Up @@ -188,20 +188,27 @@ mod tests {
proptest! {
#[test]
// The API key should be a 16 character alphanumeric string.
fn parses_valid_keys(s in "[a-zA-Z0-9]{16}") {
fn parses_valid_api_keys(s in "[a-zA-Z0-9]{16}") {
ApiKey::parse(&s).unwrap();
}
}

#[test]
fn generated_api_key_is_valid() {
let key = ApiKey::generate();

assert!(ApiKey::parse(key.as_ref()).is_ok());
}

#[test]
#[should_panic(expected = "The API key should be exactly 16 characters in length.")]
fn invalid_length() {
fn invalid_api_key_length() {
ApiKey::parse("tooshort").unwrap();
}

#[test]
#[should_panic(expected = "The API key should consist of only alphanumeric characters.")]
fn non_alphanumeric() {
fn non_alphanumeric_api_key() {
ApiKey::parse("dh9z58jttoes3qv@").unwrap();
}
}