Skip to content

Commit

Permalink
Preparing release 3.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
queue-it committed Feb 18, 2022
1 parent 3bec3d6 commit 11365d3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion akamaiContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Cookies } from 'cookies';
import { IConnectorContextProvider, ICryptoProvider, IEnqueueTokenProvider, IHttpRequest, IHttpResponse } from 'queueit-knownuser';
import { AkamaiCryptoProvider } from './akamaiCryptoProvider.js';
import { AkamaiEnqueueTokenProvider } from './akamaiEnqueueTokenProvider.js';
import { Settings } from './QueueITHelpers';
import { Settings } from './queueitHelpers.js';

export class AkamaiContextProvider implements IConnectorContextProvider {
private _httpRequest: IHttpRequest;
Expand Down
2 changes: 1 addition & 1 deletion akamaiEnqueueTokenProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IEnqueueTokenProvider } from "queueit-knownuser";
import { QueueITHelper, Settings } from "./QueueITHelpers.js";
import { QueueITHelper, Settings } from "./queueitHelpers.js";
import { Token, Payload } from "./sdk/queueToken.js";

export class AkamaiEnqueueTokenProvider implements IEnqueueTokenProvider {
Expand Down
2 changes: 1 addition & 1 deletion integrationConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IntegrationConfigProvider {
const options: any = {}
options.method = "GET";
options.headers = { "api-key": apiKey };
options.timeout = 950;
options.timeout = 1400;
return (await httpRequest("/queueit/integrationconfig/", options)).text();
}
private static getIntegrationConfigFromEdgeKV = async function () {
Expand Down
53 changes: 22 additions & 31 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

import { AkamaiContextProvider } from './akamaiContextProvider.js';
import { KnownUser } from './sdk/queueit-knownuserv3-sdk.js';
import { QueueITHelper, SettingException, Settings } from './QueueITHelpers.js';
import { QueueITHelper, SettingException, Settings } from './queueitHelpers.js';
import { IntegrationConfigProvider } from './integrationConfigProvider.js';

// @ts-ignore
import { logger } from 'log';

const COOKIE_VARIABLE_NAME = 'PMUSER_QUEUEIT_C';
const ERROR_VARIABLE_NAME = 'PMUSER_QUEUEIT_ER';
const EXECUTED_VARIABLE_NAME = 'PMUSER_QUEUEIT_EXECUTED';
const BIG_SET_COOKIE_VALUE = 'TOO_BIG_COOKIE';
const QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME = 'x-queueit-connector';
const QUEUEIT_FAILED_HEADERNAME = 'x-queueit-failed';
const QUEUEIT_CONNECTOR_NAME = "akamai";
const SHOULD_IGNORE_OPTIONS_REQUESTS = false;
const SHOULD_INCLUDE_ENQUEUETOKEN = false;
const EXECUTED_VARIABLE_NAME = 'PMUSER_QUEUEIT_EXECUTED';

export async function onClientRequest(request) {
try {
// Set PMUSER variable to allow validation that EdgeWorker was executed
request.setVariable(EXECUTED_VARIABLE_NAME, 'true');

if (isIgnored(request)) {
const settings = QueueITHelper.getSettingsFromPMVariables(request);
if (isIgnored(request, settings)) {
return;
}

let httpContext = new AkamaiContextProvider(request, {
let contextProvider = new AkamaiContextProvider(request, {
storeCookie: function (cookieString) {
if (cookieString.length < 800) {
request.setVariable(COOKIE_VARIABLE_NAME, cookieString);
Expand All @@ -36,17 +35,16 @@ export async function onClientRequest(request) {
}
}
});
const settings = QueueITHelper.getSettingsFromPMVariables(request);

if (includeEnqueueToken()) {
httpContext.setEnqueueTokenProvider(
if (settings.GenerateEnqueueToken) {
contextProvider.setEnqueueTokenProvider(
settings,
60000,
httpContext.getHttpRequest().getUserHostAddress()
contextProvider.getHttpRequest().getUserHostAddress()
);
}

let { queueitToken, requestUrlWithoutToken, validationResult } = await validateRequest(httpContext, settings);
let { queueitToken, requestUrlWithoutToken, validationResult } = await validateRequest(contextProvider, settings);

if (validationResult.doRedirect()) {
// Adding no cache headers to prevent browsers to cache requests
Expand Down Expand Up @@ -98,22 +96,22 @@ export async function onClientResponse(request, response) {
try {
response.addHeader(QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME, QUEUEIT_CONNECTOR_NAME);

if (isIgnored(request)) {
let settings = QueueITHelper.getSettingsFromPMVariables(request);
if (isIgnored(request, settings)) {
return;
}

let cookieString = request.getVariable(COOKIE_VARIABLE_NAME);
if (cookieString && cookieString !== BIG_SET_COOKIE_VALUE) {
response.addHeader('Set-Cookie', cookieString);
} else if (cookieString == BIG_SET_COOKIE_VALUE) {
let httpContext = new AkamaiContextProvider(request, {
let contextProvider = new AkamaiContextProvider(request, {
storeCookie: function (cookieString) {
response.addHeader('Set-Cookie', cookieString);
}
});
//cookie value was big so re-evaluating to be able to set the cookie on the response
let settings = QueueITHelper.getSettingsFromPMVariables(request);
await validateRequest(httpContext, settings);
await validateRequest(contextProvider, settings);
}

let error = request.getVariable(ERROR_VARIABLE_NAME);
Expand All @@ -138,27 +136,23 @@ export async function onClientResponse(request, response) {
}
}

function isIgnored(request) {
return SHOULD_IGNORE_OPTIONS_REQUESTS && request.method === 'OPTIONS';
}

function includeEnqueueToken() {
return SHOULD_INCLUDE_ENQUEUETOKEN;
function isIgnored(request, settings: Settings) {
return request.method === 'OPTIONS' && settings.IgnoreOptionsRequests;
}

async function getQueueItToken(httpContext, requestUrl: string) {
async function getQueueItToken(contextProvider: AkamaiContextProvider, requestUrl: string) {
let queueitToken = QueueITHelper.getParameterByName(KnownUser.QueueITTokenKey, requestUrl);
if (queueitToken) {
return queueitToken;
}

const tokenHeaderName = `x-${KnownUser.QueueITTokenKey}`;
return httpContext.getHttpRequest().getHeader(tokenHeaderName);
return contextProvider.getHttpRequest().getHeader(tokenHeaderName);
}

async function validateRequest(httpContext, settings: Settings) {
const requestUrl = httpContext.getHttpRequest().getAbsoluteUri();
const queueitToken = await getQueueItToken(httpContext, requestUrl);
async function validateRequest(contextProvider: AkamaiContextProvider, settings: Settings) {
const requestUrl = contextProvider.getHttpRequest().getAbsoluteUri();
const queueitToken = await getQueueItToken(contextProvider, requestUrl);
let requestUrlWithoutToken = requestUrl.replace(new RegExp("([\?&])(" + KnownUser.QueueITTokenKey + "=[^&]*)", 'i'), "");
requestUrlWithoutToken = requestUrlWithoutToken.replace(new RegExp("[?]$"), "");

Expand All @@ -168,10 +162,7 @@ async function validateRequest(httpContext, settings: Settings) {

const validationResult = KnownUser.validateRequestByIntegrationConfig(
requestUrl, queueitToken, integrationConfig,
settings.CustomerId, settings.SecretKey, httpContext);
settings.CustomerId, settings.SecretKey, contextProvider);

return { queueitToken, requestUrlWithoutToken, validationResult };
}



}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"repository": "https://github.com/queueit/KnownUser.V3.Akamai",
"license": "MIT",
"main": "./dist/index.js",
"version": "3.0.5",
"version": "3.0.6",
"type": "module",
"scripts": {},
"dependencies": {
Expand Down
14 changes: 11 additions & 3 deletions queueitHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

const AKAMAI_SDK_VERSION = "3.0.5";
const AKAMAI_SDK_VERSION = "3.0.6";

export class QueueITHelper {

Expand Down Expand Up @@ -29,7 +29,9 @@ export class QueueITHelper {
SecretKey: request.getVariable('PMUSER_QUEUEIT_SECRET_KEY'),
ApiKey: request.getVariable('PMUSER_QUEUEIT_API_KEY'),
CustomerId: request.getVariable('PMUSER_QUEUEIT_CUSTOMERID'),
IntegrationConfigType: request.getVariable('PMUSER_QUEUEIT_CONFIG_TYPE')
IntegrationConfigType: request.getVariable('PMUSER_QUEUEIT_CONFIG_TYPE'),
IgnoreOptionsRequests: request.getVariable('PMUSER_QUEUEIT_IGNORE_OPTIONS_REQUESTS'),
GenerateEnqueueToken: request.getVariable('PMUSER_QUEUEIT_GENERATE_EQTOKEN')
};

if (!setting.CustomerId) {
Expand All @@ -53,6 +55,10 @@ export class QueueITHelper {
throw settingException;
}

// convert to boolean
setting.IgnoreOptionsRequests = setting.IgnoreOptionsRequests && setting.IgnoreOptionsRequests.toLowerCase() == 'true';
setting.GenerateEnqueueToken = setting.GenerateEnqueueToken && setting.GenerateEnqueueToken.toLowerCase() == 'true';

return setting;
}

Expand All @@ -75,7 +81,9 @@ export interface Settings {
SecretKey: string,
ApiKey: string,
CustomerId: string,
IntegrationConfigType: string
IntegrationConfigType: string,
IgnoreOptionsRequests: boolean,
GenerateEnqueueToken: boolean
}

export class SettingException {
Expand Down

0 comments on commit 11365d3

Please sign in to comment.