Skip to content

Commit

Permalink
Merge pull request #18 from akdombrowski/1.0.41
Browse files Browse the repository at this point in the history
1.0.41
Now with base64url encode cli support!
  • Loading branch information
akdombrowski committed May 9, 2021
2 parents b7b835f + 4cb9d91 commit f980245
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 108 deletions.
91 changes: 45 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
![GitHub forks](https://img.shields.io/github/forks/akdombrowski/jwt-authn?style=for-the-badge&logo=github)
![GitHub watchers](https://img.shields.io/github/watchers/akdombrowski/jwt-authn?style=for-the-badge&logo=github)
[![](https://data.jsdelivr.com/v1/package/npm/jwt-authn/badge)](https://www.jsdelivr.com/package/npm/jwt-authn)

[![Rate on Openbase](https://badges.openbase.com/js/rating/jwt-authn.svg)](https://openbase.com/js/jwt-authn?utm_source=embedded&utm_medium=badge&utm_campaign=rate-badge)

[link to npm](https://www.npmjs.com/package/jwt-authn)
# jwt-authn

jwt-authn is an npm package for dealing with JSON Web Tokens (JWT). Encoding, decoding, verifying, signing, and more coming. It includes support for the RS256 and HS256 algorithms and JWK and PEM format keys (even encrypted keys). Only Node >=15.x as a requirement!
Expand All @@ -30,28 +29,28 @@ Now with command line support!
- [⬆Usage](#usage)
- [⬆Installation:](#installation)
- [⬆Accepted Form of JWTs](#accepted-form-of-jwts)
- [ CLI support for JWT Decoding](#-cli-support-for-jwt-decoding)
- [ Decoding a JWT](#-decoding-a-jwt)
- [ **jwtDecode(jwt)**](#-jwtdecodejwt)
- [ Encoding a JWT](#-encoding-a-jwt)
- [**jwtEncode(header, payload, key[, options])**](#-jwtencodeheader-payload-key-options)
- [ Signing](#-signing)
- [ **rs256PEMSign(headerPayload, privateKey, passphrase)**](#-rs256pemsignheaderpayload-privatekey-passphrase)
- [ **rs256JWKSign(headerPayload, privateKey)**](#-rs256jwksignheaderpayload-privatekey)
- [ **hs256Sign(headerPayload, key)**](#-hs256signheaderpayload-key)
- [ Verifying a signature](#-verifying-a-signature)
- [ **rs256JWKVerify(jwt, publicKey)**](#-rs256jwkverifyjwt-publickey)
- [ **rs256PEMVerify(jwt, publicKey)**](#-rs256pemverifyjwt-publickey)
- [ **hs256Verify(jwt, passphrase, passphraseEncoding)**](#-hs256verifyjwt-passphrase-passphraseencoding)
- [ Utility Methods](#-utility-methods)
- [ **createHeaderPayload(header, payload)**](#-createheaderpayloadheader-payload)
- [ **base64URLEncode(json)**](#-base64urlencodejson)
- [ Appendix](#-appendix)
- [ What is a JWT?](#-what-is-a-jwt)
- [ Generating RSA256 private and public key pair](#-generating-rsa256-private-and-public-key-pair)
- [ Changing public key generated with ssh-keygen (the above command) into PEM format](#-changing-public-key-generated-with-ssh-keygen-the-above-command-into-pem-format)
- [ Contributing](#-contributing)
- [ License](#-license)
- [⬆CLI support for JWT Decoding](#cli-support-for-jwt-decoding)
- [⬆Decoding a JWT](#decoding-a-jwt)
- [**jwtDecode(jwt)**](#jwtdecodejwt)
- [⬆Encoding a JWT](#encoding-a-jwt)
- [[**jwtEncode(header, payload, key[, options])**](#index)](#jwtencodeheader-payload-key-options)
- [⬆Signing](#signing)
- [**rs256PEMSign(headerPayload, privateKey, passphrase)**](#rs256pemsignheaderpayload-privatekey-passphrase)
- [**rs256JWKSign(headerPayload, privateKey)**](#rs256jwksignheaderpayload-privatekey)
- [**hs256Sign(headerPayload, key)**](#hs256signheaderpayload-key)
- [⬆Verifying a signature](#verifying-a-signature)
- [**rs256JWKVerify(jwt, publicKey)**](#rs256jwkverifyjwt-publickey)
- [**rs256PEMVerify(jwt, publicKey)**](#rs256pemverifyjwt-publickey)
- [**hs256Verify(jwt, passphrase, passphraseEncoding)**](#hs256verifyjwt-passphrase-passphraseencoding)
- [⬆Utility Methods](#utility-methods)
- [**createHeaderPayload(header, payload)**](#createheaderpayloadheader-payload)
- [**base64URLEncode(json)**](#base64urlencodejson)
- [⬆Appendix](#appendix)
- [⬆What is a JWT?](#what-is-a-jwt)
- [⬆Generating RSA256 private and public key pair](#generating-rsa256-private-and-public-key-pair)
- [⬆Changing public key generated with ssh-keygen (the above command) into PEM format](#changing-public-key-generated-with-ssh-keygen-the-above-command-into-pem-format)
- [⬆Contributing](#contributing)
- [⬆License](#license)

<br>

Expand Down Expand Up @@ -115,7 +114,7 @@ eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTk

---

### [](#index) CLI support for JWT Decoding
### [⬆CLI support for JWT Decoding](#index)

<br>

Expand Down Expand Up @@ -143,11 +142,11 @@ jwt-authn eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiO

---

### [](#index) Decoding a JWT
### [⬆Decoding a JWT](#index)

<br>

#### [](#index) **jwtDecode(jwt)**
#### [**jwtDecode(jwt)**](#index)

*Decoding example taken from [RFC 7515 JSON Web Signature (JWS)](https://tools.ietf.org/html/rfc7515#appendix-A.1.2).

Expand Down Expand Up @@ -176,11 +175,11 @@ jwtAuthn.jwtDecode("eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0

---

### [](#index) Encoding a JWT
### [⬆Encoding a JWT](#index)

<br>

#### [](#index) **jwtEncode(header, payload, key[, options])**
#### [**jwtEncode(header, payload, key[, options])**](#index)


where *options* contains:
Expand Down Expand Up @@ -209,11 +208,11 @@ jwt.jwtEncode(

---

### [](#index) Signing
### [Signing](#index)

<br>

#### [](#index) **rs256PEMSign(headerPayload, privateKey, passphrase)**
#### [**rs256PEMSign(headerPayload, privateKey, passphrase)**](#index)

* headerPayload: The combined base64url(header) and base64url(payload) separated by a ".".

Expand Down Expand Up @@ -298,7 +297,7 @@ const sig = rs256PEMSign(headerPayload, privateKey);

<br>

#### [](#index) **rs256JWKSign(headerPayload, privateKey)**
#### [**rs256JWKSign(headerPayload, privateKey)**](#index)

* headerPayload: The combined base64url(header) and base64url(payload) separated by a ".".

Expand Down Expand Up @@ -343,7 +342,7 @@ const sig = rs256JWKSign(headerPayload, privateKey);

<br>

#### [](#index) **hs256Sign(headerPayload, key)**
#### [**hs256Sign(headerPayload, key)**](#index)

* headerPayload: The combined base64url(header) and base64url(payload) separated by a ".".
* key: The signing key or passphrase.
Expand Down Expand Up @@ -376,11 +375,11 @@ const sig = hs256Sign(headerPayload, passphrase);

---

### [](#index) Verifying a signature
### [⬆Verifying a signature](#index)

<br>

#### [](#index) **rs256JWKVerify(jwt, publicKey)**
#### [**rs256JWKVerify(jwt, publicKey)**](#index)

* jwt: the signed JWT (JSON Web Token) that you're trying to verify.
* publicKey: the public key used to verify the signature of the JWT.
Expand All @@ -401,7 +400,7 @@ rs256JWKVerify(encoded, {

<br>

#### [](#index) **rs256PEMVerify(jwt, publicKey)**
#### [**rs256PEMVerify(jwt, publicKey)**](#index)

* jwt: the signed JWT (JSON Web Token) that you're trying to verify.
* publicKey: the public key used to verify the signature of the JWT.
Expand Down Expand Up @@ -434,7 +433,7 @@ rs256PEMVerify(

<br>

#### [](#index) **hs256Verify(jwt, passphrase, passphraseEncoding)**
#### [**hs256Verify(jwt, passphrase, passphraseEncoding)**](#index)
* jwt: the signed JWT (JSON Web Token) that you're trying to verify.
* passphrase: the passphrase used when signing with HMAC (HS256).
* passphraseEncoding: (optional) if specified and available, the encoding is used to read the passphrase. Otherwise, base64url encoding is assumed.
Expand All @@ -455,9 +454,9 @@ hs256Verify(encoded, passphrase)
<br>

---
### [](#index) Utility Methods
### [Utility Methods](#index)

#### [](#index) **createHeaderPayload(header, payload)**
#### [**createHeaderPayload(header, payload)**](#index)
* header: the decoded JWT header either in JSON object or a string literal in json format.
* payload: the decoded JWT payload either in JSON object or a string literal in json format.

Expand All @@ -481,7 +480,7 @@ createHeaderPayload(header, payload);

<br>

#### [](#index) **base64URLEncode(json)**
#### [**base64URLEncode(json)**](#index)
* json: the input in JSON object format.

Use to create the base64url encoding of the the input.
Expand All @@ -508,11 +507,11 @@ base64URLEncode(payload);

---

## [](#index) Appendix
## [Appendix](#index)

<br>

### [](#index) What is a JWT?
### [⬆What is a JWT?](#index)
A JWT (JSON Web Token), pronounced like "jot", passes along information in the form of claims. It's often used because it's url safe and compact. Its components are also in the form of JSON objects, a popular way to read information across the internet. The cryptographic mechanisms of a JWS provide integrity protection as well.

Resources:
Expand All @@ -526,7 +525,7 @@ Resources:
<br>
<br>

### [](#index) Generating RSA256 private and public key pair
### [⬆Generating RSA256 private and public key pair](#index)
```javascript
import crypto from "crypto";

Expand Down Expand Up @@ -558,7 +557,7 @@ ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
<br>


### [](#index) Changing public key generated with ssh-keygen (the above command) into PEM format
### [⬆Changing public key generated with ssh-keygen (the above command) into PEM format](#index)

*You need to do this to use it as the public key to verify a signed JWT.
```bash
Expand All @@ -577,7 +576,7 @@ openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

---

## [](#index) Contributing
## [Contributing](#index)


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand All @@ -589,7 +588,7 @@ Please make sure to update tests as appropriate.

---

## [](#index) License
## [License](#index)
![NPM](https://img.shields.io/npm/l/jwt-authn?&style=for-the-badge&logo=npm)

[MIT-Modern-Variant](https://spdx.org/licenses/MIT-Modern-Variant.html)
Expand Down
30 changes: 26 additions & 4 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node

import { jwtDecode } from "../lib";
import { jwtDecode, base64URLEncode } from "../lib";
import clipboardy from "clipboardy";

const GENERIC_ERROR_CODE = 1;

const decode = async (jwt) => {
try {
const decoded = await jwtDecode(jwt);
Expand All @@ -21,8 +23,10 @@ or call command with JWT as first argument\n\
export const cli = async (clipboard, argv) => {
// read passed in argument
let arg2;
let arg3;
if (argv) {
arg2 = argv[2];
arg3 = argv[3];
}

if (arg2 == "-h" || arg2 === "--help") {
Expand Down Expand Up @@ -52,10 +56,29 @@ export const cli = async (clipboard, argv) => {
throw e;
}
} else {
console.error("I found an error :(.");
console.error("I found an error :(");
console.error(
"Nothing in clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard"
"Nothing in your clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard"
);
return GENERIC_ERROR_CODE;
}
} else if (arg2 === "-b" || arg2 === "--base64url") {
if (!arg3) {
console.error("I found an error :(");
console.error("No argument passed in to encode.");
return GENERIC_ERROR_CODE;
}

try {
const bases64urlified = base64URLEncode(arg3);

console.log(bases64urlified);

return bases64urlified;
} catch (e) {
console.error("I found an error :(");
console.error("base64url encoding failed:", e.message);
throw e;
}
} else if (arg2) {
try {
Expand All @@ -77,7 +100,6 @@ export const cli = async (clipboard, argv) => {
"Nothing in clipboard and no arguments given. Pass in a JWT as the first argument or copy a JWT to your clipboard"
);
}
return 1;
};

// read from clipboard
Expand Down
2 changes: 1 addition & 1 deletion cli/index.min.js

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

Loading

0 comments on commit f980245

Please sign in to comment.