Skip to content

Commit bb91861

Browse files
committed
fix: Use EdDSA in tests
1 parent 0ae8a12 commit bb91861

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

objectstore-server/src/auth/context.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,16 @@ mod tests {
230230
use serde_json::json;
231231

232232
const TEST_SIGNING_KID: &str = "test-key";
233-
const TEST_SIGNING_SECRET: &str = "fa24f0a3ab08f9ff0d4b2183595a045c";
233+
// Private key generated with `openssl genpkey -algorithm Ed25519`
234+
const TEST_PRIVATE_KEY: &str = r#"-----BEGIN PRIVATE KEY-----
235+
MC4CAQAwBQYDK2VwBCIEIAZtPzCHjltjZSi3+THxP6Rh8vUM0LRNA/QDR8zJx0tB
236+
-----END PRIVATE KEY-----
237+
"#;
238+
// Public key extracted with `openssl pkey -in private_key.pem -pubout`
239+
const TEST_PUBLIC_KEY: &str = r#"-----BEGIN PUBLIC KEY-----
240+
MCowBQYDK2VwAyEA/TOsO19FvHFTsZqcYiO8HGfm02Df5oWBXgzulxYPvSs=
241+
-----END PUBLIC KEY-----
242+
"#;
234243

235244
#[derive(Serialize, Deserialize)]
236245
struct TestJwtClaims {
@@ -248,7 +257,7 @@ mod tests {
248257
}
249258

250259
fn test_config(max_permissions: HashSet<Permission>) -> AuthZ {
251-
let wrapped_key = SecretBox::new(Box::new(ConfigSecret::from(TEST_SIGNING_SECRET)));
260+
let wrapped_key = SecretBox::new(Box::new(ConfigSecret::from(TEST_PUBLIC_KEY)));
252261
let key_config = AuthZVerificationKey {
253262
key_versions: vec![wrapped_key],
254263
max_permissions,
@@ -259,24 +268,20 @@ mod tests {
259268
}
260269
}
261270

262-
fn sign_token(claims: &JwtClaims, signing_secret: &str) -> String {
271+
fn sign_token(claims: &JwtClaims, signing_secret: &str, exp: Option<u64>) -> String {
263272
use jsonwebtoken::{Algorithm, EncodingKey, Header, encode, get_current_timestamp};
264273

265-
let mut header = Header::new(Algorithm::HS256);
274+
let mut header = Header::new(Algorithm::EdDSA);
266275
header.kid = Some(TEST_SIGNING_KID.into());
267276
header.typ = Some("JWT".into());
268277

269278
let claims = TestJwtClaims {
270-
exp: get_current_timestamp() + 300,
279+
exp: exp.unwrap_or_else(|| get_current_timestamp() + 300),
271280
claims: claims.clone(),
272281
};
273282

274-
encode(
275-
&header,
276-
&claims,
277-
&EncodingKey::from_secret(signing_secret.as_bytes()),
278-
)
279-
.unwrap()
283+
let key = EncodingKey::from_ed_pem(signing_secret.as_bytes()).unwrap();
284+
encode(&header, &claims, &key).unwrap()
280285
}
281286

282287
fn sample_claims(
@@ -309,7 +314,7 @@ mod tests {
309314
fn test_from_encoded_jwt_basic() -> Result<(), AuthError> {
310315
// Create a token with max permissions
311316
let claims = sample_claims("123", "456", "attachments", max_permission());
312-
let encoded_token = sign_token(&claims, TEST_SIGNING_SECRET);
317+
let encoded_token = sign_token(&claims, TEST_PRIVATE_KEY, None);
313318

314319
// Create test config with max permissions
315320
let test_config = test_config(max_permission());
@@ -327,7 +332,7 @@ mod tests {
327332
fn test_from_encoded_jwt_max_permissions_limit() -> Result<(), AuthError> {
328333
// Create a token with max permissions
329334
let claims = sample_claims("123", "456", "attachments", max_permission());
330-
let encoded_token = sign_token(&claims, TEST_SIGNING_SECRET);
335+
let encoded_token = sign_token(&claims, TEST_PRIVATE_KEY, None);
331336

332337
// Assign read-only permissions to the signing key in config
333338
let ro_permission = HashSet::from([Permission::ObjectRead]);
@@ -359,9 +364,12 @@ mod tests {
359364

360365
#[test]
361366
fn test_from_encoded_jwt_unknown_key_fails() -> Result<(), AuthError> {
362-
// Create a token with max permissions
363367
let claims = sample_claims("123", "456", "attachments", max_permission());
364-
let encoded_token = sign_token(&claims, "unknown signing key");
368+
let unknown_key = r#"-----BEGIN PRIVATE KEY-----
369+
MC4CAQAwBQYDK2VwBCIEIKwVoE4TmTfWoqH3HgLVsEcHs9PHNe+ar/Hp6e4To8pK
370+
-----END PRIVATE KEY-----
371+
"#;
372+
let encoded_token = sign_token(&claims, unknown_key, None);
365373

366374
// Create test config with max permissions
367375
let test_config = test_config(max_permission());
@@ -376,25 +384,12 @@ mod tests {
376384

377385
#[test]
378386
fn test_from_encoded_jwt_expired() -> Result<(), AuthError> {
379-
use jsonwebtoken::{Algorithm, EncodingKey, Header, encode, get_current_timestamp};
380-
381387
let claims = sample_claims("123", "456", "attachments", max_permission());
382-
383-
let mut header = Header::new(Algorithm::HS256);
384-
header.kid = Some(TEST_SIGNING_KID.into());
385-
header.typ = Some("JWT".into());
386-
387-
let claims = TestJwtClaims {
388-
exp: get_current_timestamp() - 100, // NB: expired
389-
claims: claims.clone(),
390-
};
391-
392-
let encoded_token = encode(
393-
&header,
388+
let encoded_token = sign_token(
394389
&claims,
395-
&EncodingKey::from_secret(TEST_SIGNING_SECRET.as_bytes()),
396-
)
397-
.unwrap();
390+
TEST_PRIVATE_KEY,
391+
Some(jsonwebtoken::get_current_timestamp() - 100),
392+
);
398393

399394
// Create test config with max permissions
400395
let test_config = test_config(max_permission());

0 commit comments

Comments
 (0)