-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for supporting websocket and webhook
- Loading branch information
Ike The Coder
committed
Nov 6, 2019
1 parent
1035d4e
commit bddb9b6
Showing
12 changed files
with
260 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const passport = require('passport'); | ||
const passJwt = require('passport-jwt'); | ||
const passApiKey = require('passport-headerapikey'); | ||
const HeaderAPIKeyStrategy = passApiKey.HeaderAPIKeyStrategy; | ||
const config = require('config'); | ||
const logger = require('npmlog'); | ||
|
||
passport.use(new HeaderAPIKeyStrategy( | ||
{ header: 'Authorization', prefix: 'Api-Key ' }, | ||
false, | ||
function(apiKey, cb) { | ||
if (config.get("webhookSecret") == apiKey) { | ||
const user = { | ||
} | ||
return cb(null, user); | ||
} else { | ||
return cb(null, false); | ||
} | ||
} | ||
)); | ||
|
||
module.exports = passport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../../auth/webhook_auth'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var messages = {}; | ||
var logger = require('npmlog'); | ||
|
||
function checkRequestPermissions(user, requestId, fileId, sock, cb){ | ||
|
||
var db = require('../db/db'); | ||
|
||
logger.debug('Checking request permission', requestId, fileId); | ||
|
||
db.Request.getAll({_id: requestId}, 1, 1, user, function(findErr, findRes){ | ||
if (findErr || !findRes || findRes.length === 0){ | ||
logger.debug('Request not found.', requestId, findErr); | ||
cb(false, sock); | ||
return; | ||
} | ||
// Verify that the fileId is in the request files list | ||
if (!(fileId in findRes.files)) { | ||
logger.debug('File not found in request.', requestId); | ||
cb(false, sock); | ||
return; | ||
} | ||
cb(true, sock); | ||
}); | ||
} | ||
|
||
function sendFileStatusMessage(fileStatus){ | ||
var websockets = require('../../../websocket'); | ||
var conns = websockets.getConnections(); | ||
var keys = Object.keys(conns); | ||
for (var i=0; i<keys.length; i++){ | ||
// make sure the user has access to the request | ||
var conn = conns[keys[i]]; | ||
checkRequestPermissions(conn.user, conn.requestId, fileStatus.fileId, conn, function(send, sock) { | ||
if (send) { | ||
if (websockets.isOpen(sock)) { | ||
sock.send(JSON.stringify({fileStatus: fileStatus})); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
//wrapper to make async | ||
messages.sendFileStatusMessage = function(fileStatus){ | ||
setTimeout(sendFileStatusMessage, 0, fileStatus); | ||
}; | ||
|
||
module.exports = messages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
var messages = require('../messages/messages') | ||
|
||
router.get('/', function(req, res, next) { | ||
res.json({status:'ok'}); | ||
}); | ||
|
||
router.post('/fileStatus', function(req, res, next) { | ||
|
||
var json = res.body | ||
var status = { | ||
fileId: json.file_id, | ||
pass: (json.state === 0), | ||
state: json.state, | ||
message: json.message, | ||
name: json.rule_id, | ||
mandatory: json.mandatory | ||
} | ||
|
||
messages.sendFileStatusMessage (status); | ||
res.json({status:'ok'}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
const WebSocket = require('ws'); | ||
|
||
var config = require('config'); | ||
|
||
let token = config.get('testWebsocketJWT') | ||
let wsPort = config.get('wsPort') | ||
let requestId = "5dc30362c8a0710019533ab8" | ||
const ws = new WebSocket('ws://localhost:' + wsPort + '/' + requestId, null, { headers: { "sec-websocket-token": token }}); | ||
|
||
ws.on('open', function open() { | ||
console.log("Opened"); | ||
}); | ||
|
||
ws.on('close', function open() { | ||
console.log("Closed"); | ||
}); | ||
|
||
ws.on('message', function incoming(data) { | ||
console.log("Received Message"); | ||
console.log(data); | ||
}); | ||
|
||
setTimeout(() => ws.terminate(), 20000); |
Oops, something went wrong.