Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vkondratiuk482/jwt

Library created for education purposes to sign and decode JWT tokens using symmetric and asymmetric algorithms (HS256, RS256). Dependency free
https://github.com/vkondratiuk482/jwt

asymmetric-cryptography dependency-free hs256 jsonwebtoken jwt node rs256 symmetric-cryptography

Last synced: about 1 month ago
JSON representation

Library created for education purposes to sign and decode JWT tokens using symmetric and asymmetric algorithms (HS256, RS256). Dependency free

Awesome Lists containing this project

README

        

# @mokuteki/jwt
Zero dependency typescript friendly Node.js library designed to hash/decode JSON Web Tokens using symmetric/asymmetric algorithms

## Installation
```bash
npm i @mokuteki/jwt
```

## Quick start

* Pick one hashing strategy out of existing ones
* Pass the strategy to JSONWebToken constructor
* Use created instance to generate/verify JWTs

## HS256 Strategy

```javascript
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload);
const decoded = jwt.verify(token);

console.log(decoded); // { id: 1 }
```

## RS256 Strategy

```javascript
const { JSONWebToken, RS256Strategy } = require('@mokuteki/jwt');

const strategy = new RS256Strategy({
ttl: 10000,
publicKey: 'YOUR_PUBLIC_KEY',
privateKey: 'YOUR_PRIVATE_KEY',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload);
const decoded = jwt.verify(token);

console.log(decoded); // { id: 1 }
```

## Override predefined options

```javascript
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

/**
* Override JWT ttl option
*/
const token = jwt.generate(payload, { ttl: 1000 });

setTimeout(() => {
try {
const decoded = jwt.verify(token);
} catch (err) {
// err instanceof JwtExpiredError
}
}, 2000);
```

```javascript
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload, { ttl: 20000, secret: 'NEW_SECRET' });

try {
const decoded = jwt.verify(token);
} catch (err) {
// err instanceof InvalidSignatureError
}
```

## License
Licensed under [MIT](https://github.com/mokuteki225/jwt/blob/master/LICENSE.md)