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

Check read access #22

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions lib/in-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function InMemory (opts) {
this.subscribers = opts.subscribers || {}
}

InMemory.prototype.subscribe = function (channel, uri, client, callback) {
InMemory.prototype.subscribe = function (channel, uri, client, authToken, dpopToken, callback) {
var self = this

if (!this.subscribers[channel]) {
Expand All @@ -19,7 +19,7 @@ InMemory.prototype.subscribe = function (channel, uri, client, callback) {
client.uuid = uuid.v1()
}

this.subscribers[channel][client.uuid] = [client, uri]
this.subscribers[channel][client.uuid] = [client, uri, authToken, dpopToken]

client.on('close', function () {
delete self.subscribers[channel][client.uuid]
Expand Down
51 changes: 35 additions & 16 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function WsServer (server, opts) {
opts = opts || {}
this.suffix = opts.suffix || '.changes'
this.store = opts.store || new InMemory(opts)
this.checkReadAccess = opts.checkReadAccess
var toChannel = opts.toChannel || defaultToChannel

// Starting WSS server
Expand All @@ -29,7 +30,8 @@ function WsServer (server, opts) {
wss.on('connection', function (client) {
debug('New connection')
// var location = url.parse(client.upgradeReq.url, true)

let authToken = null;
let dpopToken = null;
// Handling messages
client.on('message', function (message) {
debug('New message: ' + message)
Expand All @@ -42,20 +44,31 @@ function WsServer (server, opts) {
var command = tuple[0]
var iri = tuple[1]

// Only accept 'sub http://example.tld/hello'
if (tuple.length < 2 || command !== 'sub') {
// Only accept:
// * 'auth w34rffderdrggf...'
// * 'dpop w4gvsefw4resef4...'
// * 'sub http://example.tld/hello'
if (tuple.length < 2) {
return
}
if (command === 'sub') {
var channel = toChannel ? toChannel(iri) : iri
self.store.subscribe(channel, iri, client, authToken, dpopToken, function (err, uuid) {
if (err) {
// TODO Should return an error
return
}

client.send('ack ' + tuple[1])
})
} else if (command === 'auth') {
console.log('got auth!', tuple)
authToken = tuple[1]
} else if (command === 'dpop') {
console.log('got dpop!', tuple)
dpopToken = tuple[1]
}

var channel = toChannel ? toChannel(iri) : iri
self.store.subscribe(channel, iri, client, function (err, uuid) {
if (err) {
// TODO Should return an error
return
}

client.send('ack ' + tuple[1])
})
})

// Respond to ping
Expand All @@ -66,6 +79,7 @@ function WsServer (server, opts) {
}

WsServer.prototype.publish = function (iri, callback) {
var self = this
this.store.get(iri, function (err, subscribers) {

if (err) {
Expand All @@ -80,10 +94,15 @@ WsServer.prototype.publish = function (iri, callback) {
var tasks = Object.keys(subscribers)
.map(function (uuid) {
return function (cb) {
var client = subscribers[uuid][0]
var channel = subscribers[uuid][1]
debug('pub ' + channel + ' to ' + client.uuid)
client.send('pub ' + channel)
const [ client, channel, authToken, dpopToken ] = subscribers[uuid]
console.log('publish!', subscribers[uuid])

if (self.checkReadAccess(iri, authToken, dpopToken)) {
debug('pub ' + channel + ' to ' + client.uuid)
client.send('pub ' + channel)
} else {
debug('not pub ' + channel + ' to ' + client.uuid)
}
}
})

Expand Down
Loading