Installation

Prerequisites

The HUMAN Edgio Enforcer uses Node.js and Edgio’s CDN as Code. Ensure you’ve satisfied the Edgio prerequisites.

The Enforcer is supported for Edgio Core v6.x and Node.js 16.

Installation

  1. Create an Edgio property (if you haven’t already) by following the instructions here.

  2. Install the HUMAN Enforcer into your Edgio project.

$npm i --save @humansecurity/edgio-enforcer
>
>yarn add @humansecurity/edgio-enforcer
  1. Integrate the HUMAN Enforcer into your routes.js file.

For an out-of-the box Router with the HUMAN Enforcer integrated into it already, simply import and use the createDefaultEnforcedRouter function.

1import { createDefaultEnforcedRouter } from '@humansecurity/edgio-enforcer';
2
3
4// define human security configuration
5
6const params = {
7
8px_app_id: '<APP_ID>',
9
10px_auth_token: process.env.PX_AUTH_TOKEN,
11
12px_cookie_secret: process.env.PX_COOKIE_SECRET,
13
14};
15
16
17// create and export default enforced router with origin name and configuration parameters
18
19export default createDefaultEnforcedRouter('origin', params);

The example below shows what the createDefaultEnforcedRouter() function does behind the scenes. For a more customized solution (e.g., multiple route patterns, multiple origins), create a new HumanSecurity instance and use it on your Router’s match functions as needed.

1import { Router } from '@edgio/core';
2
3import { HumanSecurity } from '@humansecurity/edgio-enforcer';
4
5
6const ORIGIN_NAME = 'origin';
7
8
9// define human security configuration
10
11const params = {
12
13px_app_id: '<APP_ID>',
14
15px_auth_token: process.env.PX_AUTH_TOKEN,
16
17px_cookie_secret: process.env.PX_COOKIE_SECRET,
18
19};
20
21
22// create new instance of HumanSecurity with configuration parameters
23
24const human = new HumanSecurity(params);
25
26
27export default new Router()
28
29// Match first party paths before handling static content
30
31.match(...human.matchFirstParty())
32
33
34// Avoid calling compute for static content
35
36.match(...human.matchFilteredExtensions(ORIGIN_NAME))
37
38
39// HUMAN will enforce all routes that weren't handled prior
40
41.match('/:path*', ({ compute, proxy, cache }) => {
42
43// Disable the cache
44
45cache({ edge: false, browser: false });
46
47
48// Call Edgio compute
49
50compute(async (request, response) => {
51
52// create new enforcer inside compute function
53
54const enforcer = human.createEnforcer();
55
56
57// call enforce and await the response
58
59const res = await enforcer.enforce(request, response);
60
61
62// return if the response exists
63
64if (res) return;
65
66
67// proxy to the origin
68
69return await proxy(ORIGIN_NAME, {
70
71// call to postEnforce when response is received
72
73transformResponse: (response) => enforcer.postEnforce(response),
74
75});
76
77});
78
79})
Matching static content must be done before match calls that invoke `compute` to ensure caching.
  1. Add any environment variables needed and deploy to your desired Edgio environment.
Bash
$edgio deploy