Commit e76475c4 authored by Robert Knight's avatar Robert Knight

Request authenticated user ID after connecting WebSocket

Send a "whoami" request [1] after connecting to query the authenticated
user ID for the WS connection.

This makes it easy to check that authentication for the WS worked as
expected by inspecting the frames of the connection in the devtools.

[1] http://h.readthedocs.io/en/latest/realtime/#whoami
parent 039d8670
......@@ -182,6 +182,14 @@ function Streamer($rootScope, annotationMapper, annotationUI, auth,
messageType: 'client_id',
value: clientId,
});
// Send a "whoami" message. The server will respond with a "whoyouare"
// reply which is useful for verifying that authentication worked as
// expected.
setConfig('auth-check', {
type: 'whoami',
id: 1,
});
}).catch(function (err) {
console.error('Failed to fetch token for WebSocket authentication', err);
});
......
......@@ -157,12 +157,24 @@ describe('Streamer', function () {
assert.ok(activeStreamer.clientId);
});
it('should send the client ID on connection', function () {
it('should send the client ID after connecting', function () {
createDefaultStreamer();
return activeStreamer.connect().then(function () {
assert.equal(fakeWebSocket.messages.length, 1);
assert.equal(fakeWebSocket.messages[0].messageType, 'client_id');
assert.equal(fakeWebSocket.messages[0].value, activeStreamer.clientId);
var clientIdMsg = fakeWebSocket.messages.find(function (msg) {
return msg.messageType === 'client_id';
});
assert.ok(clientIdMsg);
assert.equal(clientIdMsg.value, activeStreamer.clientId);
});
});
it('should request the logged-in user ID after connecting', function () {
createDefaultStreamer();
return activeStreamer.connect().then(function () {
var whoamiMsg = fakeWebSocket.messages.find(function (msg) {
return msg.type === 'whoami';
});
assert.ok(whoamiMsg);
});
});
......@@ -409,8 +421,11 @@ describe('Streamer', function () {
return activeStreamer.connect().then(function () {
fakeWebSocket.messages = [];
fakeWebSocket.emit('open');
assert.equal(fakeWebSocket.messages.length, 1);
assert.equal(fakeWebSocket.messages[0].messageType, 'client_id');
var configMsgTypes = fakeWebSocket.messages.map(function (msg) {
return msg.type || msg.messageType;
});
assert.deepEqual(configMsgTypes, ['client_id', 'whoami']);
});
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment