Skip to content

Commit

Permalink
Better stdout and stderr interop
Browse files Browse the repository at this point in the history
Minor changes
- Errors are now printed to STDERR instead of STDOUT
- Proper exit codes should now be emitted. `0` for successes, `1` for
failures.
- The output from the encode command is now just the token, which can be
piped or stored in a shell
  • Loading branch information
mike-engel committed Sep 5, 2017
1 parent 53a9fb7 commit 71c1b3b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 1.2.0
> 2017-09-05
Better stdout and stderr interop

#### Minor changes
- Errors are now printed to STDERR instead of STDOUT
- Proper exit codes should now be emitted. `0` for successes, `1` for failures.
- The output from the encode command is now just the token, which can be piped or stored in a shell

# 1.1.0
> 2017-07-13
Expand Down
2 changes: 1 addition & 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jwt-cli"
version = "1.1.0"
version = "1.2.0"
authors = ["Mike Engel <[email protected]>"]

[[bin]]
Expand Down
55 changes: 31 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use jwt::{Algorithm, decode, encode, Header, TokenData, Validation};
use jwt::errors::{Error, ErrorKind, Result as JWTResult};
use serde_json::{from_str, to_string_pretty, Value};
use std::collections::BTreeMap;
use std::process::exit;
use term_painter::ToStyle;
use term_painter::Color::*;
use term_painter::Attr::*;
Expand Down Expand Up @@ -349,78 +350,79 @@ fn decode_token(matches: &ArgMatches) -> (JWTResult<TokenData<Payload>>, TokenDa
fn print_encoded_token(token: JWTResult<String>) {
match token {
Ok(jwt) => {
println!("{}", Cyan.bold().paint("Success! Here's your token\n"));
println!("{}", jwt);
exit(0);
}
Err(err) => {
println!(
eprintln!(
"{}",
Red.bold().paint("Something went awry creating the jwt\n")
);
println!("{}", err);
eprintln!("{}", err);
exit(1);
}
}
}

fn print_decoded_token(validated_token: JWTResult<TokenData<Payload>>, token_data: TokenData<Payload>) {
println!("\n");

match validated_token {
Err(Error(err, _)) => {
match &validated_token {
&Err(Error(ref err, _)) => {
match err {
ErrorKind::InvalidToken => println!("{}", Red.bold().paint("The JWT provided is invalid")),
ErrorKind::InvalidSignature => {
println!(
&ErrorKind::InvalidToken => println!("{}", Red.bold().paint("The JWT provided is invalid")),
&ErrorKind::InvalidSignature => {
eprintln!(
"{}",
Red.bold().paint(
"The JWT provided has an invalid signature",
)
)
}
ErrorKind::InvalidKey => {
println!(
&ErrorKind::InvalidKey => {
eprintln!(
"{}",
Red.bold().paint(
"The secret provided isn't a valid RSA key",
)
)
}
ErrorKind::ExpiredSignature => println!("{}", Red.bold().paint("The token has expired")),
ErrorKind::InvalidIssuer => println!("{}", Red.bold().paint("The token issuer is invalid")),
ErrorKind::InvalidAudience => {
println!(
&ErrorKind::ExpiredSignature => println!("{}", Red.bold().paint("The token has expired")),
&ErrorKind::InvalidIssuer => println!("{}", Red.bold().paint("The token issuer is invalid")),
&ErrorKind::InvalidAudience => {
eprintln!(
"{}",
Red.bold().paint(
"The token audience doesn't match the subject",
)
)
}
ErrorKind::InvalidSubject => {
println!(
&ErrorKind::InvalidSubject => {
eprintln!(
"{}",
Red.bold().paint(
"The token subject doesn't match the audience",
)
)
}
ErrorKind::InvalidIssuedAt => {
println!(
&ErrorKind::InvalidIssuedAt => {
eprintln!(
"{}",
Red.bold().paint(
"The issued at claim is in the future which isn't allowed",
)
)
}
ErrorKind::ImmatureSignature => {
println!(
&ErrorKind::ImmatureSignature => {
eprintln!(
"{}",
Red.bold().paint(
"The `nbf` claim is in the future which isn't allowed",
)
)
}
ErrorKind::InvalidAlgorithm => {
println!(
&ErrorKind::InvalidAlgorithm => {
eprintln!(
"{}",
Red.bold().paint(
"The JWT provided has a different signing algorithm than the one you \
Expand All @@ -429,13 +431,13 @@ fn print_decoded_token(validated_token: JWTResult<TokenData<Payload>>, token_dat
)
}
_ => {
println!(
eprintln!(
"{} {:?}",
Red.bold().paint("The JWT provided is invalid because"),
err
)
}
}
};
}
_ => println!("{}", Cyan.bold().paint("Looks like a valid JWT!")),
}
Expand All @@ -444,6 +446,11 @@ fn print_decoded_token(validated_token: JWTResult<TokenData<Payload>>, token_dat
println!("{}\n", to_string_pretty(&token_data.header).unwrap());
println!("{}", Plain.bold().paint("Token claims\n------------"));
println!("{}", to_string_pretty(&token_data.claims).unwrap());

exit(match validated_token {
Err(_) => 1,
Ok(_) => 0,
})
}

fn main() {
Expand Down

0 comments on commit 71c1b3b

Please sign in to comment.