Traffic Allow list of users

This example implements an allow list depending on the geographic locale of the end user. If user is arriving from United States embargoed countries, a 403 deny will occur.

Link to GitHub

Outcome : If user is arriving from United States embargoed countries, a 403 deny will occur else Hello from Edge Workers

Below is the code snippet in main.js


/*
(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license.
Version: 0.1
Purpose:  Respond with allow or deny message depending on country of end user.
Repo: https://github.com/akamai/edgeworkers-examples/tree/master/traffic-allow-list
*/

// List of currently US embargoed countries, plus N/A indicating no country data found (rare, if any)
const embargoedCountries = ['IR', 'KP', 'SY', 'SD', 'CU', 'VE', 'N/A'];

export function onClientRequest (request) {
  // Collect the end user's country based on Akamai EdgeScape data
  const country = (request.userLocation.country) ? request.userLocation.country : 'N/A';

  // Check if end user's country is in embargo list
  const embargoed = embargoedCountries.includes(country);

  // Provide appropriate messaging based on embargo status
  if (!embargoed) {
    request.respondWith(200, { 'Content-Type': ['text/html;charset=utf-8'] }, '

Hello ' + country + ' from Akamai EdgeWorkers!

'); } else { request.respondWith(403, { 'Content-Type': ['text/html;charset=utf-8'] }, '

Sorry, users from ' + country + ' may not view this content

', 'EW-embargo'); } }