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

node-token-auth

v1.0.3

Published

A Node module that provides auth token generation and manaagment.

Downloads

1

Readme

This is a Node module that provides auth token generation and management. It has only been test with Express apps so far.

The tokens are encrypted strings that contain three pieces of information: the username, the client IP address, and the timestamp in milliseconds after which the session will expire. The goal is to make it very difficult for a hacker to impersonate a valid user or hijack an existing session. Even if a hacker was able to intercept one of these tokens, it would be difficult for them to utilize because they would have to spoof the client IP address and do that within the session timeout.

This module provides five functions.

configure

This function must be called before any of the others. It takes the name of an encryption algorithm, a password to be used with it, and a session timeout in minutes. Encryption is provided by the builtin Node crypto module. Supported encryption algorithms can be seen by running the following code:

const crypto = require('crypto');
for (const cipher of crypto.getCiphers()) {
  console.log(cipher);
}

A recommended algorithm is "aes-256-ctr".

generateToken

This function takes a username, request object, and response object. It creates an encrypted token and returns it in the "Authorization" response header. It also uses setTimeout to delete the token from its cache after the session expires. If global.socket is set, it assumes that is a reference to a socket.io socket and emits a "session-timeout" event rather than deleting the token from its cache. This allows clients to proactively end a session rather than waiting for the user to initiate a request that causes a new token check.

authorize

This function takes a request and response object. It extracts a token from the "Authorization" request header and returns a boolean indicating whether the token matches one that was created earlier using the generateToken function. Any REST services that require authentication should call this and exit if it returns false as shown below:

if (!auth.authorize(req, res)) return;

deleteToken

This function takes are request object and deletes the token found in its "Authorization" request header from the token cache. This is useful when a user logs out before their session expires. It prevents future successful use of that token.

deleteExpiredTokens

This function can be called at any time to purge expired tokens from the cache. This typically is not necessary unless global.socket is set since otherwise tokens are automatically deleted when their session expires.