# jwt-service-handler

This library can be used in any js service for jwt token validation (for m2m and frontend tokens), forwarding, caching and refreshing jwt_enabled_services list in your service.

## Table of Contents

- [Getting Started](#getting-started)
- [Additional Configuration](#additional-configuration)

## Getting Started

To install jwt-service-handler package add the following section to your `package.json`:

```json
"@orchard/jwt-service-handler": "0.1.14",
```

Import the library in your file:

```javascript
import JwtHandler from '@orchard/jwt-service-handler';
```

To validate your jwt token add the below validation code:

```javascript
const tokenHandlerInst = new JwtHandler();
const isValidAuthorization = await tokenHandlerInst.validateAuthorization(
    jwtToken, serviceName, correlationId, logInfo);
```

To cache jwt_enabled_services in your service add the below code:

```javascript
const cacheHandlerInst = new JwtHandler(yourCacheObject, yourServiceName);
cacheHandlerInst.cacheJwtServicesList();
```

It uses apollo-server-caching package with InMemoryLRUCache class for caching.
If there is no existing InMemoryLRUCache cache object in your application library will create its own InMemoryLRUCache cache

```javascript
const cacheHandlerInst = new JwtHandler(null, yourServiceName);
cacheHandlerInst.cacheJwtServicesList();
```

To retrieve jwt_enabled_services from cache in your service add the below code:

```javascript
const jwtenabledServiceslist = await cacheHandlerInst.retrieve();
```

To start a refresh job for updating jwt_enabled_services list in your service cache add the below code:

```javascript
const cacheHandlerInst = new JwtHandler(yourCacheObject, yourServiceName);
cacheHandlerInst.cacheRefreshScheduler(logInfo);
```
## To use in other projects:

The package is hosted by our private npm registry packagecloud.io. The package is available as @orchard/jwt-service-handler.

### Using packagecloud.io
- Add the registry to your npm configuration.
  ```bash
  # add registry to your .nmprc file
  curl -s ${PACKAGECLOUD_USER}:@packagecloud.io/install/repositories/orchardit/npm/script.node.sh | bash
  ```

- Then, include the library to the project, using regular yarn commands.:
  ```bash
  yarn add jwt-service-handler
  ```

### Using github tags
- Add the desired version by adding a github link to the tagged version.
  ```json
  {
    "dependencies": {
      "@orchard/jwt-service-handler": "0.1.14"
  }
  ```
- Then, install the library using yarn.:
  ```bash
  yarn install
  ```
## Additional Configuration
### Installing jwt-service-handler package from https://packagecloud.io/
To authorize any repo to access and install the private package jwt-service-handler from packagecloud.io add below step on the jenkins deployment builds for that repo,
https://****:@packagecloud.io/install/repositories/orchardit/npm/script.node.sh
### Access to jwt_enabled_services secret
Your service should have read and update access to jwt_enabled_services secret.
This requires that your service is terraformed with latest terraform-fargate version (>=1.5.1).