@kobotech/core-auth
Provides Authentication & Authorization utilities
Last updated 2 months ago by ctumwebaze .
ISC · Repository · Original npm · Tarball · package.json
$ cnpm install @kobotech/core-auth 
SYNC missed versions from official npm registry.

Kobo360 Auth Library

This library provides utilities to support the authentication of API requests within the services

Usage

Add the dependency of this library in your project's package.json in the dependencies section;

{
  "dependencies": {
    "@kobotech/core-auth": "1.0.0"
  }
}

Run npm install to install the defined dependencies;

How to use in your library

const express = require("express");
const coreAuth = require("@kobotech/core-auth");

const app = express();

app.get(
  "/user",
  coreAuth.authenticate(jwtSecret, { view: [coreAuth.roles.ADMIN] }, ["/user"]),
  coreAuth.hasPermission("view", coreAuth.roles.ADMIN)
);

In the setup above, a /user endpoint is defined along with the authenticate middleware and the hasPermission middleware.

The authenticate middleware takes three arguments; jwtSecret, accessControlList end exclusionList

  • jwtSecret - this is the secret that is used in the creation and verification of JWT tokens
  • accessControlList - an object of allowed actions defined in the format;
{
  "<action>": ["<role>", "<role>"]
}

e.g.

{
  "admin": [
    coreAuth.roles.SUPER_ADMIN,
    coreAuth.roles.ADMIN,
    coreAuth.roles.COMMS,
    coreAuth.roles.OPS,
    coreAuth.roles.FINANCE,
    coreAuth.roles.ECEO
  ],
  "all": [
    coreAuth.roles.SUPER_ADMIN,
    coreAuth.roles.ADMIN,
    coreAuth.roles.COMMS,
    coreAuth.roles.OPS,
    coreAuth.roles.CUSTOMER,
    coreAuth.roles.DRIVER,
    coreAuth.roles.PARTNER,
    coreAuth.roles.RECIPIENT,
    coreAuth.roles.FINANCE,
    coreAuth.roles.FIELD_OFFICER,
    coreAuth.roles.ECEO,
    coreAuth.roles.KOBO_AGENT,
    coreAuth.roles.BANK
  ]
}
  • exclusionList - an array of endpoints that will be excluded from authentication.

Also in the configuration above, the hasPermission middleware, which can also be invoked via the hasResource is configured to determine whether the authenticate user has the required permission to access the resource.

As defined, the hasPermission middleware takes two arguments; permission and userType.

  • permission - this corresponds to the object key in the defined accessControlList object defined in the authenticate middleware.
  • userType - this is an optional parameter that when defined, is used to enforce that the logged in user must be of the given userType/role before they can access the resource. However, even when not defined, the userType of the user is used to determine whether they have access to the resource based on the accessControlList defined in the authenticate middleware.

Current Tags

  • 1.2.7                                ...           latest (2 months ago)

23 Versions

  • 1.2.7                                ...           2 months ago
  • 1.2.6                                ...           2 months ago
  • 1.2.5                                ...           2 months ago
  • 1.2.4                                ...           2 months ago
  • 1.2.3                                ...           2 months ago
  • 1.2.2                                ...           2 months ago
  • 1.2.1                                ...           3 months ago
  • 1.2.0                                ...           3 months ago
  • 1.1.12                                ...           3 months ago
  • 1.1.11                                ...           3 months ago
  • 1.1.10                                ...           3 months ago
  • 1.1.9                                ...           3 months ago
  • 1.1.8                                ...           3 months ago
  • 1.1.7                                ...           3 months ago
  • 1.1.6                                ...           3 months ago
  • 1.1.5                                ...           3 months ago
  • 1.1.4                                ...           3 months ago
  • 1.1.3                                ...           3 months ago
  • 1.1.2                                ...           3 months ago
  • 1.1.1                                ...           3 months ago
  • 1.1.0                                ...           3 months ago
  • 1.0.1                                ...           4 months ago
  • 1.0.0                                ...           4 months ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 23
Last Month 24
Dependencies (6)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |