feat(api): add and update webhooks routing

This commit is contained in:
Mrugesh Mohapatra
2020-03-19 12:20:04 +05:30
committed by mrugesh
parent d8e5f3ebc8
commit 4ee032d664
4 changed files with 16 additions and 10 deletions

View File

@ -21,6 +21,7 @@ const log = debug('fcc:boot:donate');
export default function donateBoot(app, done) { export default function donateBoot(app, done) {
let stripe = false; let stripe = false;
const api = app.loopback.Router(); const api = app.loopback.Router();
const hooks = app.loopback.Router();
const donateRouter = app.loopback.Router(); const donateRouter = app.loopback.Router();
const subscriptionPlans = Object.keys( const subscriptionPlans = Object.keys(
@ -281,11 +282,11 @@ export default function donateBoot(app, done) {
.then(getAsyncPaypalToken) .then(getAsyncPaypalToken)
.then(token => verifyWebHook(headers, body, token, keys.paypal.webhookId)) .then(token => verifyWebHook(headers, body, token, keys.paypal.webhookId))
.then(hookBody => updateUser(hookBody, app)) .then(hookBody => updateUser(hookBody, app))
.then(() => res.status(200).json({ message: 'received hook' }))
.catch(err => { .catch(err => {
// Todo: This probably need to be thrown and caught in error handler
log(err.message); log(err.message);
return res.status(200).json({ message: 'received hook' }); })
}); .finally(() => res.status(200).json({ message: 'received paypal hook' }));
} }
const stripeKey = keys.stripe.public; const stripeKey = keys.stripe.public;
@ -316,8 +317,9 @@ export default function donateBoot(app, done) {
api.post('/charge-stripe', createStripeDonation); api.post('/charge-stripe', createStripeDonation);
api.post('/create-hmac-hash', createHmacHash); api.post('/create-hmac-hash', createHmacHash);
api.post('/add-donation', addDonation); api.post('/add-donation', addDonation);
api.post('/update-paypal', updatePaypal); hooks.post('/update-paypal', updatePaypal);
donateRouter.use('/donate', api); donateRouter.use('/donate', api);
donateRouter.use('/hooks', hooks);
app.use(donateRouter); app.use(donateRouter);
connectToStripe().then(done); connectToStripe().then(done);
} }

View File

@ -7,8 +7,8 @@ export default function() {
} }
}); });
return function csrf(req, res, next) { return function csrf(req, res, next) {
const path = req.path.split('/')[1]; const { path } = req;
if (/^donate\/update-paypal$/.test(path)) { if (/^\/hooks\/update-paypal$|^\/hooks\/update-stripe$/.test(path)) {
return next(); return next();
} }
return protection(req, res, next); return protection(req, res, next);

View File

@ -23,7 +23,7 @@ const signinRE = /^\/signin/;
const statusRE = /^\/status\/ping$/; const statusRE = /^\/status\/ping$/;
const unsubscribedRE = /^\/unsubscribed\//; const unsubscribedRE = /^\/unsubscribed\//;
const unsubscribeRE = /^\/u\/|^\/unsubscribe\/|^\/ue\//; const unsubscribeRE = /^\/u\/|^\/unsubscribe\/|^\/ue\//;
const updatePaypalRE = /^\/donate\/update-paypal/; const updateHooksRE = /^\/hooks\/update-paypal$|^\/hooks\/update-stripe$/;
const _whiteListREs = [ const _whiteListREs = [
authRE, authRE,
@ -37,7 +37,7 @@ const _whiteListREs = [
statusRE, statusRE,
unsubscribedRE, unsubscribedRE,
unsubscribeRE, unsubscribeRE,
updatePaypalRE updateHooksRE
]; ];
export function isWhiteListedPath(path, whiteListREs = _whiteListREs) { export function isWhiteListedPath(path, whiteListREs = _whiteListREs) {

View File

@ -40,7 +40,7 @@ describe('request-authorization', () => {
const statusRE = /^\/status\/ping$/; const statusRE = /^\/status\/ping$/;
const unsubscribedRE = /^\/unsubscribed\//; const unsubscribedRE = /^\/unsubscribed\//;
const unsubscribeRE = /^\/u\/|^\/unsubscribe\/|^\/ue\//; const unsubscribeRE = /^\/u\/|^\/unsubscribe\/|^\/ue\//;
const updatePaypalRE = /^\/donate\/update-paypal/; const updateHooksRE = /^\/hooks\/update-paypal$|^\/hooks\/update-stripe$/;
const whiteList = [ const whiteList = [
authRE, authRE,
@ -54,7 +54,7 @@ describe('request-authorization', () => {
statusRE, statusRE,
unsubscribedRE, unsubscribedRE,
unsubscribeRE, unsubscribeRE,
updatePaypalRE updateHooksRE
]; ];
it('returns a boolean', () => { it('returns a boolean', () => {
@ -68,8 +68,12 @@ describe('request-authorization', () => {
whiteList whiteList
); );
const resultB = isWhiteListedPath('/ue/WmjInLerysPrcon6fMb/', whiteList); const resultB = isWhiteListedPath('/ue/WmjInLerysPrcon6fMb/', whiteList);
const resultC = isWhiteListedPath('/hooks/update-paypal', whiteList);
const resultD = isWhiteListedPath('/hooks/update-stripe', whiteList);
expect(resultA).toBe(true); expect(resultA).toBe(true);
expect(resultB).toBe(true); expect(resultB).toBe(true);
expect(resultC).toBe(true);
expect(resultD).toBe(true);
}); });
it('returns false for a non-white-listed path', () => { it('returns false for a non-white-listed path', () => {