From 085ecf570073f9f90e91fb26b340cfd73e7151a0 Mon Sep 17 00:00:00 2001 From: Adminy <22717869+adminy@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:15:29 +0100 Subject: [PATCH] make it a nix module (#39) * Create flake.nix * chore: Updates to code and to README --------- Co-authored-by: Luciano Mammino --- README.md | 120 +++++++++++++++++++++++++++++++++++++++------------- default.nix | 9 ++++ flake.lock | 27 ++++++++++++ flake.nix | 27 ++++++++++++ shell.nix | 11 +++++ 5 files changed, 164 insertions(+), 30 deletions(-) create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 shell.nix diff --git a/README.md b/README.md index 3eaf524..0d21f2c 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,16 @@ [![rustc badge](https://img.shields.io/badge/rustc-1.40+-lightgray.svg)](https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html) [![Clippy Linting Result](https://img.shields.io/badge/clippy-<3-yellowgreen)](https://github.com/rust-lang/rust-clippy) [![License: MIT OR Apache-2.0](https://img.shields.io/crates/l/jwtinfo.svg)](#license) -[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/lmammino/jwtinfo) +[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/lmammino/jwtinfo) - -A command line tool to get information about [JWTs](https://tools.ietf.org/html/rfc7519) (Json Web Tokens). +A command line tool to get information about +[JWTs](https://tools.ietf.org/html/rfc7519) (Json Web Tokens). ## Usage -`jwtinfo` is a command line interface that allows you to inspect a given JWT. The tool currently allows to see the body of the token in JSON format. It accepts a single command line argument which should be a valid JWT. +`jwtinfo` is a command line interface that allows you to inspect a given JWT. +The tool currently allows to see the body of the token in JSON format. It +accepts a single command line argument which should be a valid JWT. Here's an example: @@ -25,10 +27,11 @@ jwtinfo eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI Which will print: ```json -{"sub":"1234567890","name":"John Doe","iat":1516239022} +{ "sub": "1234567890", "name": "John Doe", "iat": 1516239022 } ``` -If you want to visualize the token header (rather than the body), you can do that by passing the `--header` flag: +If you want to visualize the token header (rather than the body), you can do +that by passing the `--header` flag: ```bash jwtinfo --header eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c @@ -37,10 +40,11 @@ jwtinfo --header eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIi Which will print: ```json -{"alg":"HS256","typ":"JWT"} +{ "alg": "HS256", "typ": "JWT" } ``` -You can combine the tool with other command line utilities, for instance [`jq`](https://stedolan.github.io/jq/): +You can combine the tool with other command line utilities, for instance +[`jq`](https://stedolan.github.io/jq/): ```bash jwtinfo eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c | jq . @@ -52,7 +56,8 @@ You can install the binary in several ways: ### Cargo -You can install the binary in your system with [`cargo`](https://doc.rust-lang.org/cargo/getting-started/installation.html): +You can install the binary in your system with +[`cargo`](https://doc.rust-lang.org/cargo/getting-started/installation.html): ```bash cargo install jwtinfo @@ -60,29 +65,71 @@ cargo install jwtinfo At this point `jwtinfo` will be available as a binary in your system. - ### Install script -The following script will download and install precompiled binaries from the latest GitHub release +The following script will download and install precompiled binaries from the +latest GitHub release ```bash curl https://raw.githubusercontent.com/lmammino/jwtinfo/main/install.sh | sh ``` -By default it will install the binary in `/usr/local/bin`. You can customize this by setting the `INSTALL_DIRECTORY` environment variable before running the script (e.g. `INSTALL_DIRECTORY=$HOME` will install the binary in `$HOME/bin`). - -If you want to install a specific release you can set the `RELEASE_TAG` environment variable to point to your target versiong before running the script (e.g. `RELESE_TAG=v0.1.7`). +By default, it will install the binary in `/usr/local/bin`. You can customize +this by setting the `INSTALL_DIRECTORY` environment variable before running the +script (e.g. `INSTALL_DIRECTORY=$HOME` will install the binary in `$HOME/bin`). +If you want to install a specific release you can set the `RELEASE_TAG` +environment variable to point to your target version before running the script +(e.g. `RELESE_TAG=v0.1.7`). ### Precompiled binaries -Pre-compiled binaries for x64 (Windows, MacOs and Unix) and ARMv7 are available in the [Releases](https://github.com/lmammino/jwtinfo/releases) page. +Pre-compiled binaries for x64 (Windows, macOs and Unix) and ARMv7 are available +in the [Releases](https://github.com/lmammino/jwtinfo/releases) page. +### Using Nix -#### Alternatives +If you are using [Nix](https://nixos.org/), you can install the `jwtinfo` binary +with the following command: + +```bash +nix profile install github:lmammino/jwtinfo +``` -If you don't want to install a binary for debugging JWT, a super simple `bash` alternative called [`jwtinfo.sh`](https://gist.github.com/lmammino/920ee0699af627a3492f86c607c859f6) is available. +Or, if you prefer to use a configuration file, you can add the following to your +flake: +```nix +{ + inputs.jwtinfo.url = "github:lmammino/jwtinfo"; +} +# ... with home.nix +home.packages = [ inputs.jwtinfo.packages."x86_64-linux".default ]; + +# ... with configuration.nix +environment.systemPackages = [ inputs.jwtinfo.packages."x86_64-linux".default ]; +``` + +Make sure to replace `"x86_64-linux"` with your target platform. + +You can also just try it out in a Nix shell with: + +```bash +nix shell github:lmammino/jwtinfo -c jwtinfo +``` + +Finally, for development purposes, you can clone this repo and then run: + +```bash +nix develop +``` + +#### Alternatives + +If you don't want to install a binary for debugging JWT, a super simple `bash` +alternative called +[`jwtinfo.sh`](https://gist.github.com/lmammino/920ee0699af627a3492f86c607c859f6) +is available. ## Programmatic usage @@ -112,7 +159,6 @@ assert_eq!(token.header.to_string(), "{\"alg\":\"HS256\",\"typ\":\"JWT\"}"); assert_eq!(token.body.to_string(), "{\"iat\":1516239022,\"name\":\"John Doe\",\"sub\":\"1234567890\"}"); ``` - ## Coverage reports If you want to run coverage reports locally you can follow this recipe. @@ -145,34 +191,48 @@ Now we can run `grcov`: grcov ./target/debug/ -s . -t html --llvm --branch --ignore-not-existing -o ./target/debug/coverage/ ``` -Finally you will have your browsable coverage report at `./target/debug/coverage/index.html`. - +Finally you will have your browsable coverage report at +`./target/debug/coverage/index.html`. ### Tarpaulin coverage -Since `grcov` tends to be somewhat inaccurate at times, you can also get a coverage report by running [tarpaulin](https://github.com/xd009642/tarpaulin) using docker: +Since `grcov` tends to be somewhat inaccurate at times, you can also get a +coverage report by running [tarpaulin](https://github.com/xd009642/tarpaulin) +using docker: ```bash docker run --security-opt seccomp=unconfined -v "${PWD}:/volume" xd009642/tarpaulin:develop-nightly bash -c 'cargo build && cargo tarpaulin -o Html' ``` -Your coverage report will be available as `tarpaulin-report.html` in the root of the project. - +Your coverage report will be available as `tarpaulin-report.html` in the root of +the project. ## Credits -A special thank you goes to the [Rust Reddit community](https://www.reddit.com/r/rust/) for providing a lot of useful suggestions on how to improve this project. A special thanks goes to: [mardiros](https://www.reddit.com/user/mardiros/), [matthieum](https://www.reddit.com/user/matthieum/), [steveklabnik1](https://www.reddit.com/user/steveklabnik1/), [ESBDB](https://www.reddit.com/user/ESBDB/), [Dushistov](https://www.reddit.com/user/Dushistov/), [Doddzilla7](https://www.reddit.com/user/Doddzilla7/). Another huge thank you goes to the [Rust stackoverflow community](https://chat.stackoverflow.com/rooms/62927/rust), especially to [Denys Séguret](https://chat.stackoverflow.com/users/263525). - -Big thanks also go to [Tim McNamara](https://twitter.com/timClicks) for conducting a [live code review](https://loige.co/learning-rust-through-open-source-and-live-code-reviews) of this codebase. - +A special thank you goes to the +[Rust Reddit community](https://www.reddit.com/r/rust/) for providing a lot of +useful suggestions on how to improve this project. A special thanks goes to: +[mardiros](https://www.reddit.com/user/mardiros/), +[matthieum](https://www.reddit.com/user/matthieum/), +[steveklabnik1](https://www.reddit.com/user/steveklabnik1/), +[ESBDB](https://www.reddit.com/user/ESBDB/), +[Dushistov](https://www.reddit.com/user/Dushistov/), +[Doddzilla7](https://www.reddit.com/user/Doddzilla7/). Another huge thank you +goes to the +[Rust stackoverflow community](https://chat.stackoverflow.com/rooms/62927/rust), +especially to [Denys Séguret](https://chat.stackoverflow.com/users/263525). + +Big thanks also go to [Tim McNamara](https://twitter.com/timClicks) for +conducting a +[live code review](https://loige.co/learning-rust-through-open-source-and-live-code-reviews) +of this codebase. ## Contributing -Everyone is very welcome to contribute to this project. -You can contribute just by submitting bugs or suggesting improvements by +Everyone is very welcome to contribute to this project. You can contribute just +by submitting bugs or suggesting improvements by [opening an issue on GitHub](https://github.com/lmammino/jwtinfo/issues). - ## License Licensed under [MIT License](LICENSE). © Luciano Mammino & Stefano Abalsamo. diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..72bd7f6 --- /dev/null +++ b/default.nix @@ -0,0 +1,9 @@ +{ pkgs ? import { } }: +let manifest = (pkgs.lib.importTOML ./Cargo.toml).package; +in +pkgs.rustPlatform.buildRustPackage rec { + pname = manifest.name; + version = manifest.version; + cargoLock.lockFile = ./Cargo.lock; + src = pkgs.lib.cleanSource ./.; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..48828c7 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1724479785, + "narHash": "sha256-pP3Azj5d6M5nmG68Fu4JqZmdGt4S4vqI5f8te+E/FTw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d0e1602ddde669d5beb01aec49d71a51937ed7be", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..8654779 --- /dev/null +++ b/flake.nix @@ -0,0 +1,27 @@ +{ + description = "jwtinfo"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + outputs = { self, nixpkgs }: + let + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + "x86_64-windows" + "aarch64-windows" + ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + pkgsFor = nixpkgs.legacyPackages; + in + { + packages = forAllSystems (system: { + default = pkgsFor.${system}.callPackage ./default.nix { }; + }); + devShells = forAllSystems (system: { + default = pkgsFor.${system}.callPackage ./shell.nix { }; + }); + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..99c93b6 --- /dev/null +++ b/shell.nix @@ -0,0 +1,11 @@ +{ pkgs ? import { } }: +pkgs.mkShell { + # Get dependencies from the main package + inputsFrom = [ (pkgs.callPackage ./default.nix { }) ]; + # Additional tooling + buildInputs = with pkgs; [ + rust-analyzer # LSP Server + rustfmt # Formatter + clippy # Linter + ]; +}