@apolitical/server 2.5.6-rc.1 → 2.6.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.6.0] - 2022-07-25
9
+ ### Updated
10
+ - `buildOptions` helper: use sessionSecret from argument if not in process.env
11
+
8
12
  ## [2.5.5] - 2022-07-19
9
13
  ### Added
10
14
  - Unit tests for `buildOptions` helper
package/README.md CHANGED
@@ -248,3 +248,20 @@ start({ appLoader, port: 3000, swaggerDocument });
248
248
  The `swaggerDocument` variable can be provided when starting the server to define the API documentation. The documentation can be found at [http://localhost:3000/docs](http://localhost:3000/docs).
249
249
 
250
250
  For usage examples, see [jwt.apolitical.spec.js](tests/../test/integration/jwt.apolitical.spec.js) test cases.
251
+
252
+ ### Options helper
253
+
254
+ The __Apolitical Server__ comes with an optsHelper function that can be used to generate an apolitical_auth header cookie when needed. This functionality is especially useful to generate an admin token when making request from one api to another.
255
+
256
+ ```js
257
+
258
+ // Instantiate the hellper with a session secret
259
+ const { buildOptions } = optsHelper({ sessionSecret: 'hello' });
260
+
261
+ // If generating an admin token: authToken must be truthy boolean
262
+ buildOptions({ headers: { authToken: true }})
263
+
264
+ // If using token passed in: authToken must be a string
265
+ buildOptions({ headers: { authToken: 'some-token' }})
266
+ ```
267
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.5.6-rc.1",
3
+ "version": "2.6.0-rc.1",
4
4
  "description": "Node.js module to encapsulate Apolitical's express server setup",
5
5
  "author": "Apolitical Group Limited <engineering@apolitical.co>",
6
6
  "license": "MIT",
package/src/config.js CHANGED
@@ -1,15 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const { NODE_ENV, LOG_LEVEL, SESSION_SECRET } = process.env;
3
+ const { NODE_ENV, LOG_LEVEL } = process.env;
4
4
 
5
5
  const NAME = 'apolitical-server';
6
- const VERSION = '2.5.0';
6
+ const VERSION = '2.6.0';
7
7
  const ADMIN_ROLE = 'administrator';
8
8
 
9
9
  module.exports = {
10
10
  NODE_ENV,
11
11
  LOG_LEVEL,
12
- SESSION_SECRET,
13
12
  LOGGER_OPTIONS: {
14
13
  logLevel: LOG_LEVEL,
15
14
  labels: {
@@ -1,51 +1,51 @@
1
1
  'use strict';
2
2
 
3
- module.exports = ({ jwtEncodeHelper, config }) => {
4
- const { SESSION_SECRET } = config;
5
- const {
6
- APOLITICAL: { ISSUER, ADMIN_ROLE, COOKIE_KEY },
7
- } = config.JWT;
3
+ module.exports =
4
+ ({ jwtEncodeHelper, config }) =>
5
+ ({ sessionSecret }) => {
6
+ const {
7
+ APOLITICAL: { ISSUER, ADMIN_ROLE, COOKIE_KEY },
8
+ } = config.JWT;
8
9
 
9
- function generateAdminToken(sessionSecret) {
10
- const secret = SESSION_SECRET || sessionSecret;
11
- const token = jwtEncodeHelper(secret, {
12
- role: ADMIN_ROLE,
13
- admin: true,
14
- iss: ISSUER,
15
- exp: Math.floor(Date.now() / 1000) + 90 * 24 * 3600,
16
- });
17
- return token;
18
- }
19
-
20
- function buildHeaders({ authToken }, sessionSecret) {
21
- const headers = {};
22
- if (authToken && typeof authToken === 'boolean') {
23
- // When authToken is a boolean then generate an admin token
24
- const adminToken = generateAdminToken(sessionSecret);
25
- Object.assign(headers, { Cookie: `${COOKIE_KEY}=${adminToken}` });
26
- } else if (authToken && typeof authToken === 'string') {
27
- // When authToken is a string then use the it as the token
28
- Object.assign(headers, { Cookie: `${COOKIE_KEY}=${authToken}` });
10
+ function generateAdminToken() {
11
+ const token = jwtEncodeHelper(sessionSecret, {
12
+ role: ADMIN_ROLE,
13
+ admin: true,
14
+ iss: ISSUER,
15
+ exp: Math.floor(Date.now() / 1000) + 90 * 24 * 3600,
16
+ });
17
+ return token;
29
18
  }
30
- return headers;
31
- }
32
19
 
33
- function buildOptions({ headers, params, validateStatus, data, sessionSecret }) {
34
- const opts = {};
35
- if (headers) {
36
- Object.assign(opts, { headers: buildHeaders(headers, sessionSecret) });
37
- }
38
- if (params) {
39
- Object.assign(opts, { params });
20
+ function buildHeaders({ authToken }) {
21
+ const headers = {};
22
+ if (authToken && typeof authToken === 'boolean') {
23
+ // When authToken is a boolean then generate an admin token
24
+ const adminToken = generateAdminToken();
25
+ Object.assign(headers, { Cookie: `${COOKIE_KEY}=${adminToken}` });
26
+ } else if (authToken && typeof authToken === 'string') {
27
+ // When authToken is a string then use the it as the token
28
+ Object.assign(headers, { Cookie: `${COOKIE_KEY}=${authToken}` });
29
+ }
30
+ return headers;
40
31
  }
41
- if (validateStatus) {
42
- Object.assign(opts, { validateStatus });
43
- }
44
- if (data) {
45
- Object.assign(opts, { data });
32
+
33
+ function buildOptions({ headers, params, validateStatus, data }) {
34
+ const opts = {};
35
+ if (headers) {
36
+ Object.assign(opts, { headers: buildHeaders(headers) });
37
+ }
38
+ if (params) {
39
+ Object.assign(opts, { params });
40
+ }
41
+ if (validateStatus) {
42
+ Object.assign(opts, { validateStatus });
43
+ }
44
+ if (data) {
45
+ Object.assign(opts, { data });
46
+ }
47
+ return opts;
46
48
  }
47
- return opts;
48
- }
49
49
 
50
- return buildOptions;
51
- };
50
+ return { buildOptions };
51
+ };
@@ -21,8 +21,6 @@ module.exports = ({
21
21
  authentication: authenticationMiddleware,
22
22
  authorisation: authorisationMiddleware,
23
23
  },
24
- request: {
25
- buildOptions: optsHelper,
26
- },
24
+ request: optsHelper,
27
25
  };
28
26
  };