# @ministryofjustice/fb-user

> Form Builder User Data Store, User File Store, and JSON Web Token Clients (for Node)

Latest version **1.0.1** (published 2020-01-01) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @ministryofjustice/fb-user
pnpm add @ministryofjustice/fb-user
yarn add @ministryofjustice/fb-user
bun add @ministryofjustice/fb-user
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-01-01 |
| First published | 2019-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 30.9 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Form Builder Team |
| Maintainers | adamsilver, asmega, elena_vi, emileswarts, form-builder-team, romidane, sequencemedialimited, solidgoldpig, stevemarshall, todderz, umaar, whatterz |

## Links

- npm: https://www.npmjs.com/package/@ministryofjustice/fb-user
- Repository: https://github.com/ministryofjustice/fb-user
- Homepage: https://github.com/ministryofjustice/fb-user#readme
- Issues: https://github.com/ministryofjustice/fb-user/issues
- npm.io page: https://npm.io/package/@ministryofjustice/fb-user

## Dependencies (5)

- [got](https://npm.io/package/got.md) 10.2.0
- [aes256](https://npm.io/package/aes256.md) ^1.0.4
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.1.0
- [@ministryofjustice/fb-utils-node](https://npm.io/package/@ministryofjustice/fb-utils-node.md) ^0.1.8

## Recent versions

- 1.0.1 (latest) — 2020-01-01
- 1.0.0 — 2019-12-18

## README

## JSON Web Token client

Base client for requests to endpoints which require a JSON Web Token for authentication.

### Using a client

``` javascript
// require the client class
const FBJWTClient = require('@ministryofjustice/fb-user/jwt/client')

// create a client instance
const jwtClient = new FBJWTClient(serviceSecret, serviceToken, serviceSlug, microserviceUrl, [errorClass])
```

#### `serviceSecret`

A `serviceSecret` is _required_.

The constructor will throw an error if no `serviceSecret` is provided.

#### `serviceToken`

A `serviceToken` is _required_.

The constructor will throw an error if no `serviceToken` is provided.

#### `serviceSlug`

A `serviceSlug` is _required_.

The constructor will throw an error if no `serviceSlug` is provided.

#### `microserviceUrl`

A `microserviceUrl` is _required_.

The constructor will throw an error if no `microserviceUrl` is provided.

#### `errorClass`

An `errorClass` is _optional_.

### Extending

``` javascript
// extend base class
class FBMyClient extends FBJWTClient {
  constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl, myVar) {
    super(serviceSecret, serviceToken, serviceSlug, microserviceUrl)
    // do something with additional constructor argument
    this.myVar = myVar
  }
}

const myClient = new FBMyClient('service_secret', 'service_token', 'myservice', 'http://myservice', 'my var')
```

``` javascript
// extend base class with custom error
class FBAnotherClient extends FBJWTClient {
  constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl) {
    // create custom error class
    class FBAnotherClientError extends FBJWTClient.prototype.ErrorClass {}
    super(serviceSecret, serviceToken, serviceSlug, microserviceUrl, FBAnotherClientError)
  }
}
```

### Methods

- generateAccessToken

  Generate a JWT access token

- createEndpointUrl

  Create the URL for an endpoint

- sendGet

  Dispatch `GET` requests to an endpoint

- sendPost

  Dispatch `POST` requests to an endpoint

- encrypt

  Encrypt data with AES 256

- decrypt

  Decrypt data

- encryptUserIdAndToken

  Encrypt the user ID and token using the service secret

- decryptUserIdAndToken

  Decrypt the user ID and token using the service secret

- handleRequestError

  This function will be invoked with an error an argument when the transaction fails

- createRequestOptions

  Create request options, whether `GET` or `POST`

- throwRequestError

  This function can be invoked to throw request errors

## JSON Web Token client implementations

### Data Store client

Client for requests to datastore endpoints.

#### Using a client

``` javascript
// load client
const FBUserDataStoreClient = require('@ministryofjustice/fb-user/datastore/client')

// initialise client
const userDataStoreClient = new FBUserDataStoreClient(serviceSecret, serviceToken, serviceSlug, userDataStoreUrl)
```

#### Fetching and storing

``` javascript
// fetch user data
const userData = await userDataStoreClient.getData(userId, userToken)

// store user data
await userDataStoreClient.setData(userId, userToken, userData)
```

### File Store client

Client for requests to filestore endpoints.

#### Using a client

``` javascript
// load client
const FBUserFileStoreClient = require('@ministryofjustice/fb-user/filestore/client')

// initialise client
const userFileStoreClient = new FBUserFileStoreClient(serviceSecret, serviceToken, serviceSlug, userFileStoreUrl)
```

#### Fetching and storing

``` javascript
// fetch user file
const userFile = await userFileStoreClient.fetch(userId, userToken, fingerprint)
// userFile => { file }

// With the policy defined
const policy = { [max_size], [expires], [allowed_types] }

let uploadDetails

// Either
// store user file
uploadDetails = await userFileStoreClient.store(userId, userToken, file, policy)
// uploadDetails => { fingerpint, url, size, type, date }

// Or
// store user file from file path
uploadDetails = await userFileStoreClient.storeFromPath(userId, userToken, filePath, policy)
```

---
_Source: https://npm.io/package/@ministryofjustice/fb-user · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
