1.0.0 • Published 6 years ago

cidp-api-sdk v1.0.0

Weekly downloads
3
License
MIT
Repository
-
Last release
6 years ago

CIDP API NODE 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 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.
  • Deploy to github.

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-api-sdk --save

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

var jwt = require('cidp-api-sdk');

var app = express();

var settings = {
  issuer: "IdentityServerUri",
  audience: "audience"
};

// use validateJwt middleware to:
//1.Connect to IdentityServer
//2.Get metadata about token signing key.
//3.Validate token is not expired and is valid for the audience specified
app.use(jwt.validateJwt(settings));

//use validateClaim middleware to check token contains the claim 
//validateClaim(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', jwt.validateClaim('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', jwt.validateClaim('scope', ['profile']), function (req, res) {
  const user = req.user;
  return res.json(user);
});