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

koa-auth-basic-secure

v1.0.1

Published

A library implementing HTTP basic authentication for koa using hashing based methods

Downloads

11

Readme

koa-auth-basic-secure

npm version

Build Status

codecov

This library is a middleware for protecting Koa Servers/Endpoints with HTTP Basic authentication. The focus thereby is on providing a hash based authentication, which means no clear text passwords are written to code. This is similar to the Spring Security approach to HTTP Basic protection.

Features:

  • Multiple users
  • Password hashing
  • Integrated with bcrypt or bcryptjs and Node out of the box
  • 100% Customizable hashing implementation
  • Optional Caching for ensuring low latency response times
  • Batteries included (Tool for hashing Password from terminal)
  • Written in 100% Typescript and therefore types are properly included

Howto

Sample:

import * as Koa from 'koa';
import { basicAuth } from 'koa-basic-auth-secure';
const app = new Koa();
app.use(
    basicAuth({
        credentials: [{ username: 'example', password: '<HASH HERE>' }],
        passwordProtection: 'mySecret'
    })
);
app.use(ctx => {
    ctx.body = 'Hello World';
});

To get the above mentioned hash for a user, use the following command:

hashPassword hmac --secret mySecret --input example
hashPassword bcrypt --rounds 10 --input test

For the cmd line t work you can use either yarn run hashPassword ... or add hashPassword to your node scripts.

The library does not force you to use a specific hash implementation, it comes with Hmac (crypto) and bcrypt included.

If you want to use bcrypt, you either have to do: npm install --save bcrypt or npm install --save bcryptjs

Configuration

{
        credentials: UsernamePasswordCombination[];
        passwordProtection: PasswordService | string;
        customRealm?: string;
        cache?: Cache;
}
  • credentials (required) this is the collection of username password objects. Each entry requires a username and a password (Hashed depending) on your password protection config. e.g. [{username: "test", password: "<HASH>"}].

  • passwordProtection (required) this is the mechanism to verify the password, it can bei either a secret which is used for Hmac by default or any other custom implementation such as BCryptPasswordService (For examples see the test cases)

  • customRealm A custom naming for the password prompt which is presented by the browser to the user.

  • cache A cache which fulfills the interface of get/set, such as node-cache or the simple implementation in this library. This is only of interest if computationally expensive hashing such as bcrypt is used.

Changelog

1.0.1

  • Properly added types
  • Updated dependencies

Remarks

This library is compiled with a compatibility target of ES6 and therefore only supports Node 8.21 and newer

Why use bcrypt over hmac based hashing? Depending on the personal security requirements bcrypt is typically stronger as the user of computational overhead is a lot stronger than just "salting" a password. It makes bruteforcing even using password lists harder.

Why not use scrypt instead of bcrypt? From experience the library is a little flaky and hard to debug further scrypt has only been included in very recent versions of Node in crypto.