Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/vkondratiuk482/jwt
- Owner: vkondratiuk482
- License: mit
- Created: 2022-12-25T15:31:59.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-18T20:11:31.000Z (almost 2 years ago)
- Last Synced: 2024-06-30T13:14:19.592Z (6 months ago)
- Topics: asymmetric-cryptography, dependency-free, hs256, jsonwebtoken, jwt, node, rs256, symmetric-cryptography
- Language: JavaScript
- Homepage:
- Size: 83 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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)