Skip to content

Commit

Permalink
feat: add jwtToken fn
Browse files Browse the repository at this point in the history
  • Loading branch information
fixcik committed May 13, 2024
1 parent fefea3f commit 313e65c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
42 changes: 41 additions & 1 deletion Cargo.lock

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

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "texp"
version = "0.2.3"
version = "0.2.4"
edition = "2021"

license = "GPL-3.0"
Expand All @@ -17,6 +17,13 @@ repository = "https://github.com/fixcik/texp"
anyhow = "1.0.82"
cla = "0.0.1"
clap = { version = "4.5.4", features = ["derive"] }
hmac = { version = "0.12.1", optional = true }
jwt = { version = "0.16.0", optional = true }
rand = "0.8.5"
serde_yaml = "0.9.34"
sha2 = { version = "0.10.8", optional = true }
tera = { version = "1.19.1", features = ["preserve_order"] }

[features]
default = ["jwt"]
jwt = ["dep:jwt", "dep:hmac", "dep:sha2"]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Table of Contents:
- [Via homebrew](#via-homebrew)
- [Custom functions](#custom-functions)
- [randomString](#randomstring)
- [jwtToken](#jwttoken)

## Examples

Expand Down Expand Up @@ -88,3 +89,11 @@ Generates random string, with given length:
```
{{ randomString(length=20) }}
```

### jwtToken

Generate jwt token

```
{{ jwtToken(claims=claims, secret=secret) }}
```
41 changes: 41 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ use std::{collections::HashMap, fs::File, path::PathBuf};

use anyhow::{anyhow, Context};
use clap::Parser;
use hmac::Mac;
use rand::{
distributions::{Alphanumeric, Slice},
thread_rng, Rng,
};
use serde_yaml::{to_value, Mapping, Value};

#[cfg(feature = "jwt")]
use hmac::Hmac;
#[cfg(feature = "jwt")]
use jwt::SignWithKey;
#[cfg(feature = "jwt")]
use sha2::Sha256;

#[derive(Parser, Debug)]
#[command(name = "texp")]
#[command(bin_name = "texp")]
Expand Down Expand Up @@ -61,6 +69,39 @@ fn main() -> anyhow::Result<()> {
}),
);

if cfg!(feature = "jwt") {
tera.register_function(
"jwtToken",
Box::new(|args: &HashMap<String, tera::Value>| {
let claims = {
let claims = args
.get("claims")
.ok_or(tera::Error::msg("No claims provided"))?;

let claims = claims
.as_object()
.ok_or(tera::Error::msg("Claims should be an object"))?;

claims
};

let secret = args
.get("secret")
.ok_or(tera::Error::msg("No secret provided"))?
.as_str()
.ok_or(tera::Error::msg("Secret should be a string"))?;

let key: Hmac<Sha256> = Hmac::new_from_slice(secret.as_bytes()).unwrap();

let token = claims
.sign_with_key(&key)
.map_err(|e| tera::Error::msg(e))?;

tera::Result::Ok(tera::to_value(token)?)
}),
);
}

let mut values: Mapping = if let Some(path) = cli.values {
let reader = File::open(path).context("Failed to read file with values")?;
serde_yaml::from_reader(reader)?
Expand Down

0 comments on commit 313e65c

Please sign in to comment.