# @inventsable/gatekeeper

> Utilities for Moore auth and decryption

Latest version **1.0.0** (published 2021-05-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @inventsable/gatekeeper
pnpm add @inventsable/gatekeeper
yarn add @inventsable/gatekeeper
bun add @inventsable/gatekeeper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-05-24 |
| First published | 2021-05-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| Author | Thomas Scharstein II |
| Maintainers | inventsable |

## Links

- npm: https://www.npmjs.com/package/@inventsable/gatekeeper
- npm.io page: https://npm.io/package/@inventsable/gatekeeper

## Dependencies (3)

- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.912.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [@inventsable/smalltalk](https://npm.io/package/@inventsable/smalltalk.md) ^1.0.0

## Recent versions

- 1.0.0 (latest) — 2021-05-24

## README

# @inventsable/gatekeeper

## Installation

```bash
npm i @inventsable/gatekeeper
```

```js
import {
  encrypt,
  decrypt,
  getAWSSecrets,
  parseJWTString,
  login,
  makeRequest,
  requestFromServer,
  isJSON,
} from "@inventsable/gatekeeper";
```

---

## API

<dl>
<dt><a href="https://github.com/Inventsable/smalltalk#encryptvalueany-passwordstring--returns-cipherparams">encrypt(value, password)</a> ⇒ <code>CiperParams</code> | <code>Error</code></dt>
<dd><p>Encodes any valid string, object or array data with a given password to a CipherParams object containing ciphertext, salt and initialization vector keys inline with CryptoJS</p>
</dd>
<dl>
<dt><a href="https://github.com/Inventsable/smalltalk#decryptvaluecipherparams-string-passwordstring--returns-any">decrypt(CipherParams, password)</a> ⇒ <code>any</code> | <code>Error</code></dt>
<dd><p>Decodes any valid CipherParams string with a given password to its original value</p>
</dd>
<dl>
<dt><a href="#async-loginusername-password-url--object--error"><strong>async</strong> login(username, password, url)</a> ⇒ <code>Object</code> | <code>Error</code></dt>
<dd><p>Thenable method for remote OAuth access to Moore&#39;s SSO system</p>
</dd>
<dt><a href="#async-makerequestdata-headers-asjson--object--error"><strong>async</strong> makeRequest(data, headers, asJSON)</a> ⇒ <code>Object</code> | <code>Error</code></dt>
<dd><p>Thenable shorthand method for POST with dynamic body and header content</p>
</dd>
<dt><a href="#async-requestfromserverurl-requestoptions-asjson--object"><strong>async</strong> requestFromServer(url, requestOptions, asJSON)</a> ⇒ <code>Object</code></dt>
<dd></dd>
<dt><a href="#async-getawssecretsconfig--object--error"><strong>async</strong> getAWSSecrets(config)</a> ⇒ <code>Object</code> | <code>Error</code></dt>
<dd><p>Thenable method for querying AWS Key Manager contents, later to be used for all decryption and encryption.
By default this assumes the existence of environment variables containing the 4 required keys designated below,
but these can be passed as the parameter to the function in a destructured options/config format</p>
</dd>
<dt><a href="#parsejwtstringstringvalue--object--error">parseJWTString(stringValue)</a> ⇒ <code>Object</code> | <code>Error</code></dt>
<dd><p>Shorthand method to parse JWT (JSON web token) string contents and retrieve original data object</p>
</dd>
<dt><a href="#isjsontext--boolean">isJSON(text)</a> ⇒ <code>Boolean</code></dt>
<dd><p>Shorthand to determine whether data is string or stringified JSON</p>
</dd>
</dl>

<a name="login"></a>

## **async** login(username, password, url) ⇒ <code>Object</code> \| <code>Error</code>

Thenable method for remote OAuth access to Moore's SSO system

**Kind**: global function

| Param    | Type                | Default                                                                              | Description                                 |
| -------- | ------------------- | ------------------------------------------------------------------------------------ | ------------------------------------------- |
| username | <code>String</code> |                                                                                      | Exact string of user to log in              |
| password | <code>String</code> |                                                                                      | Exact string of password associated to user |
| url      | <code>String</code> | <code>https://dev-sso.gomsi.net/auth/realms/sso/protocol/openid-connect/token</code> | (optional) URL of SSO auth origin to query  |

```js
const loginToken = await login(process.env.username, process.env.password);
console.log("\r\nResult of login:");
console.log(loginToken);
```

<a name="parseJWTString"></a>

## parseJWTString(stringValue) ⇒ <code>Object</code> \| <code>Error</code>

Shorthand method to parse JWT (JSON web token) string contents and retrieve original data object

**Kind**: global function  
**Returns**: <code>Object</code> \| <code>Error</code> - The parsed object result or error when invalid

| Param       | Type                | Description                                                             |
| ----------- | ------------------- | ----------------------------------------------------------------------- |
| stringValue | <code>String</code> | The contents of an access_token, refresh_token, or valid JSON Web Token |

```js
// Parsing the login token retrieved from the login function above
const parsedToken = parseJWTString(loginToken.access_token);
console.log("\r\nJWT parsing:");
console.log(parsedToken);
```

---

<a name="makeRequest"></a>

## **async** makeRequest(data, headers, asJSON) ⇒ <code>Object</code> \| <code>Error</code>

Thenable shorthand method for POST with dynamic body and header content

**Kind**: global function  
**Returns**: <code>Object</code> \| <code>Error</code> - Response from server

| Param   | Type                 | Default            | Description                                                                                                     |
| ------- | -------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------- |
| data    | <code>Object</code>  |                    | Key/value pairs to append to request body on POST with exception to "url" key/value which serves as destination |
| headers | <code>Array</code>   |                    | (optional) Array of Object with explicit "key" and "value" key/values to append as headers                      |
| asJSON  | <code>Boolean</code> | <code>false</code> | (optional) Whether to return as original or parsed JSON format                                                  |

```js
// Encrypting the parsedToken sub value, then sending it with proper credentials to Moore's server:
const serverResponse = await makeRequest({
  sub: encrypt(parsedToken.sub, secret), // "secret" is the getAWSSecrets passphrase
  url: "https://moorepass.com/get-token.php",
}).catch((err) => {
  console.error(err);
});
console.log("\r\nServer response:");
console.log(serverResponse);
```

---

<a name="requestFromServer"></a>

## **async** requestFromServer(url, requestOptions, asJSON) ⇒ <code>Object</code>

**Kind**: global function  
**Returns**: <code>Object</code> - Request event response

| Param          | Type                 | Default           | Description                                                                      |
| -------------- | -------------------- | ----------------- | -------------------------------------------------------------------------------- |
| url            | <code>String</code>  |                   | Destination of HTTP request                                                      |
| requestOptions | <code>Object</code>  |                   | Valid HTTP request options, such as method: "POST", body, headers, redirect, etc |
| asJSON         | <code>Boolean</code> | <code>true</code> | (optional) Whether to return as original or parsed JSON format                   |

```js
// A very basic POST request:
let requestOptions = {
  method: "POST",
  headers: myHeaders, // new Headers()
  body: urlencoded, // new URLSearchParams()
  redirect: "follow",
};
let response = await requestFromServer(url, requestOptions, asJSON);
```

---

<a name="isJSON"></a>

## isJSON(text) ⇒ <code>Boolean</code>

Shorthand to determine whether data is string or stringified JSON

**Kind**: global function  
**Returns**: <code>Boolean</code> - Whether data is valid JSON format and can be parsed

| Param | Type                | Description                                              |
| ----- | ------------------- | -------------------------------------------------------- |
| text  | <code>String</code> | Data to query and check if created from JSON.stringify() |

```js
let realData = isJSON(someVariable) ? JSON.parse(data) : data;
// realData is now reliably a parsed object rather than a potential string or object
```

---

<a name="getAWSSecrets"></a>

## **async** getAWSSecrets(config) ⇒ <code>Object</code> \| <code>Error</code>

Thenable method for querying AWS Key Manager contents, later to be used for all decryption and encryption.
By default this assumes the existence of environment variables containing the 4 required keys designated below,
but these can be passed as the parameter to the function in a destructured options/config format

**Kind**: global function  
**Returns**: <code>Object</code> \| <code>Error</code> - The server response if credentials were valid, or message outlining any errors

**Prerequisites**: Must include config option as first parameter or .ENV variables with of the keys: `region`, `secret_name`, `secret_access_id`, and `secret_access_key`.

| Param  | Type                | Description                                                                                                      |
| ------ | ------------------- | ---------------------------------------------------------------------------------------------------------------- |
| config | <code>Object</code> | (optional) Valid AWS credentials, including "region", "secret_name", "secret_access_id", and "secret_access_key" |

```js
// Query AWS SecretsManager to obtain a secure passphrase used later encryption/decryption.
let secret = await getAWSSecrets();
console.log("\r\n AWS Key Manager response:", secret);
secret = secret["token-login"];

// The below is redundant. If ENV vars already exist, they're inherited. Should only need to use object destructuring for config if these values would be coming from somewhere other than ENV variables (but no sense to expose them here, even in a private NPM package and Git repo)
let secretAlt = await getAWSSecrets({
  region: process.env.region,
  secret_name: process.env.secret_name,
  secret_access_id: process.env.secret_access_id,
  secret_access_key: process.env.secret_access_key,
});
```

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