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

json-token-manager

v1.0.1

Published

A token manager for handling JSON Web Tokens

Downloads

19

Readme

JsonTokenManager

JsonTokenManager is a TypeScript library for managing API tokens. It handles token storage, lifecycle management, and automatic refreshing, supporting multiple retry attempts and customizable configurations.

Overview

JsonTokenManager simplifies the management of API tokens by automating the refresh process and handling token expiration seamlessly. The library is designed to be configurable, allowing you to tailor it to your specific API requirements. Key features include automatic token refreshing, support for various configuration options, and the ability to manually refresh tokens when needed. Future improvements will include optimized storage solutions, enhanced security measures, concurrent queue management, and token caching.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue on GitHub. Feel free to fork the repository and submit pull requests for any enhancements or bug fixes.

Features

  • Automatic token refreshing based on expiration time.
  • Configurable retry logic for token refresh attempts.
  • Support for customizable request headers and body.
  • Buffer time to refresh tokens before they expire.
  • Manual token refresh capability.

Unimplemented Features

  1. Optimized Storage : Improve storage by moving credentials and tokens out of memory to a more secure and efficient storage solution.
  2. Enhanced Security : Implement additional security measures for storing and handling tokens and credentials.
  3. Concurrent Queue Management : Introduce a concurrent queue to handle multiple token queries efficiently.
  4. Token Caching : Implement a caching mechanism to store and retrieve tokens efficiently, reducing unnecessary token refresh requests.

Installation

Install the package using npm:

npm install json-token-manager

Usage

Import the JsonTokenManager

Import the JsonTokenManager class in your TypeScript file:

import JsonTokenManager from 'json-token-manager';

Configure the Token Manager

Create a configuration object with the necessary details:

const config = {
  tokenUrl: 'https://api.example.com/oauth/token',
  body: {
    grant_type: 'client_credentials',
    client_id: 'your-client-id',
    client_secret: 'your-client-secret'
  },
  header_list: {
    'Content-Type': 'application/json'
  },
  tokenField: 'access_token',
  expireField: 'expires_in',
  retryAttempts: 3, // Optional, default is 3
  retryDelay: 1000, // Optional, default is 1000ms
  bufferTime: 60000 // Optional, default is 60000ms (1 minute)
};

Initialize the Token Manager

Create an instance of JsonTokenManager with the configuration:

const tokenManager = new JsonTokenManager(config);

Get a Token

Use the getToken method to obtain a valid token. This method will refresh the token if it is expired or about to expire.

const token = await tokenManager.getToken();
console.log(token);

Manual Token Refresh

If needed, you can manually refresh the token using the manualRefresh method:

await tokenManager.manualRefresh();

Example

Here's a complete example of using JsonTokenManager to make an API request:

import JsonTokenManager from 'json-token-manager';
import axios from 'axios';

const config = {
  tokenUrl: 'https://api.example.com/oauth/token',
  body: {
    grant_type: 'client_credentials',
    client_id: 'your-client-id',
    client_secret: 'your-client-secret'
  },
  header_list: {
    'Content-Type': 'application/json'
  },
  tokenField: 'access_token',
  expireField: 'expires_in'
};

const tokenManager = new JsonTokenManager(config);

(async () => {
  try {
    const token = await tokenManager.getToken();

    const response = await axios.get('https://api.example.com/resource', {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    });

    console.log(response.data);
  } catch (error) {
    console.error('Error making API request:', error);
  }
})();

Configuration Options

  • tokenUrl: The URL to request a new token.
  • body: The body of the token request.
  • header_list: Headers for the token request.
  • tokenField: The field in the response containing the token.
  • expireField: The field in the response containing the expiration time (in seconds).
  • retryAttempts: (Optional) Number of retry attempts for token refresh (default is 3).
  • retryDelay: (Optional) Delay between retry attempts in milliseconds (default is 1000ms).
  • bufferTime: (Optional) Buffer time in milliseconds to refresh token before expiry (default is 60000ms or 1 minute).

Error Handling

If the token refresh fails, the JsonTokenManager will retry based on the configured retryAttempts and retryDelay. If all attempts fail, an error is thrown.

License

This project is licensed under the MIT License.