cidp-node-api-sdk

1.0.6 • Public • Published

CIDP NODE API SDK

A library for application built using node js. Is used to authorize requests to api resources by validating access token.

Features:

  • node library
  • unit tests for the library
  • a demo application using express web framework that consumes the library

Common tasks are present as npm scripts:

  • npm run build to build the library
  • npm run start to run a server with the demo app using express
  • npm run test run unit tests

What's in the CIDP NODE API SDK?

demo/
    ├── app.js
lib/
   ├── index.js
   └── services/
        └── claimService.js
        └── jwtService.js

Files inside lib/ "belong" to library, while demo/ contains demo applications that loads the library.

Libraries do not run by themselves, so it's very useful to have this "demo" apps while developing to see how your library would look like to consumers.

The build step

You can build the library by running npm run build. This will generate a dist/ directory with all the entry points described above.

All the logic for creating the build can be found in ./gulpfile.js. It consists of:

  • Identify any security vulnerabilities
  • Clean dist folder.
  • Transpile with babel.
  • Copy the source to dist folder.

Testing

The CIDP API NODE SDK includes a directory called test containing unit tests to verify it works.

To run the unit tests, do npm run test

Using in the node application

Install node package in your app : npm install cidp-node-api-sdk --save

Import the module in your app. Set the authSettings properties to match the server configuration.

 
 
var cidp = require('cidp-node-api-sdk');
 
var app = express();
 
//Settings defines
//1.authorityURI - a CIDP instance uri, used by sdk to check if token issuer is a trusted one
//2.The received token should have the required audiece in order to be authorized
var settings = {
  authorityUri: "IdentityServerUri",
  audience: "audience"
};
 
// use validateJwt middleware to:
//1.Retrieve from authorityUri trusted issuer host names
//2.Check if token issuer host name is trusted
//3.Connect to issuer that generate the received access token
//4.Get metadata about token signing key.
//5.Validate token is not expired and is valid for the audience specified
app.use(cidp.validateJwt(settings));
 
//use hasClaim middleware to check token contains the claim 
//hasClaim(claimKey,claimValue)
//claimValue should be an array of strings.It allows to give access to profile resource if user role is Admin or Dev
app.get('/profile', cidp.hasClaim('role', ['Admin','Dev']), function (req, res) {
  const user = req.user;
  return res.json(user);
});
 
//or we can grant access to profile resource if token has profile scope
app.get('/profile', cidp.hasClaim('scope', ['profile']), function (req, res) {
  const user = req.user;
  return res.json(user);
});
 

Readme

Keywords

Package Sidebar

Install

npm i cidp-node-api-sdk

Weekly Downloads

7

Version

1.0.6

License

MIT

Last publish

Collaborators

  • reza.panah
  • crisboarna