Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Router<
{
urlParams: req.params,
queryParams: req.query,
bodyParams: req.body,
bodyParams: (req as any).bodyParams || req.body,
request: req,
response: res,
} as any,
Expand Down
79 changes: 18 additions & 61 deletions apps/meteor/app/integrations/server/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,6 @@ function integrationInfoRest() {

class WebHookAPI extends APIClass {
async authenticatedRoute(request) {
const payloadKeys = Object.keys(request.body);
const payloadIsWrapped = request.body && request.body.payload && payloadKeys.length === 1;
if (payloadIsWrapped && request.headers['content-type'] === 'application/x-www-form-urlencoded') {
try {
request.body = JSON.parse(request.body.payload);
} catch ({ message }) {
return {
error: {
statusCode: 400,
body: {
success: false,
error: message,
},
},
};
}
}

request.integration = await Integrations.findOne({
_id: request.params.integrationId,
token: decodeURIComponent(request.params.token),
Expand Down Expand Up @@ -329,52 +311,27 @@ class WebHookAPI extends APIClass {
const Api = new WebHookAPI({
enableCors: true,
apiPath: 'hooks/',
auth: {
async user() {
const payloadKeys = Object.keys(this.bodyParams);
const payloadIsWrapped = this.bodyParams && this.bodyParams.payload && payloadKeys.length === 1;
if (payloadIsWrapped && this.request.headers['content-type'] === 'application/x-www-form-urlencoded') {
try {
this.bodyParams = JSON.parse(this.bodyParams.payload);
} catch ({ message }) {
return {
error: {
statusCode: 400,
body: {
success: false,
error: message,
},
},
};
}
}

this.request.integration = await Integrations.findOne({
_id: this.request.params.integrationId,
token: decodeURIComponent(this.request.params.token),
});

if (!this.request.integration) {
incomingLogger.info(`Invalid integration id ${this.request.params.integrationId} or token ${this.request.params.token}`);
});

return {
error: {
statusCode: 404,
body: {
success: false,
error: 'Invalid integration id or token provided.',
},
},
};
}
// middleware for special requests that are urlencoded but have a json payload (like GitHub webhooks)
Api.router.use((req, res, next) => {
if (req.headers['content-type'] !== 'application/x-www-form-urlencoded') {
return next();
}

const user = await Users.findOne({
_id: this.request.integration.userId,
});
const payloadKeys = Object.keys(req.body);
if (payloadKeys.length !== 1) {
return next();
}

return { user };
},
},
try {
// need to compose the full payload in this weird way because body-parser thought it was a form
req.bodyParams = JSON.parse(payloadKeys[0] + req.body[payloadKeys[0]]);
return next();
} catch (e) {
res.writeHead(400);
res.end(JSON.stringify({ success: false, error: e.message }));
}
});

Api.addRoute(
Expand Down
Loading