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(jwt): support jwt cli #13

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.DS_Store
/.vscode
/.idea
96 changes: 93 additions & 3 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ anyhow = "^1.0"
axum = "^0.7.5"
base64 = "^0.22.1"
blake3 = "^1.5.1"
chrono = "0.4.38"
clap = { version = "^4.5.4", features = ["derive"] }
csv = "^1.3.0"
ed25519-dalek = { version = "^2.1.1", features = ["rand_core"] }
enum_dispatch = "0.3.13"
fancy-duration = "0.9.2"
futures = "^0.3.30"
hex = "^0.4.3"
jsonwebtoken = "9.3.0"
rand = "^0.8.5"
reqwest = "0.12.5"
serde = { version = "^1.0", features = ["derive"] }
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ run_with_log:

# ******** http ********
# make run ARGS="http serve"

# ******** jwt ********
# make run ARGS="jwt sign"
# make run_with_log ARGS="jwt sign --sub noah-future --aud noah --exp 7d"
# make run_with_log ARGS="jwt verify --token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJub2FoIiwiZXhwIjoxNzE5ODQyMjg3LCJzdWIiOiJub2FoLWZ1dHVyZSIsImlhdCI6MTcxOTIzNzQ4N30.V0IEykmRp58PT6fQk4_KbnoytltQKdBU4jmlNMyCh8U"
8 changes: 5 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod base64;
mod csv;
mod genpass;
mod http;
mod jwt;
mod text;

// pub use csv_opts::{CsvOpts, OutputFormat};
Expand All @@ -18,7 +19,7 @@ mod text;
// pub use self::genpass::GenPassOpts;
// pub use self::http::{HttpServeOpts, HttpSubCommand};
// pub use self::text::{TextSignFormat, TextSignOpts, TextSubcommand, TextVerifyOpts};
pub use self::{base64::*, csv::*, genpass::*, http::*, text::*};
pub use self::{base64::*, csv::*, genpass::*, http::*, jwt::*, text::*};

#[derive(Debug, Parser)]
#[command(name = "rcli", version, author, about, long_about = None)]
Expand All @@ -28,7 +29,7 @@ pub struct Opts {
}

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExecuter)]
#[enum_dispatch(CmdExecutor)]
pub enum SubCommand {
#[command(name = "csv", about = "Show CSV, or convert CSV to other formats")]
Csv(CsvOpts),
Expand All @@ -40,13 +41,14 @@ pub enum SubCommand {
Text(TextSubcommand),
#[command(subcommand, about = "HTTP server")]
Http(HttpSubCommand),
#[command(subcommand, about = "JWT sign/verify")]
Jwt(JwtSubCommand),
}

// 会传入文件名
// csv --input filename(xxx.csv)
// pub fn verify_input_file(filename: &str) -> Result<String, &'static str> {
// if Path::new(filename).exists() {
// // Ok(filename.to_string())
// Ok(filename.into())
// } else {
// Err("File not found")
Expand Down
8 changes: 4 additions & 4 deletions src/cli/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::{fmt, str::FromStr};
use clap::Parser;
use enum_dispatch::enum_dispatch;

use crate::CmdExecuter;
use crate::CmdExecutor;

use super::verify_file;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExecuter)]
#[enum_dispatch(CmdExecutor)]
pub enum Base64Subcommand {
#[command(name = "encode", about = "Encode a string to base64")]
Encode(Base64EncodeOpts),
Expand Down Expand Up @@ -67,7 +67,7 @@ impl fmt::Display for Base64Format {
}
}

impl CmdExecuter for Base64EncodeOpts {
impl CmdExecutor for Base64EncodeOpts {
async fn execute(self) -> anyhow::Result<()> {
let mut reader = crate::get_reader(&self.input)?;
let ret = crate::process_encode(&mut reader, self.format)?;
Expand All @@ -76,7 +76,7 @@ impl CmdExecuter for Base64EncodeOpts {
}
}

impl CmdExecuter for Base64DecodeOpts {
impl CmdExecutor for Base64DecodeOpts {
async fn execute(self) -> anyhow::Result<()> {
let mut reader = crate::get_reader(&self.input)?;
let ret = crate::process_decode(&mut reader, self.format)?;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/csv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use std::{fmt, str::FromStr};

use crate::{process_csv, CmdExecuter};
use crate::{process_csv, CmdExecutor};

use super::verify_file;

Expand Down Expand Up @@ -41,7 +41,7 @@ pub enum OutputFormat {
}

// region: --- impls
impl CmdExecuter for CsvOpts {
impl CmdExecutor for CsvOpts {
async fn execute(self) -> anyhow::Result<()> {
let output = if let Some(output) = self.output {
output
Expand Down
4 changes: 2 additions & 2 deletions src/cli/genpass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;

use crate::CmdExecuter;
use crate::CmdExecutor;
use zxcvbn::zxcvbn;

#[derive(Debug, Parser)]
Expand All @@ -26,7 +26,7 @@ pub struct GenPassOpts {
pub symbol: bool,
}

impl CmdExecuter for GenPassOpts {
impl CmdExecutor for GenPassOpts {
async fn execute(self) -> anyhow::Result<()> {
let ret = crate::process_genpass(
self.length,
Expand Down
6 changes: 3 additions & 3 deletions src/cli/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::path::PathBuf;
use clap::Parser;
use enum_dispatch::enum_dispatch;

use crate::{process_http_serve, CmdExecuter};
use crate::{process_http_serve, CmdExecutor};

use super::verify_path;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExecuter)]
#[enum_dispatch(CmdExecutor)]
pub enum HttpSubCommand {
#[command(about = "Serve a directory over HTTP")]
Serve(HttpServeOpts),
Expand All @@ -23,7 +23,7 @@ pub struct HttpServeOpts {
}

// region: --- impls
impl CmdExecuter for HttpServeOpts {
impl CmdExecutor for HttpServeOpts {
async fn execute(self) -> anyhow::Result<()> {
process_http_serve(self.dir, self.port).await
}
Expand Down
Loading