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 🙏

© 2025 – Pkg Stats / Ryan Hefner

egain-ps-utils

v3.0.2

Published

eGain PS Utils. A utility package for eGain PS

Readme

egain-ps-utils

egain-ps-utils is a collection of utility functions designed to simplify common development tasks. This package provides helpers and methods to boost your development workflow, and for the 3.0.0 release, we're offering a convenient jwt authorizer utility for handling authorization-related tasks with dual module support (ESM and CommonJS).

Release Notes (3.0.2)

  • Dual Module Support: Now supports both ESM (import) and CommonJS (require) syntax
  • TypeScript Support: Full TypeScript definitions included
  • Requires Node.js 18+ (uses native fetch)
  • Improved compatibility and performance
  • Updated documentation and usage examples
  • Enhanced JWT authorizer utility for Azure AD

Requirements

  • Node.js 18 or higher (uses native fetch)
  • ps-chronicle: Use the latest version (^1.0.6 or higher) for best compatibility with this package.

Features

  • validateToken: A utility function to assist with authorization checks, ensuring that only authorized users can access specific resources or perform particular actions.

    • This validator will check for:
      1. token expiry
      2. issuer
      3. audience
      4. certificate
    • This function will return a boolean value indicating whether the token is valid or not.
  • generateAuthResponse: A utility function to assist with generating authorization responses. This will return a policy document with the effect and the methodArn.

  • isTokenIssuedForValidClaimTenantId: A utility function to validate if a token is issued for a valid claim tenant ID.

  • getDecodedJwtToken: A utility function to decode a JWT token string and return the decoded token object. This is useful for extracting claims and other information from JWT tokens without validation.

Prerequisites

  • AWS Secrets Manager SDK v3 (if using with AWS Lambda and secrets)

Create a secret in AWS Secrets Manager with the following format:

{
    "audience": "xxxx",
    "issuer": "https://xxxx.com/xxxxxx/xx.x"
}

To get the audience and issuer values, you can use the following steps:

  1. Get the Metadata from your identity provider.
  2. Use these values as the audience and issuer in the secret.

Installation

Using npm:

npm install egain-ps-utils

Using yarn:

yarn add egain-ps-utils

Usage

ESM (ES6 Modules)

import { validateToken, generateAuthResponse, isTokenIssuedForValidClaimTenantId, getDecodedJwtToken } from 'egain-ps-utils';

const authorize = async (event) => {
  try {
    const authorization = event.headers.Authorization || event.headers.authorization;
    if (!authorization) {
      throw new Error('Authorization token is missing');
    }

    const secretName = 'xxxxx';

    // Validate the token
    const { isTokenValid } = await validateToken(authorization, secretName);
    if (isTokenValid) {
      return generateAuthResponse('Allow', event.methodArn);
    } else {
      return generateAuthResponse('Deny', event.methodArn);
    }
  } catch (error) {
    console.error('Authorization error:', error);
    return generateAuthResponse('Deny', event.methodArn);
  }
};

// Example: Decode JWT token to extract claims
const extractTokenClaims = (jwtString) => {
  try {
    const decodedToken = getDecodedJwtToken(jwtString);
    return {
      userId: decodedToken.sub,
      email: decodedToken.email,
      tenantId: decodedToken.tid,
      expiresAt: new Date(decodedToken.exp * 1000)
    };
  } catch (error) {
    console.error('Error decoding token:', error);
    return null;
  }
};

export { authorize, extractTokenClaims };

CommonJS

const { validateToken, generateAuthResponse, isTokenIssuedForValidClaimTenantId, getDecodedJwtToken } = require('egain-ps-utils');

const authorize = async (event) => {
  try {
    const authorization = event.headers.Authorization || event.headers.authorization;
    if (!authorization) {
      throw new Error('Authorization token is missing');
    }

    const secretName = 'xxxxx';

    // Validate the token
    const { isTokenValid } = await validateToken(authorization, secretName);
    if (isTokenValid) {
      return generateAuthResponse('Allow', event.methodArn);
    } else {
      return generateAuthResponse('Deny', event.methodArn);
    }
  } catch (error) {
    console.error('Authorization error:', error);
    return generateAuthResponse('Deny', event.methodArn);
  }
};

// Example: Decode JWT token to extract claims
const extractTokenClaims = (jwtString) => {
  try {
    const decodedToken = getDecodedJwtToken(jwtString);
    return {
      userId: decodedToken.sub,
      email: decodedToken.email,
      tenantId: decodedToken.tid,
      expiresAt: new Date(decodedToken.exp * 1000)
    };
  } catch (error) {
    console.error('Error decoding token:', error);
    return null;
  }
};

module.exports = { authorize, extractTokenClaims };

TypeScript

import { validateToken, generateAuthResponse, isTokenIssuedForValidClaimTenantId, getDecodedJwtToken } from 'egain-ps-utils';

interface LambdaEvent {
  headers: {
    Authorization?: string;
    authorization?: string;
  };
  methodArn: string;
}

interface TokenClaims {
  userId?: string;
  email?: string;
  tenantId?: string;
  expiresAt?: Date;
}

const authorize = async (event: LambdaEvent) => {
  try {
    const authorization = event.headers.Authorization || event.headers.authorization;
    if (!authorization) {
      throw new Error('Authorization token is missing');
    }

    const secretName = 'xxxxx';

    // Validate the token
    const { isTokenValid } = await validateToken(authorization, secretName);
    if (isTokenValid) {
      return generateAuthResponse('Allow', event.methodArn);
    } else {
      return generateAuthResponse('Deny', event.methodArn);
    }
  } catch (error) {
    console.error('Authorization error:', error);
    return generateAuthResponse('Deny', event.methodArn);
  }
};

// Example: Decode JWT token to extract claims
const extractTokenClaims = (jwtString: string): TokenClaims | null => {
  try {
    const decodedToken = getDecodedJwtToken(jwtString);
    return {
      userId: decodedToken.sub,
      email: decodedToken.email,
      tenantId: decodedToken.tid,
      expiresAt: new Date(decodedToken.exp * 1000)
    };
  } catch (error) {
    console.error('Error decoding token:', error);
    return null;
  }
};

export { authorize, extractTokenClaims };

Notes

  • This package requires Node.js 18+ for native fetch support.
  • For best results, use the latest version of ps-chronicle (at least 1.0.6).
  • For Azure AD JWT validation, ensure you provide the correct issuer, audience, and kid in the options object.
  • For AWS Lambda usage, ensure your secrets are set up as described above.
  • The package automatically detects your module system and provides the appropriate format (ESM or CommonJS).
  • The getDecodedJwtToken function only decodes the JWT without validation - use validateToken for secure token validation.