Skip to content

Commit

Permalink
Feature - Add support for EdDSA (#238)
Browse files Browse the repository at this point in the history
* Feature - Add support for EdDSA

Does what it says on the tin:
- Adds EdDSA as a supported algo
- Adds EdDSA key loading

Signed-off-by: lloydmeta <lloydmeta@gmail.com>

* * Remove leftover dbg!

Signed-off-by: lloydmeta <lloydmeta@gmail.com>

* * appease clippy

Signed-off-by: lloydmeta <lloydmeta@gmail.com>

---------

Signed-off-by: lloydmeta <lloydmeta@gmail.com>
  • Loading branch information
lloydmeta committed Feb 1, 2023
1 parent a595bcd commit 59e06a6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update from jsonwebtoken 7 to 8
- Add Macports installation info #231
- Remove Gofish installation info. See #228
- Adds support for EdDSA algo

# 5.0.3

Expand Down
2 changes: 2 additions & 0 deletions src/cli_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub enum SupportedAlgorithms {
PS512,
ES256,
ES384,
EdDSA,
}

fn is_payload_item(val: &str) -> Result<Option<PayloadItem>, String> {
Expand Down Expand Up @@ -199,5 +200,6 @@ pub fn translate_algorithm(alg: &SupportedAlgorithms) -> Algorithm {
SupportedAlgorithms::PS512 => Algorithm::PS512,
SupportedAlgorithms::ES256 => Algorithm::ES256,
SupportedAlgorithms::ES384 => Algorithm::ES384,
SupportedAlgorithms::EdDSA => Algorithm::EdDSA,
}
}
7 changes: 6 additions & 1 deletion src/translators/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ pub fn decoding_key_from_secret(alg: &Algorithm, secret_string: &str) -> JWTResu
}
}
Algorithm::EdDSA => {
panic!("EdDSA is not implemented yet!");
let secret = slurp_file(&secret_string.chars().skip(1).collect::<String>());

match secret_string.ends_with(".pem") {
true => DecodingKey::from_ed_pem(&secret),
false => Ok(DecodingKey::from_ed_der(&secret)),
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/translators/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ pub fn encoding_key_from_secret(alg: &Algorithm, secret_string: &str) -> JWTResu
false => Ok(EncodingKey::from_ec_der(&secret)),
}
}
Algorithm::EdDSA => panic!("EdDSA is not implemented yet"),
Algorithm::EdDSA => {
let secret = slurp_file(&secret_string.chars().skip(1).collect::<String>());

match secret_string.ends_with(".pem") {
true => EncodingKey::from_ed_pem(&secret),
false => Ok(EncodingKey::from_ed_der(&secret)),
}
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/main_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,42 @@ mod tests {
assert!(result.is_ok());
}

#[test]
fn encodes_and_decodes_an_eddsa_token_using_key_from_file() {
let body: String = "{\"field\":\"value\"}".to_string();
let encode_matcher = App::command()
.try_get_matches_from(vec![
"jwt",
"encode",
"-A",
"EDDSA",
"--exp",
"-S",
"@./tests/private_eddsa_key.pem",
&body,
])
.unwrap();
let encode_matches = encode_matcher.subcommand_matches("encode").unwrap();
let encode_arguments = EncodeArgs::from_arg_matches(encode_matches).unwrap();
let encoded_token = encode_token(&encode_arguments).unwrap();
let decode_matcher = App::command()
.try_get_matches_from(vec![
"jwt",
"decode",
"-S",
"@./tests/public_eddsa_key.pem",
"-A",
"EDDSA",
&encoded_token,
])
.unwrap();
let decode_matches = decode_matcher.subcommand_matches("decode").unwrap();
let decode_arguments = DecodeArgs::from_arg_matches(decode_matches).unwrap();
let (result, _, _) = decode_token(&decode_arguments);

assert!(result.is_ok());
}

#[test]
fn shows_timestamps_as_iso_dates() {
let exp = (Utc::now() + Duration::minutes(60)).timestamp();
Expand Down
3 changes: 3 additions & 0 deletions tests/private_eddsa_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIOlt2x5aBpWjO8MNAiE7h9nfpZqFDXVBoRAuZu85fWMU
-----END PRIVATE KEY-----
3 changes: 3 additions & 0 deletions tests/public_eddsa_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEABzKf6VPTrsj8oqur2gHMkpRCl2DHxe04q0A8lV/QP+A=
-----END PUBLIC KEY-----

0 comments on commit 59e06a6

Please sign in to comment.