Add webhookKey support (#1920)

This commit is contained in:
Tyler Brock
2016-05-26 11:17:24 -07:00
parent 0850c184d3
commit 2561987d20
4 changed files with 70 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ import * as DatabaseAdapter from "../DatabaseAdapter";
import * as triggers from "../triggers";
import * as Parse from "parse/node";
import * as request from "request";
import { logger } from '../logger';
const DefaultHooksCollectionName = "_Hooks";
@@ -12,9 +13,10 @@ export class HooksController {
_collectionPrefix:string;
_collection;
constructor(applicationId:string, collectionPrefix:string = '') {
constructor(applicationId:string, collectionPrefix:string = '', webhookKey) {
this._applicationId = applicationId;
this._collectionPrefix = collectionPrefix;
this._webhookKey = webhookKey;
this.database = DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix).WithoutValidation();
}
@@ -79,7 +81,7 @@ export class HooksController {
}
addHookToTriggers(hook) {
var wrappedFunction = wrapToHTTPRequest(hook);
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
wrappedFunction.url = hook.url;
if (hook.className) {
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
@@ -153,7 +155,7 @@ export class HooksController {
};
}
function wrapToHTTPRequest(hook) {
function wrapToHTTPRequest(hook, key) {
return (req, res) => {
let jsonBody = {};
for (var i in req) {
@@ -174,6 +176,12 @@ function wrapToHTTPRequest(hook) {
body: JSON.stringify(jsonBody)
};
if (key) {
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
} else {
logger.warn('Making outgoing webhook request without webhookKey being set!');
}
request.post(hook.url, jsonRequest, function (err, httpResponse, body) {
var result;
if (body) {

View File

@@ -98,6 +98,7 @@ class ParseServer {
javascriptKey,
dotNetKey,
restAPIKey,
webhookKey,
fileKey = 'invalid-file-key',
facebookAppIds = [],
enableAnonymousUsers = true,
@@ -166,7 +167,7 @@ class ParseServer {
const filesController = new FilesController(filesControllerAdapter, appId);
const pushController = new PushController(pushControllerAdapter, appId);
const loggerController = new LoggerController(loggerControllerAdapter, appId);
const hooksController = new HooksController(appId, collectionPrefix);
const hooksController = new HooksController(appId, collectionPrefix, webhookKey);
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
const liveQueryController = new LiveQueryController(liveQuery);
const cacheController = new CacheController(cacheControllerAdapter, appId);
@@ -179,6 +180,7 @@ class ParseServer {
javascriptKey: javascriptKey,
dotNetKey: dotNetKey,
restAPIKey: restAPIKey,
webhookKey: webhookKey,
fileKey: fileKey,
facebookAppIds: facebookAppIds,
cacheController: cacheController,