Giter VIP home page Giter VIP logo

koa-auth-basic-secure's Introduction

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.

koa-auth-basic-secure's People

Contributors

cjanietz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.