Skip to content

Commit f980245

Browse files
authored
Merge pull request #18 from akdombrowski/1.0.41
1.0.41 Now with base64url encode cli support!
2 parents b7b835f + 4cb9d91 commit f980245

File tree

9 files changed

+320
-108
lines changed

9 files changed

+320
-108
lines changed

README.md

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
![GitHub forks](https://img.shields.io/github/forks/akdombrowski/jwt-authn?style=for-the-badge&logo=github)
99
![GitHub watchers](https://img.shields.io/github/watchers/akdombrowski/jwt-authn?style=for-the-badge&logo=github)
1010
[![](https://data.jsdelivr.com/v1/package/npm/jwt-authn/badge)](https://www.jsdelivr.com/package/npm/jwt-authn)
11-
1211
[![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)
13-
12+
[link to npm](https://www.npmjs.com/package/jwt-authn)
1413
# jwt-authn
1514

1615
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!
@@ -30,28 +29,28 @@ Now with command line support!
3029
- [⬆Usage](#usage)
3130
- [⬆Installation:](#installation)
3231
- [⬆Accepted Form of JWTs](#accepted-form-of-jwts)
33-
- [ CLI support for JWT Decoding](#-cli-support-for-jwt-decoding)
34-
- [ Decoding a JWT](#-decoding-a-jwt)
35-
- [ **jwtDecode(jwt)**](#-jwtdecodejwt)
36-
- [ Encoding a JWT](#-encoding-a-jwt)
37-
- [**jwtEncode(header, payload, key[, options])**](#-jwtencodeheader-payload-key-options)
38-
- [ Signing](#-signing)
39-
- [ **rs256PEMSign(headerPayload, privateKey, passphrase)**](#-rs256pemsignheaderpayload-privatekey-passphrase)
40-
- [ **rs256JWKSign(headerPayload, privateKey)**](#-rs256jwksignheaderpayload-privatekey)
41-
- [ **hs256Sign(headerPayload, key)**](#-hs256signheaderpayload-key)
42-
- [ Verifying a signature](#-verifying-a-signature)
43-
- [ **rs256JWKVerify(jwt, publicKey)**](#-rs256jwkverifyjwt-publickey)
44-
- [ **rs256PEMVerify(jwt, publicKey)**](#-rs256pemverifyjwt-publickey)
45-
- [ **hs256Verify(jwt, passphrase, passphraseEncoding)**](#-hs256verifyjwt-passphrase-passphraseencoding)
46-
- [ Utility Methods](#-utility-methods)
47-
- [ **createHeaderPayload(header, payload)**](#-createheaderpayloadheader-payload)
48-
- [ **base64URLEncode(json)**](#-base64urlencodejson)
49-
- [ Appendix](#-appendix)
50-
- [ What is a JWT?](#-what-is-a-jwt)
51-
- [ Generating RSA256 private and public key pair](#-generating-rsa256-private-and-public-key-pair)
52-
- [ 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)
53-
- [ Contributing](#-contributing)
54-
- [ License](#-license)
32+
- [⬆CLI support for JWT Decoding](#cli-support-for-jwt-decoding)
33+
- [⬆Decoding a JWT](#decoding-a-jwt)
34+
- [**jwtDecode(jwt)**](#jwtdecodejwt)
35+
- [⬆Encoding a JWT](#encoding-a-jwt)
36+
- [[**jwtEncode(header, payload, key[, options])**](#index)](#jwtencodeheader-payload-key-options)
37+
- [⬆Signing](#signing)
38+
- [**rs256PEMSign(headerPayload, privateKey, passphrase)**](#rs256pemsignheaderpayload-privatekey-passphrase)
39+
- [**rs256JWKSign(headerPayload, privateKey)**](#rs256jwksignheaderpayload-privatekey)
40+
- [**hs256Sign(headerPayload, key)**](#hs256signheaderpayload-key)
41+
- [⬆Verifying a signature](#verifying-a-signature)
42+
- [**rs256JWKVerify(jwt, publicKey)**](#rs256jwkverifyjwt-publickey)
43+
- [**rs256PEMVerify(jwt, publicKey)**](#rs256pemverifyjwt-publickey)
44+
- [**hs256Verify(jwt, passphrase, passphraseEncoding)**](#hs256verifyjwt-passphrase-passphraseencoding)
45+
- [⬆Utility Methods](#utility-methods)
46+
- [**createHeaderPayload(header, payload)**](#createheaderpayloadheader-payload)
47+
- [**base64URLEncode(json)**](#base64urlencodejson)
48+
- [⬆Appendix](#appendix)
49+
- [⬆What is a JWT?](#what-is-a-jwt)
50+
- [⬆Generating RSA256 private and public key pair](#generating-rsa256-private-and-public-key-pair)
51+
- [⬆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)
52+
- [⬆Contributing](#contributing)
53+
- [⬆License](#license)
5554

5655
<br>
5756

@@ -115,7 +114,7 @@ eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTk
115114

116115
---
117116

118-
### [](#index) CLI support for JWT Decoding
117+
### [⬆CLI support for JWT Decoding](#index)
119118

120119
<br>
121120

@@ -143,11 +142,11 @@ jwt-authn eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiO
143142

144143
---
145144

146-
### [](#index) Decoding a JWT
145+
### [⬆Decoding a JWT](#index)
147146

148147
<br>
149148

150-
#### [](#index) **jwtDecode(jwt)**
149+
#### [**jwtDecode(jwt)**](#index)
151150

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

@@ -176,11 +175,11 @@ jwtAuthn.jwtDecode("eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0
176175

177176
---
178177

179-
### [](#index) Encoding a JWT
178+
### [⬆Encoding a JWT](#index)
180179

181180
<br>
182181

183-
#### [](#index) **jwtEncode(header, payload, key[, options])**
182+
#### [**jwtEncode(header, payload, key[, options])**](#index)
184183

185184

186185
where *options* contains:
@@ -209,11 +208,11 @@ jwt.jwtEncode(
209208

210209
---
211210

212-
### [](#index) Signing
211+
### [Signing](#index)
213212

214213
<br>
215214

216-
#### [](#index) **rs256PEMSign(headerPayload, privateKey, passphrase)**
215+
#### [**rs256PEMSign(headerPayload, privateKey, passphrase)**](#index)
217216

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

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

299298
<br>
300299

301-
#### [](#index) **rs256JWKSign(headerPayload, privateKey)**
300+
#### [**rs256JWKSign(headerPayload, privateKey)**](#index)
302301

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

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

344343
<br>
345344

346-
#### [](#index) **hs256Sign(headerPayload, key)**
345+
#### [**hs256Sign(headerPayload, key)**](#index)
347346

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

377376
---
378377

379-
### [](#index) Verifying a signature
378+
### [⬆Verifying a signature](#index)
380379

381380
<br>
382381

383-
#### [](#index) **rs256JWKVerify(jwt, publicKey)**
382+
#### [**rs256JWKVerify(jwt, publicKey)**](#index)
384383

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

402401
<br>
403402

404-
#### [](#index) **rs256PEMVerify(jwt, publicKey)**
403+
#### [**rs256PEMVerify(jwt, publicKey)**](#index)
405404

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

435434
<br>
436435

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

457456
---
458-
### [](#index) Utility Methods
457+
### [Utility Methods](#index)
459458

460-
#### [](#index) **createHeaderPayload(header, payload)**
459+
#### [**createHeaderPayload(header, payload)**](#index)
461460
* header: the decoded JWT header either in JSON object or a string literal in json format.
462461
* payload: the decoded JWT payload either in JSON object or a string literal in json format.
463462

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

482481
<br>
483482

484-
#### [](#index) **base64URLEncode(json)**
483+
#### [**base64URLEncode(json)**](#index)
485484
* json: the input in JSON object format.
486485

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

509508
---
510509

511-
## [](#index) Appendix
510+
## [Appendix](#index)
512511

513512
<br>
514513

515-
### [](#index) What is a JWT?
514+
### [⬆What is a JWT?](#index)
516515
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.
517516

518517
Resources:
@@ -526,7 +525,7 @@ Resources:
526525
<br>
527526
<br>
528527

529-
### [](#index) Generating RSA256 private and public key pair
528+
### [⬆Generating RSA256 private and public key pair](#index)
530529
```javascript
531530
import crypto from "crypto";
532531

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

560559

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

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

578577
---
579578

580-
## [](#index) Contributing
579+
## [Contributing](#index)
581580

582581

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

590589
---
591590

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

595594
[MIT-Modern-Variant](https://spdx.org/licenses/MIT-Modern-Variant.html)

cli/index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env node
22

3-
import { jwtDecode } from "../lib";
3+
import { jwtDecode, base64URLEncode } from "../lib";
44
import clipboardy from "clipboardy";
55

6+
const GENERIC_ERROR_CODE = 1;
7+
68
const decode = async (jwt) => {
79
try {
810
const decoded = await jwtDecode(jwt);
@@ -21,8 +23,10 @@ or call command with JWT as first argument\n\
2123
export const cli = async (clipboard, argv) => {
2224
// read passed in argument
2325
let arg2;
26+
let arg3;
2427
if (argv) {
2528
arg2 = argv[2];
29+
arg3 = argv[3];
2630
}
2731

2832
if (arg2 == "-h" || arg2 === "--help") {
@@ -52,10 +56,29 @@ export const cli = async (clipboard, argv) => {
5256
throw e;
5357
}
5458
} else {
55-
console.error("I found an error :(.");
59+
console.error("I found an error :(");
5660
console.error(
57-
"Nothing in clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard"
61+
"Nothing in your clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard"
5862
);
63+
return GENERIC_ERROR_CODE;
64+
}
65+
} else if (arg2 === "-b" || arg2 === "--base64url") {
66+
if (!arg3) {
67+
console.error("I found an error :(");
68+
console.error("No argument passed in to encode.");
69+
return GENERIC_ERROR_CODE;
70+
}
71+
72+
try {
73+
const bases64urlified = base64URLEncode(arg3);
74+
75+
console.log(bases64urlified);
76+
77+
return bases64urlified;
78+
} catch (e) {
79+
console.error("I found an error :(");
80+
console.error("base64url encoding failed:", e.message);
81+
throw e;
5982
}
6083
} else if (arg2) {
6184
try {
@@ -77,7 +100,6 @@ export const cli = async (clipboard, argv) => {
77100
"Nothing in clipboard and no arguments given. Pass in a JWT as the first argument or copy a JWT to your clipboard"
78101
);
79102
}
80-
return 1;
81103
};
82104

83105
// read from clipboard

cli/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)