KnownUser.V3.Javascript
Before getting started please read the documentation to get acquainted with server-side connectors.
The connector was developed with TypeScript and verified using Nodejs v.8.12 and Express v.4.16.
You can find the latest released version here. or download latest npm package from here.
Implementation
The KnownUser validation must be done on all requests except requests for static and cached pages, resources like images, css files and ....
So, if you add the KnownUser validation logic to a central place, then be sure that the Triggers only fire on page requests (including ajax requests) and not on e.g. image.
The following is an example route in express/nodejs which shows how to validate that a user has been through the queue.
It assumes that your integration configuration file is located in root of the web application.
const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
const QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME = 'x-queueit-connector';
const QUEUEIT_CONNECTOR_NAME = "nodejs"
var express = require('express');
var router = express.Router();
var fs = require('fs');
var QueueITConnector = require('queueit-knownuser');
configureKnownUserHashing();
function isIgnored(req){
return req.method == 'HEAD' || req.method == 'OPTIONS'
}
router.get('/', function (req, res, next) {
try {
res.header(QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME, QUEUEIT_CONNECTOR_NAME);
if(isIgnored(req)){
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
return;
}
var integrationsConfigString = fs.readFileSync('integrationconfiguration.json', 'utf8');
var customerId = "";
var secretKey = "";
var httpContextProvider = initializeExpressHttpContextProvider(req, res);
var knownUser = QueueITConnector.KnownUser;
var queueitToken = req.query[knownUser.QueueITTokenKey];
var requestUrl = httpContextProvider.getHttpRequest().getAbsoluteUri();
var requestUrlWithoutToken = getRequestUrlWithoutToken(requestUrl);
var validationResult = knownUser.validateRequestByIntegrationConfig(
requestUrlWithoutToken, queueitToken, integrationsConfigString,
customerId, secretKey, httpContextProvider);
if (validationResult.doRedirect()) {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT'
});
if (validationResult.isAjaxResult) {
res.set(validationResult.getAjaxQueueRedirectHeaderKey(), validationResult.getAjaxRedirectUrl());
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
}
else {
res.redirect(validationResult.redirectUrl);
}
}
else {
if (requestUrl !== requestUrlWithoutToken && validationResult.actionType === "Queue") {
res.redirect(requestUrlWithoutToken);
}
else {
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
}
}
}
catch (e) {
console.log("ERROR:" + e);
res.header(QUEUEIT_FAILED_HEADERNAME, 'true');
}
});
function getRequestUrlWithoutToken(requestUrl){
try {
const url = new URL(requestUrl);
const params = new URLSearchParams(url.search);
params.delete(KnownUser.QueueITTokenKey);
url.search = params.toString();
return url.toString();
} catch (e) {
console.error('[Queue IT] Could not remove token in URL', e);
return requestUrl;
}
}
module.exports = router;
Code to initialize a httpContextProvider in Express (requires node module 'cookie-parser'):
function initializeExpressHttpContextProvider(req, res) {
return {
getHttpRequest: function () {
var httpRequest = {
getUserAgent: function () {
return this.getHeader("user-agent");
},
getHeader: function (headerName) {
var headerValue = req.header(headerName);
if (!headerValue)
return "";
return headerValue;
},
getAbsoluteUri: function () {
return req.protocol + '://' + req.get('host') + req.originalUrl;
},
getUserHostAddress: function () {
return req.ip;
},
getCookieValue: function (cookieKey) {
return req.cookies[cookieKey];
}
};
return httpRequest;
},
getHttpResponse: function () {
var httpResponse = {
setCookie: function (cookieName, cookieValue, domain, expiration, isCookieHttpOnly, isCookieSecure) {
if (domain === "")
domain = null;
const expirationDate = new Date(expiration * 1000);
res.cookie(
cookieName,
cookieValue,
{
expires: expirationDate,
path: "/",
domain: domain,
secure: isCookieSecure,
httpOnly: isCookieHttpOnly
});
}
};
return httpResponse;
}
};
}
Code to configure hashing in KnownUser SDK (requires node module 'crypto'):
function configureKnownUserHashing() {
var utils = QueueITConnector.Utils;
utils.generateSHA256Hash = function (secretKey, stringToHash) {
const crypto = require('crypto');
const hash = crypto.createHmac('sha256', secretKey)
.update(stringToHash)
.digest('hex');
return hash;
};
}
Implementation using inline queue configuration
Specify the configuration in code without using the Trigger/Action paradigm. In this case it is important only to queue-up page requests and not requests for resources or AJAX calls. This can be done by adding custom filtering logic before caling the knownUser.resolveQueueRequestByLocalConfig()
method.
The following is an example (using Express/Nodejs) of how to specify the configuration in code:
const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
const QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME = 'x-queueit-connector';
const QUEUEIT_CONNECTOR_NAME = "nodejs"
var express = require('express');
var router = express.Router();
var fs = require('fs');
var QueueITConnector = require('queueit-knownuser');
configureKnownUserHashing();
function isIgnored(req){
return req.method == 'HEAD' || req.method == 'OPTIONS'
}
router.get('/', function (req, res, next) {
try {
res.header(QUEUEIT_CONNECTOR_EXECUTED_HEADER_NAME, QUEUEIT_CONNECTOR_NAME);
if(isIgnored(req)){
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
return;
}
var integrationsConfigString = fs.readFileSync('integrationconfiguration.json', 'utf8');
var customerId = "";
var secretKey = "";
var queueConfig = new QueueITConnector.QueueEventConfig();
queueConfig.eventId = ""
queueConfig.queueDomain = "xxx.queue-it.net"
queueConfig.cookieValidityMinute = 15
queueConfig.extendCookieValidity = true
var httpContextProvider = initializeExpressHttpContextProvider(req, res);
var knownUser = QueueITConnector.KnownUser;
var queueitToken = req.query[knownUser.QueueITTokenKey];
var requestUrl = httpContextProvider.getHttpRequest().getAbsoluteUri();
var requestUrlWithoutToken = getRequestUrlWithoutToken(requestUrl);
var validationResult = knownUser.resolveQueueRequestByLocalConfig(
requestUrlWithoutToken, queueitToken, queueConfig,
customerId, secretKey, httpContextProvider);
if (validationResult.doRedirect()) {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT'
});
if (validationResult.isAjaxResult) {
res.set(validationResult.getAjaxQueueRedirectHeaderKey(), validationResult.getAjaxRedirectUrl());
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
}
else {
res.redirect(validationResult.redirectUrl);
}
}
else {
if (requestUrl !== requestUrlWithoutToken && validationResult.actionType === "Queue") {
res.redirect(requestUrlWithoutToken);
}
else {
res.render('index', {
node_version: process.version,
express_version: require('express/package').version
});
}
}
}
catch (e) {
console.log("ERROR:" + e);
res.header(QUEUEIT_FAILED_HEADERNAME, 'true');
}
});
function getRequestUrlWithoutToken(requestUrl){
try {
const url = new URL(requestUrl);
const params = new URLSearchParams(url.search);
params.delete(KnownUser.QueueITTokenKey);
url.search = params.toString();
return url.toString();
} catch (e) {
console.error('[Queue IT] Could not remove token in URL', e);
return requestUrl;
}
}
module.exports = router;