Skip to content

Commit

Permalink
Merge pull request #126 from Brewskey/dev
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
jlkalberer authored Feb 28, 2017
2 parents 8ddb333 + f758dec commit 1bb6dad
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 102 deletions.
5 changes: 0 additions & 5 deletions dist/controllers/WebhooksController.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
return desc;
}

var REQUEST_TYPES = ['DELETE', 'GET', 'POST', 'PUT'];

var validateWebhookMutator = function validateWebhookMutator(webhookMutator) {
if (!webhookMutator.event) {
return new _HttpError2.default('no event name provided');
Expand All @@ -101,9 +99,6 @@ var validateWebhookMutator = function validateWebhookMutator(webhookMutator) {
if (!webhookMutator.requestType) {
return new _HttpError2.default('no requestType provided');
}
if (!REQUEST_TYPES.includes(webhookMutator.requestType)) {
return new _HttpError2.default('wrong requestType');
}

return null;
};
Expand Down
40 changes: 20 additions & 20 deletions dist/lib/PasswordHasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ var _crypto2 = _interopRequireDefault(_crypto);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var HASH_DIGEST = 'sha1'; /**
* Copyright (C) 2013-2014 Spark Labs, Inc. All rights reserved. - https://www.spark.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can download the source here: https://github.com/spark/spark-server
*
*
*
*/
var HASH_DIGEST = 'sha256'; /**
* Copyright (C) 2013-2014 Spark Labs, Inc. All rights reserved. - https://www.spark.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can download the source here: https://github.com/spark/spark-server
*
*
*
*/

var HASH_ITERATIONS = 30000;
var KEY_LENGTH = 64;
Expand Down
31 changes: 22 additions & 9 deletions dist/managers/WebhookManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ var splitBufferIntoChunks = function splitBufferIntoChunks(buffer, chunkSize) {
return chunks;
};

var validateRequestType = function validateRequestType(requestType) {
var upperRequestType = requestType.toUpperCase();
if (!REQUEST_TYPES.includes(upperRequestType)) {
throw new _HttpError2.default('wrong requestType');
}

return upperRequestType;
};

var REQUEST_TYPES = ['DELETE', 'GET', 'POST', 'PUT'];
var MAX_WEBHOOK_ERRORS_COUNT = 10;
var WEBHOOK_THROTTLE_TIME = 1000 * 60; // 1min;
var MAX_RESPONSE_MESSAGE_CHUNK_SIZE = 512;
Expand Down Expand Up @@ -302,45 +312,48 @@ var WebhookManager = function WebhookManager(webhookRepository, eventPublisher)
case 0:
_context7.prev = 0;
return _context7.delegateYield(_regenerator2.default.mark(function _callee6() {
var webhookVariablesObject, requestJson, requestFormData, requestUrl, requestQuery, responseTopic, isJsonRequest, requestOptions, responseBody, isResponseBodyAnObject, responseTemplate, responseEventData, chunks;
var webhookVariablesObject, requestAuth, requestJson, requestFormData, requestHeaders, requestUrl, requestQuery, responseTopic, requestType, isJsonRequest, requestOptions, responseBody, isResponseBodyAnObject, responseTemplate, responseEventData, chunks;
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
webhookVariablesObject = _this._getEventVariables(event);
requestAuth = _this._compileJsonTemplate(webhook.auth, webhookVariablesObject);
requestJson = _this._compileJsonTemplate(webhook.json, webhookVariablesObject);
requestFormData = _this._compileJsonTemplate(webhook.form, webhookVariablesObject);
requestHeaders = _this._compileJsonTemplate(webhook.headers, webhookVariablesObject);
requestUrl = _this._compileTemplate(webhook.url, webhookVariablesObject);
requestQuery = _this._compileJsonTemplate(webhook.query, webhookVariablesObject);
responseTopic = _this._compileTemplate(webhook.responseTopic, webhookVariablesObject);
requestType = _this._compileTemplate(webhook.requestType, webhookVariablesObject);
isJsonRequest = !!requestJson;
requestOptions = {
auth: webhook.auth,
auth: requestAuth,
body: isJsonRequest ? _this._getRequestData(requestJson, event, webhook.noDefaults) : undefined,
form: !isJsonRequest ? _this._getRequestData(requestFormData, event, webhook.noDefaults) : undefined,
headers: webhook.headers,
headers: requestHeaders,
json: true,
method: webhook.requestType,
method: validateRequestType((0, _nullthrows2.default)(requestType)),
qs: requestQuery,
strictSSL: webhook.rejectUnauthorized,
url: (0, _nullthrows2.default)(requestUrl)
};
_context6.next = 10;
_context6.next = 13;
return _this._callWebhook(webhook, event, requestOptions);

case 10:
case 13:
responseBody = _context6.sent;

if (responseBody) {
_context6.next = 13;
_context6.next = 16;
break;
}

return _context6.abrupt('return', {
v: void 0
});

case 13:
case 16:
isResponseBodyAnObject = responseBody === Object(responseBody);
responseTemplate = webhook.responseTemplate && isResponseBodyAnObject && _hogan2.default.compile(webhook.responseTemplate).render(responseBody);
responseEventData = responseTemplate || (isResponseBodyAnObject ? (0, _stringify2.default)(responseBody) : responseBody);
Expand All @@ -358,7 +371,7 @@ var WebhookManager = function WebhookManager(webhookRepository, eventPublisher)
});
});

case 18:
case 21:
case 'end':
return _context6.stop();
}
Expand Down
12 changes: 1 addition & 11 deletions src/controllers/WebhooksController.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
// @flow

import type {
RequestType,
WebhookMutator,
} from '../types';
import type { WebhookMutator } from '../types';
import type WebhookManager from '../managers/WebhookManager';

import Controller from './Controller';
import HttpError from '../lib/HttpError';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';

const REQUEST_TYPES: Array<RequestType> = [
'DELETE', 'GET', 'POST', 'PUT',
];

const validateWebhookMutator = (webhookMutator: WebhookMutator): ?HttpError => {
if (!webhookMutator.event) {
return new HttpError('no event name provided');
Expand All @@ -25,9 +18,6 @@ const validateWebhookMutator = (webhookMutator: WebhookMutator): ?HttpError => {
if (!webhookMutator.requestType) {
return new HttpError('no requestType provided');
}
if (!REQUEST_TYPES.includes(webhookMutator.requestType)) {
return new HttpError('wrong requestType');
}

return null;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PasswordHasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import crypto from 'crypto';

const HASH_DIGEST = 'sha1';
const HASH_DIGEST = 'sha256';
const HASH_ITERATIONS = 30000;
const KEY_LENGTH = 64;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/deviceToAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type DeviceAPIType = {|
cellular: boolean,
connected: boolean,
current_build_target: string,
functions?: Array<string>,
functions?: ?Array<string>,
id: string,
imei?: string,
last_app: ?string,
Expand All @@ -18,7 +18,7 @@ export type DeviceAPIType = {|
product_id: number,
return_value?: mixed,
status: string,
variables?: Object,
variables?: ?Object,
|};

const deviceToAPI = (device: Device, result?: mixed): DeviceAPIType => ({
Expand Down
34 changes: 31 additions & 3 deletions src/managers/WebhookManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Event,
Repository,
RequestOptions,
RequestType,
Webhook,
WebhookMutator,
} from '../types';
Expand Down Expand Up @@ -40,6 +41,18 @@ const splitBufferIntoChunks = (
return chunks;
};

const validateRequestType = (requestType: string): RequestType => {
const upperRequestType = ((requestType.toUpperCase(): any): RequestType);
if (!REQUEST_TYPES.includes(upperRequestType)) {
throw new HttpError('wrong requestType');
}

return upperRequestType;
};

const REQUEST_TYPES: Array<RequestType> = [
'DELETE', 'GET', 'POST', 'PUT',
];
const MAX_WEBHOOK_ERRORS_COUNT = 10;
const WEBHOOK_THROTTLE_TIME = 1000 * 60; // 1min;
const MAX_RESPONSE_MESSAGE_CHUNK_SIZE = 512;
Expand Down Expand Up @@ -153,6 +166,11 @@ class WebhookManager {
const webhookVariablesObject =
this._getEventVariables(event);

const requestAuth = this._compileJsonTemplate(
webhook.auth,
webhookVariablesObject,
);

const requestJson = this._compileJsonTemplate(
webhook.json,
webhookVariablesObject,
Expand All @@ -163,6 +181,11 @@ class WebhookManager {
webhookVariablesObject,
);

const requestHeaders = this._compileJsonTemplate(
webhook.headers,
webhookVariablesObject,
);

const requestUrl = this._compileTemplate(
webhook.url,
webhookVariablesObject,
Expand All @@ -178,18 +201,23 @@ class WebhookManager {
webhookVariablesObject,
);

const requestType = this._compileTemplate(
webhook.requestType,
webhookVariablesObject,
);

const isJsonRequest = !!requestJson;
const requestOptions = {
auth: webhook.auth,
auth: (requestAuth: any),
body: isJsonRequest
? this._getRequestData(requestJson, event, webhook.noDefaults)
: undefined,
form: !isJsonRequest
? this._getRequestData(requestFormData, event, webhook.noDefaults)
: undefined,
headers: webhook.headers,
headers: requestHeaders,
json: true,
method: webhook.requestType,
method: validateRequestType(nullthrows(requestType)),
qs: requestQuery,
strictSSL: webhook.rejectUnauthorized,
url: nullthrows(requestUrl),
Expand Down
8 changes: 4 additions & 4 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Webhook = {
productIdOrSlug?: string,
query?: { [key: string]: Object },
rejectUnauthorized?: boolean,
requestType: RequestType,
requestType: string,
responseTemplate?: string,
responseTopic?: string,
url: string,
Expand All @@ -38,7 +38,7 @@ export type WebhookMutator = {
productIdOrSlug?: string,
query?: { [key: string]: Object },
rejectUnauthorized?: boolean,
requestType: RequestType,
requestType: string,
responseTemplate?: string,
responseTopic?: string,
url: string,
Expand Down Expand Up @@ -112,9 +112,9 @@ export type UserCredentials = {

export type Device = DeviceAttributes & {
connected: boolean,
functions?: Array<string>,
functions?: ?Array<string>,
lastFlashedAppName: ?string,
variables?: Object,
variables?: ?Object,
};

export type Repository<TModel> = {
Expand Down
Loading

0 comments on commit 1bb6dad

Please sign in to comment.