Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide possibility to customize the bodyParser configuration #199

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
26 changes: 25 additions & 1 deletion dist/Provider/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var _devMode = /*#__PURE__*/new WeakMap();
var _ltiaas = /*#__PURE__*/new WeakMap();
var _tokenMaxAge = /*#__PURE__*/new WeakMap();
var _cookieOptions = /*#__PURE__*/new WeakMap();
var _bodyParserOptions = /*#__PURE__*/new WeakMap();
var _setup = /*#__PURE__*/new WeakMap();
var _connectCallback2 = /*#__PURE__*/new WeakMap();
var _deepLinkingCallback2 = /*#__PURE__*/new WeakMap();
Expand Down Expand Up @@ -97,6 +98,17 @@ class Provider {
signed: true
}
});
_classPrivateFieldInitSpec(this, _bodyParserOptions, {
writable: true,
value: {
json: {},
raw: {},
text: {},
urlencoded: {
extended: false
}
}
});
// Setup flag
_classPrivateFieldInitSpec(this, _setup, {
writable: true,
Expand Down Expand Up @@ -243,6 +255,10 @@ class Provider {
* @param {Array<String>} [options.dynReg.redirectUris] - Additional redirect URIs. (Ex: ['https://tool.example.com/launch'])
* @param {Object} [options.dynReg.customParameters] - Custom parameters object. (Ex: { key: 'value' })
* @param {Boolean} [options.dynReg.autoActivate = false] - Platform auto activation flag. If true, every Platform registered dynamically is immediately activated. Defaults to false.
* @param {Object} [options.bodyParserOpt.json = {}] - Parameters object to configure bodyParserOpt.json. (See documentation @ https://github.com/expressjs/body-parser#bodyparserjsonoptions)
* @param {Object} [options.bodyParserOpt.raw = {}] - Parameters object to configure bodyParserOpt.json. (See documentation @ https://github.com/expressjs/body-parser#bodyparserrawoptions)
* @param {Object} [options.bodyParserOpt.text = {}] - Parameters object to configure bodyParserOpt.json. (See documentation @ https://github.com/expressjs/body-parser#bodyparsertextoptions)
* @param {Object} [options.bodyParserOpt.urlencoded = { extended: false }] - Parameters object to configure bodyParserOpt.json. (See documentation @ https://github.com/expressjs/body-parser#bodyparserurlencodedoptions)
*/
setup(encryptionkey, database, options) {
if ((0, _classPrivateFieldGet2.default)(this, _setup)) throw new Error('PROVIDER_ALREADY_SETUP');
Expand Down Expand Up @@ -270,8 +286,16 @@ class Provider {
if (options.cookies.sameSite) (0, _classPrivateFieldGet2.default)(this, _cookieOptions).sameSite = options.cookies.sameSite;
if (options.cookies.domain) (0, _classPrivateFieldGet2.default)(this, _cookieOptions).domain = options.cookies.domain;
}

// BodyParser options
if (options && options.bodyParserOpt) {
if (options.bodyParserOpt.json) (0, _classPrivateFieldGet2.default)(this, _bodyParserOptions).json = options.bodyParserOpt.json;
if (options.bodyParserOpt.raw) (0, _classPrivateFieldGet2.default)(this, _bodyParserOptions).raw = options.bodyParserOpt.raw;
if (options.bodyParserOpt.text) (0, _classPrivateFieldGet2.default)(this, _bodyParserOptions).text = options.bodyParserOpt.text;
if (options.bodyParserOpt.urlencoded) (0, _classPrivateFieldGet2.default)(this, _bodyParserOptions).urlencoded = options.bodyParserOpt.urlencoded;
}
(0, _classPrivateFieldSet2.default)(this, _ENCRYPTIONKEY2, encryptionkey);
(0, _classPrivateFieldSet2.default)(this, _server, new Server(options ? options.https : false, options ? options.ssl : false, (0, _classPrivateFieldGet2.default)(this, _ENCRYPTIONKEY2), options ? options.cors : true, options ? options.serverAddon : false));
(0, _classPrivateFieldSet2.default)(this, _server, new Server(options ? options.https : false, options ? options.ssl : false, (0, _classPrivateFieldGet2.default)(this, _ENCRYPTIONKEY2), options ? options.cors : true, options ? options.serverAddon : false, options ? options.bodyParserOpt : (0, _classPrivateFieldGet2.default)(this, _bodyParserOptions)));

/**
* @description Express server object.
Expand Down
12 changes: 7 additions & 5 deletions dist/Utils/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cookieParser = require('cookie-parser');
const cors = require('cors');
const provAuthDebug = require('debug')('provider:auth');
class Server {
constructor(https, ssl, ENCRYPTIONKEY, corsOpt, serverAddon) {
constructor(https, ssl, ENCRYPTIONKEY, corsOpt, serverAddon, bodyParserOpt) {
this.app = express();
this.server = false;
this.ssl = false;
Expand Down Expand Up @@ -49,12 +49,14 @@ class Server {
}));
this.app.options('*', cors());
}
this.app.use(bodyParser.urlencoded({

// Ingest body parser options for each parsertype
this.app.use(bodyParser.urlencoded(bodyParserOpt && bodyParserOpt.urlencoded ? bodyParserOpt.urlencoded : {
extended: false
}));
this.app.use(bodyParser.json());
this.app.use(bodyParser.raw());
this.app.use(bodyParser.text());
this.app.use(bodyParser.json(bodyParserOpt && bodyParserOpt.json ? bodyParserOpt.json : {}));
this.app.use(bodyParser.raw(bodyParserOpt && bodyParserOpt.raw ? bodyParserOpt.raw : {}));
this.app.use(bodyParser.text(bodyParserOpt && bodyParserOpt.text ? bodyParserOpt.text : {}));
this.app.use(cookieParser(ENCRYPTIONKEY));
this.app.use(async (req, res, next) => {
// Creating Authorization schema LTIK-AUTH-V1
Expand Down
Loading