-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from PerimeterX/dev
[ADD] multi app support. (#75)
- Loading branch information
Showing
6 changed files
with
140 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,17 @@ | ||
'use strict'; | ||
|
||
const MODULE_VERSION = 'NodeJS Module v5.1.0'; | ||
|
||
const PxExpressClient = require('./pxclient'); | ||
const PxEnforcer = require('perimeterx-node-core').PxEnforcer; | ||
const cookieParser = require('cookie-parser'); | ||
let enforcer; | ||
const PerimeterXEnforcer = require('./pxenforcer'); | ||
let enforcer = null; | ||
|
||
/** | ||
* PerimeterX (http://www.perimeterx.com) NodeJS-Express SDK | ||
* Version 1.0 Published 12 May 2016 | ||
*/ | ||
module.exports.init = initPXModule; | ||
module.exports.middleware = pxMiddleware; | ||
module.exports.enforcer = () => {return enforcer}; | ||
|
||
/** | ||
* initPXModule - Initialize PerimeterX middleware. | ||
* | ||
* @param {object} params - Configurations object to extend and overwrite the default settings. | ||
* | ||
*/ | ||
function initPXModule(params) { | ||
params.moduleVersion = MODULE_VERSION; | ||
let pxClient = new PxExpressClient(); | ||
enforcer = new PxEnforcer(params, pxClient); | ||
if (enforcer.config.conf.DYNAMIC_CONFIGURATIONS) { | ||
setInterval(enforcer.config.confManager.loadData.bind(enforcer.config.confManager), enforcer.config.conf.CONFIGURATION_LOAD_INTERVAL); | ||
} | ||
} | ||
module.exports.init = (params) => { | ||
const instance = new PerimeterXEnforcer(params); | ||
enforcer = instance.enforcer; | ||
module.exports.middleware = instance.middleware; | ||
}; | ||
module.exports.enforcer = () => enforcer; | ||
|
||
function parseCookies(req, res) { | ||
return new Promise((resolve, reject) => { | ||
if (!req.cookies) { | ||
cookieParser()(req, res, () => { | ||
resolve(); | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* pxMiddleware - middleware wrapper to score verification. | ||
* | ||
* @param {Object} req - HTTP Request. | ||
* @param {Object} res - HTTP Response. | ||
* @param {Function} next - callback function. | ||
*/ | ||
function pxMiddleware(req, res, next) { | ||
parseCookies(req, res).then(() => { | ||
enforcer.enforce(req, res, (err, response) => { | ||
if (!err && response) { //block | ||
res.status(response.status); | ||
if (response.header) { | ||
res.setHeader(response.header.key, response.header.value); | ||
} | ||
if (response.headers) { | ||
for (let header in response.headers) { | ||
res.setHeader(header, response.headers[header]); | ||
} | ||
} | ||
res.send(response.body); | ||
} else { //pass | ||
next(); | ||
} | ||
}); | ||
}); | ||
} | ||
module.exports.new = (params) => new PerimeterXEnforcer(params); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const MODULE_VERSION = 'NodeJS Module v6.0.0'; | ||
const PxExpressClient = require('./pxclient'); | ||
const PxEnforcer = require('perimeterx-node-core').PxEnforcer; | ||
const cookieParser = require('cookie-parser'); | ||
|
||
function parseCookies(req, res) { | ||
return new Promise((resolve, reject) => { | ||
if (!req.cookies) { | ||
cookieParser()(req, res, () => { | ||
resolve(); | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
|
||
class PerimeterXEnforcer { | ||
/** | ||
* @param {object} params - Configurations object to extend and overwrite the default settings | ||
*/ | ||
constructor(params) { | ||
params.moduleVersion = MODULE_VERSION; | ||
let pxClient = new PxExpressClient(); | ||
this.enforcer = new PxEnforcer(params, pxClient); | ||
|
||
if (this.enforcer.config.conf.DYNAMIC_CONFIGURATIONS) { | ||
setInterval( | ||
this.enforcer.config.confManager.loadData.bind(this.enforcer.config.confManager), | ||
this.enforcer.config.conf.CONFIGURATION_LOAD_INTERVAL | ||
); | ||
} | ||
|
||
this.middleware = this.newMiddleware(); | ||
} | ||
|
||
newMiddleware() { | ||
const enforcer = this.enforcer; | ||
/** | ||
* pxMiddleware - middleware wrapper to score verification. | ||
* | ||
* @param {Object} req - HTTP Request. | ||
* @param {Object} res - HTTP Response. | ||
* @param {Function} next - callback function. | ||
*/ | ||
function pxMiddleware(req, res, next) { | ||
parseCookies(req, res).then(() => { | ||
enforcer.enforce(req, res, (err, response) => { | ||
if (!err && response) { //block | ||
res.status(response.status); | ||
if (response.header) { | ||
res.setHeader(response.header.key, response.header.value); | ||
} | ||
|
||
if (response.headers) { | ||
for (let header in response.headers) { | ||
if (response.headers.hasOwnProperty(header)) { | ||
res.setHeader(header, response.headers[header]); | ||
} | ||
} | ||
} | ||
res.send(response.body); | ||
} else { //pass | ||
next(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
return pxMiddleware; | ||
} | ||
} | ||
|
||
module.exports = PerimeterXEnforcer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters