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

tokenease

v1.0.2

Published

A library for handling JWT authentication in Express.js applications

Downloads

14

Readme

Express JWT Library tokenease

===================

This library provides easy-to-use functions for handling JWT (JSON Web Token) authentication in Express.js applications. It simplifies token authentication, generation, refreshing, and revocation, making it more convenient for developers to implement JWT-based authentication in their projects.

Installation

To use this library, you need to install it via npm:

npm install tokenease

Usage

Below are the functions provided by this library:

authenticateToken(secretKey)

Middleware function to authenticate JWT tokens.

  • secretKey: The secret key used to verify JWT tokens.

     const { authenticateToken } = require('tokenease');

    app.use(authenticateToken('your_secret_key'));

generateToken(payload, secretKey, expiresIn = '1h')

Function to generate a JWT token.

  • payload: Data to be included in the token.

  • secretKey: The secret key used to sign the token.

  • expiresIn: Expiry duration for the token (default is 1 hour).

     const { generateToken } = require('tokenease');
    
     const token = generateToken({ userId: '123456' }, 'your_secret_key');

refreshAccessToken(refreshToken, secretKey, extractUser)

Function to generate a new access token using a refresh token.

  • refreshToken: The refresh token used to generate a new access token.

  • secretKey: The secret key used to verify tokens.

  • extractUser: Function to extract user data from the refresh token.

     const { refreshAccessToken } = require('tokenease');
    
    refreshAccessToken(refreshToken, 'your_secret_key', (decoded) => decoded.user);

blacklistToken(tokenIdentifier, schemaClass)

Function to revoke or blacklist a token.

  • tokenIdentifier: Identifier of the token to be blacklisted.

  • schemaClass: Schema class for interacting with the database.

     const { blacklistToken } = require('tokenease');
    
     blacklistToken(tokenIdentifier, YourSchemaClass)
     .then((success) => {
    console.log('Token blacklisted:', success);
    })
    .catch((error) => {
    console.error('Error blacklisting token:', error);
    });

revokeUserTokens(userId, schemaClass)

Function to revoke all tokens associated with a user.

  • userId: ID of the user whose tokens need to be revoked.

  • schemaClass: Schema class for interacting with the database.

     const { revokeUserTokens } = require('tokenease');
    
    revokeUserTokens(userId, YourSchemaClass)
    .then((success) => {
     console.log('Tokens revoked:', success);
    })
    .catch((error) => {
    console.error('Error revoking tokens:', error);
    });

revokeTokenByIdentifier(tokenIdentifier, schemaClass)

Function to revoke a specific token by its identifier.

  • tokenIdentifier: Identifier of the token to be revoked.

  • schemaClass: Schema class for interacting with the database.

     const { revokeTokenByIdentifier } = require('tokenease');
     revokeTokenByIdentifier(tokenIdentifier, YourSchemaClass).then((success) => {
     console.log('Token revoked:', success);
    })
    .catch((error) => {
    console.error('Error revoking token:', error);
    });

handleTokenExpiry(accessToken, refreshToken, secretKey, extractUser)

Function to handle token expiry by refreshing access tokens using refresh tokens.

  • accessToken: The access token to be verified.

  • refreshToken: The refresh token used to refresh the access token.

  • secretKey: The secret key used to verify tokens.

  • extractUser: Function to extract user data from the refresh token.

     const { handleTokenExpiry } = require('tokenease');
    
    handleTokenExpiry(accessToken, refreshToken, 'your_secret_key', (decoded) => decoded.user)
    .then((newAccessToken) => {
    console.log('New access token:', newAccessToken);
    }).catch((error) => {
    console.error('Error handling token expiry:', error);
    });

JWT Encryption Token Functions

This document describes two additional functions added to the JWT authentication library: generateEncryptionToken and verifyEncryptionToken. These functions enable developers to generate and verify JWT tokens with encrypted payloads, enhancing the security of token-based authentication in Express.js applications.

generateEncryptionToken

Description

The generateEncryptionToken function generates a JWT token with an encrypted payload. This function encrypts the provided payload using AES-256-CBC encryption and signs the JWT token with the encrypted payload. Developers can specify the secret key, encryption key, expiration time, and payload when calling this function.

Syntax

generateEncryptionToken(payload, secretKey, encryptionKey, [expiresIn])
  • payload: The payload object to be encrypted and included in the JWT token.
  • secretKey: The secret key used for signing the JWT token.
  • encryptionKey: The encryption key used to encrypt the payload.
  • expiresIn: (optional) The expiration time for the JWT token (default: '1h')

exemple

const token = generateEncryptionToken({ userId: '123', role: 'admin' }, 'mySecretKey', 'encryptionSecret');

verifyEncryptionToken

The verifyEncryptionToken function verifies a JWT token with an encrypted payload and decrypts the payload. This function checks the validity of the token signature using the provided secret key and then decrypts the encrypted payload using the specified encryption key. If the token is valid and the payload decryption is successful, the function returns the decrypted payload.

sayntax

verifyEncryptionToken(token, secretKey, encryptionKey)

  • token: The JWT token to be verified and decrypted.
  • secretKey: The secret key used for verifying the JWT token signature.
  • encryptionKey: The encryption key used to decrypt the payload.

exemple

const payload = verifyEncryptionToken(token, 'mySecretKey', 'encryptionSecret'); 
console.log(payload); // { userId: '123', role: 'admin' }

This library is licensed under the MIT License.