npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cva-jwt

v1.0.0

Published

Simple JWT (JSON Web Token) encode and decode module using node crypto

Downloads

14

Readme

# CVA JWT

CVA JWT is a simple JSON Web Token (JWT) encode and decode module for Node.js. It allows you to create and verify JWTs using various algorithms.

Installation

You can install the cva-jwt package using npm:

npm install cva-jwt

Usage

Encoding a JWT

To encode a JWT, use the encode method. You'll need to provide a payload, a secret key, and optionally, an algorithm.

const jwt = require('cva-jwt');

// Define your payload
const payload = {
  userId: 123,
  role: 'admin'
};

// Define your secret key
const secretKey = 'your-256-bit-secret';


// Encode the token
const token = jwt.encode(payload, secretKey, 'HS256');

console.log('Encoded JWT:', token);

HS256 secrets are typically 128-bit random strings, for example hex-encoded:

let secret = Buffer.from('fe1a1915a379f3be5394b64d14794932', 'hex')

Decoding a JWT

To decode and verify a JWT, use the decode method. You will need to provide the token and the secret key.

const token = 'your.jwt.token.here';

try {
  const decodedPayload = jwt.decode(token, secretKey);
  console.log('Decoded Payload:', decodedPayload);
} catch (error) {
  console.error('Error decoding token:', error.message);
}

Options

The encode function accepts an optional options parameter, which allows you to customize the JWT header.

const options = {
  header: {
    kid: 'your-key-id' // Optional key ID
  }
};

const token = jwt.encode(payload, secretKey, 'HS256', options);

Supported Algorithms

  • HS256 (HMAC using SHA-256)
  • HS384 (HMAC using SHA-384)
  • HS512 (HMAC using SHA-512)
  • RS256 (RSA Signature with SHA-256)

Error Handling

When decoding a JWT, several errors may be thrown:

  • No token supplied: If no token is provided.
  • Invalid token: Not enough or too many segments: If the token does not contain exactly three segments.
  • Signature verification failed: If the signature does not match.
  • Token not yet active: If the token's nbf (not before) claim is not met.
  • Token expired: If the token's exp (expiration) claim has passed.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any enhancements or fixes.


### Notes:
- Feel free to customize the content to better match your project's specifics.
- Make sure to include any additional details relevant to your implementation or usage.