@apolitical/server 2.5.2-rc.1 → 2.5.4-beta.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,14 @@ 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.5.4] - 2022-07-08
9
+ ### Added
10
+ - Generating apolitical-auth token function
11
+
12
+ ## [2.5.3] - 2022-06-29
13
+ ### Changed
14
+ - Pipelines ref
15
+
8
16
  ## [2.5.2] - 2022-06-08
9
17
  ### Added
10
18
  - Probes prefix to support custom path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.5.2-rc.1",
3
+ "version": "2.5.4-beta.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,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const { NODE_ENV, LOG_LEVEL } = process.env;
3
+ const { NODE_ENV, LOG_LEVEL, SESSION_SECRET } = process.env;
4
4
 
5
5
  const NAME = 'apolitical-server';
6
6
  const VERSION = '2.5.0';
@@ -9,6 +9,7 @@ const ADMIN_ROLE = 'administrator';
9
9
  module.exports = {
10
10
  NODE_ENV,
11
11
  LOG_LEVEL,
12
+ SESSION_SECRET,
12
13
  LOGGER_OPTIONS: {
13
14
  logLevel: LOG_LEVEL,
14
15
  labels: {
@@ -69,6 +70,8 @@ module.exports = {
69
70
  COOKIE_KEY: 'apolitical_auth',
70
71
  NAME: 'jwt',
71
72
  SESSION: { session: false },
73
+ ADMIN_ROLE,
74
+ ISSUER: NAME,
72
75
  },
73
76
  AUTH0: {
74
77
  CACHE: true,
package/src/container.js CHANGED
@@ -33,6 +33,7 @@ const jwtDecodeHelper = require('./helpers/jwt/decode.helper');
33
33
  const jwtEncodeHelper = require('./helpers/jwt/encode.helper');
34
34
  const jwtPassportHelper = require('./helpers/jwt/passport.helper');
35
35
  const loggerHelper = require('./helpers/logger.helper');
36
+ const optsHelper = require('./helpers/opts.helper');
36
37
  // Loaders
37
38
  const documentationLoader = require('./loaders/documentation.loader');
38
39
  const expressLoader = require('./loaders/express.loader');
@@ -87,6 +88,7 @@ container.register({
87
88
  jwtEncodeHelper: asFunction(jwtEncodeHelper).singleton(),
88
89
  jwtPassportHelper: asFunction(jwtPassportHelper).singleton(),
89
90
  loggerHelper: asFunction(loggerHelper).singleton(),
91
+ optsHelper: asFunction(optsHelper).singleton(),
90
92
  // Loaders
91
93
  documentationLoader: asFunction(documentationLoader).singleton(),
92
94
  expressLoader: asFunction(expressLoader).singleton(),
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ jwtEncodeHelper, config }) => {
4
+ const { SESSION_SECRET } = config.SERVER;
5
+ const {
6
+ APOLITICAL: { ISSUER, ADMIN_ROLE, COOKIE_KEY },
7
+ } = config.JWT;
8
+
9
+ function generateAdminToken() {
10
+ const token = jwtEncodeHelper(SESSION_SECRET, {
11
+ role: ADMIN_ROLE,
12
+ admin: true,
13
+ iss: ISSUER,
14
+ exp: Math.floor(Date.now() / 1000) + 90 * 24 * 3600,
15
+ });
16
+ return token;
17
+ }
18
+
19
+ function buildHeaders({ authToken }) {
20
+ const headers = {};
21
+ if (typeof authToken === 'boolean' && authToken) {
22
+ // When authToken is a boolean then generate an admin token
23
+ const adminToken = generateAdminToken();
24
+ Object.assign(headers, { Cookie: `${COOKIE_KEY}=${adminToken}` });
25
+ } else if (typeof authToken === 'string' && authToken) {
26
+ // When authToken is a string then use the it as the token
27
+ Object.assign(headers, { Cookie: `${COOKIE_KEY}=${authToken}` });
28
+ }
29
+ return headers;
30
+ }
31
+
32
+ function buildOptions({ headers, params, validateStatus, data }) {
33
+ const opts = {};
34
+ if (headers) {
35
+ Object.assign(opts, { headers: buildHeaders(headers) });
36
+ }
37
+ if (params) {
38
+ Object.assign(opts, { params });
39
+ }
40
+ if (validateStatus) {
41
+ Object.assign(opts, { validateStatus });
42
+ }
43
+ if (data) {
44
+ Object.assign(opts, { data });
45
+ }
46
+ return opts;
47
+ }
48
+
49
+ return buildOptions;
50
+ };
@@ -7,6 +7,7 @@ module.exports = ({
7
7
  healthService: { registerShutdown },
8
8
  jwtService,
9
9
  serverError,
10
+ optsHelper,
10
11
  }) => {
11
12
  // Register shutdown to clean up any resources used by the express app
12
13
  registerShutdown(shutdown);
@@ -20,5 +21,8 @@ module.exports = ({
20
21
  authentication: authenticationMiddleware,
21
22
  authorisation: authorisationMiddleware,
22
23
  },
24
+ request: {
25
+ buildOptions: optsHelper,
26
+ },
23
27
  };
24
28
  };