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

@bateswebtech/api-keys

v0.5.0

Published

API-Keys A zero-dependency, lightweight tool designed to assist in the creation, hashing, and validation of API keys. It provides a simple yet powerful interface for managing API keys with optional prefixes, SHA256 hashing for security, and validation met

Downloads

8

Readme

API-Keys

API-Keys A zero-dependency, lightweight tool designed to assist in the creation, hashing, and validation of API keys. It provides a simple yet powerful interface for managing API keys with optional prefixes, SHA256 hashing for security, and validation methods to ensure that API keys are correctly formatted and authorized.

Features

  • Generate API keys with custom prefixes.
  • SHA256 hashing of API keys for enhanced security.
  • Validate API keys and authorization headers.
  • Compare API keys with their hashed counterparts to confirm validity.

Installation

To install API-Keys, you need to have Node.js installed on your system. Once Node.js is installed, you can add API-Keys to your project by running the following command in your project's root directory:

npm install @bateswebtech/api-keys

Usage

Importing the Module

First, import the createAPIKey, hashAPIKey, validateHeaders, and validateHash functions from the module:

import { createAPIKey, hashAPIKey, validateHeaders, validateHash } from '@bateswebtech/api-keys';

Creating an API Key

To create an API key, use the createAPIKey function by providing an optional configuration object. This function generates an API key, applies a prefix (if provided), and returns both the key and its SHA256 hashed version.

import { createAPIKey } from '@bateswebtech/api-keys';

async function generateAPIKey() {
    const apiKeyData = await createAPIKey({ prefix: 'myapp_' });
    console.log(apiKeyData);
    // Output: { key: 'myapp_...', hashedKey: '...' }
}
generateAPIKey();

Hashing an API Key

To hash an existing API key for secure storage or comparison, use the hashAPIKey function. This function takes an API key as input and returns a Promise that resolves to the SHA256 hashed version of the API key.

import { hashAPIKey } from '@bateswebtech/api-keys';

async function hashKey() {
  const hashedKey = await hashAPIKey('myapp_someApiKey');
  console.log(hashedKey);
  // Output: SHA256 hashed version of 'myapp_someApiKey'
}
hashKey();

Validating Headers

To validate authorization headers, particularly to ensure they contain a valid API key or token, use the validateHeaders function. This function checks if the provided headers contain the expected authorization type (e.g., "Bearer") and a token. It returns the token if the authorization header is valid, or an error object if there's an issue.

import { validateHeaders } from '@bateswebtech/api-keys';

function validateAuthHeader() {
  // Simulating headers object similar to what you'd receive in an HTTP request
  const headers = new Headers();
  headers.append('authorization', 'Bearer someApiKey');

  const token = validateHeaders(headers, { auth: 'Bearer', header: 'authorization' });
  if (token.error) {
    console.error(token.error);
  } else {
    console.log('Token is valid:', token);
  }
}
validateAuthHeader();

Validating a Hashed Key

To verify the validity of an API key by comparing it against its hashed version, use the validateHash function. This can be particularly useful for authenticating or authorizing API requests. The function accepts an API key and its hashed counterpart as parameters, returning a boolean indicating whether the key matches the hashed version.

import { validateHash } from '@bateswebtech/api-keys';

async function verifyKey() {
  const isValid = await validateHash('someApiKey', 'expectedHashedValue');
  console.log('Is valid:', isValid);
  // Output: true or false
}
verifyKey();

Contributing

Contributions are always welcome! If you have any suggestions, bug fixes, or enhancements, please feel free to fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you would like to change.

Ensure to update tests as appropriate and adhere to the project's coding standards.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.