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

Fixes webhook verification with Micro when verifyToken is set in bot #236

Merged
merged 1 commit into from
Apr 13, 2018
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
19 changes: 18 additions & 1 deletion src/micro/__tests__/createServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import createServer from '../createServer';
jest.mock('urlencoded-body-parser');
jest.mock('../../connectNgrok');

function setup({ platform }) {
function setup({ platform, verifyToken }) {
const requestHandler = jest.fn();
const bot = {
createRequestHandler: () => requestHandler,
connector: {
platform,
verifyToken,
},
};
return {
Expand Down Expand Up @@ -48,6 +49,22 @@ it('should handle token verification', async () => {
expect(text).toBe('chatbot is awesome');
});

it('should handle token verification if verify token is passed in bot ', async () => {
const verifyToken = '1qaz2wsx';
const { bot } = setup({ platform: 'messenger', verifyToken });
const server = createServer(bot);
const { status, text } = await request(server)
.get('/')
.query({
'hub.mode': 'subscribe',
'hub.verify_token': verifyToken,
'hub.challenge': 'chatbot is awesome',
});

expect(status).toBe(200);
expect(text).toBe('chatbot is awesome');
});

it('should not handle token verification if platform is not messenger', async () => {
const { bot } = setup({ platform: 'line' });
const verifyToken = '1qaz2wsx';
Expand Down
6 changes: 5 additions & 1 deletion src/micro/createRequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { json, send } from 'micro';
import parseUrlencoded from 'urlencoded-body-parser';
import shortid from 'shortid';

import verifyLineSignature from './verifyLineSignature';
import verifyMessengerSignature from './verifyMessengerSignature';
Expand All @@ -12,7 +13,10 @@ function createRequestHandler(bot, config = {}) {
const requestHandler = bot.createRequestHandler();
return async (req, res) => {
if (req.method === 'GET' && bot.connector.platform === 'messenger') {
verifyMessengerWebhook({ verifyToken: config.verifyToken })(req, res);
verifyMessengerWebhook({
verifyToken:
config.verifyToken || bot.connector.verifyToken || shortid.generate(),
})(req, res);
} else if (req.method === 'POST') {
let body;
if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
Expand Down