Skip to content
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
16 changes: 14 additions & 2 deletions doc/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,22 @@ Handshake packets always have ID equal to `0`. The response contains either
the key `ok` with a value that is the session identifier or `error` that is
an array with error code and optional error message.

The action field of handshake requests specifies the authentication strategy.
There are two supported strategies now:

* `login` — authentication with login and password. The payload is an array of
two elements: username and password, both represented as strings.
* `anonymous` — anonymous session request. The payload is ignored (e.g., `true`
or an empty array may be used). For anonymous handshakes the action field can
be omitted completely, `anonymous` is implied by default.

More strategies may be added in the future (for example, `session` to reconnect
to an existing session after connection break due to a network error).

Successful handshake:

```javascript
C: {handshake:[0,'example'],marcus:'7b458e1a9dda....67cb7a3e'}
C: {handshake:[0,'example'],login:['marcus','7b458e1a9dda....67cb7a3e']}
S: {handshake:[0],ok:'9b71d224bd62...bcdec043'}
```

Expand All @@ -153,7 +165,7 @@ Successful handshake of [Impress](https://github.com/metarhia/Impress) worker
connecting to a private cloud controller:

```javascript
C: {handshake:[0,'impress'],S1N5:'d3ea3d73319b...5c2e5c3a'}
C: {handshake:[0,'impress'],login:['S1N5','d3ea3d73319b...5c2e5c3a']}
S: {handshake:[0],ok:'PrivateCloud'}
```

Expand Down
24 changes: 13 additions & 11 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ Connection.prototype.notifyStateChange = function(path, verb, value) {
// callback - callback function to invoke after the handshake is completed
//
Connection.prototype.handshake = function(appName, login, password, callback) {
const packet = this.createPacket('handshake', appName, login, password);
const packetId = packet.handshake[0];
const packet = login && password ?
this.createPacket('handshake', appName, 'login', [login, password]) :
this.createPacket('handshake', appName);

const packetId = packet.handshake[0];
this._callbacks[packetId] = (error, sessionId) => {
if (login && password && !error) {
this.username = login;
Expand Down Expand Up @@ -379,26 +381,26 @@ Connection.prototype._processHandshakeRequest = function(packet, keys) {

this.application = application;

const username = keys[1];
const password = packet[username];
let authStrategy = keys[1];
const credentials = authStrategy && packet[authStrategy];
authStrategy = authStrategy || 'anonymous';

this._emitPacketEvent('handshakeRequest', packet, packet.handshake[0], {
packetType: 'handshake',
handshakeRequest: true,
username,
password
authStrategy
});

const callback = this._onSessionCreated.bind(this, username);
this.server.startSession(this, application, username, password, callback);
this.server.startSession(this, application, authStrategy, credentials,
this._onSessionCreated.bind(this));
};

// Callback of authentication operation
// username - user login, if any
// error - error that has occured or null
// sessionId - session id or hash
// username - user login or null
// sessionId - session id
//
Connection.prototype._onSessionCreated = function(username, error, sessionId) {
Connection.prototype._onSessionCreated = function(error, username, sessionId) {
if (error) {
this._handshakeError(errors.ERR_AUTH_FAILED);
return;
Expand Down
17 changes: 10 additions & 7 deletions lib/simple-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ module.exports = simpleAuth;
// Start session. Only anonymous handshakes are allowed.
// connection - JSTP connection
// application - application instance
// username - null (required by the auth callback contract)
// password - null (required by the auth callback contract)
// callback - callback function that has signature (error, sessionId)
// strategy - authentication strategy (only 'anonymous' is supported)
// credentials - authentication credentials
// callback - callback function that has signature
// (error, username, sessionId)
//
simpleAuth.startSession = (connection, application,
username, password, callback) => {
if (username) {
simpleAuth.startSession = (
connection, application,
strategy, credentials, callback
) => {
if (strategy !== 'anonymous') {
return callback(errors.ERR_AUTH_FAILED);
}

const sessionId = generateUuid4();
simpleAuth.emit('session', sessionId, connection, application);

callback(null, sessionId);
callback(null, null, sessionId);
};

// Generate UUID v4
Expand Down
21 changes: 9 additions & 12 deletions test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ describe('JSTP Connection', () => {
constants.TEST_USERNAME, constants.TEST_PASSWORD, callback);

const handshakeRequest = {
handshake: [0, constants.TEST_APPLICATION]
handshake: [0, constants.TEST_APPLICATION],
login: [constants.TEST_USERNAME, constants.TEST_PASSWORD]
};

handshakeRequest[constants.TEST_USERNAME] = constants.TEST_PASSWORD;

expect(clientTransportMock.send)
.to.be.called.with(jstp.stringify(handshakeRequest));

Expand Down Expand Up @@ -193,10 +192,9 @@ describe('JSTP Connection', () => {

const packet = {
handshake: [0, constants.TEST_APPLICATION],
login: [constants.TEST_USERNAME, constants.TEST_PASSWORD]
};

packet[constants.TEST_USERNAME] = constants.TEST_PASSWORD;

serverTransportMock.emitPacket(packet);

expect(sendSpy).to.have.been.called.with(jstp.stringify({
Expand All @@ -205,8 +203,8 @@ describe('JSTP Connection', () => {
}));

expect(startSessionSpy).to.have.been.called.with(
serverConnection, applicationMock,
constants.TEST_USERNAME, constants.TEST_PASSWORD);
serverConnection, applicationMock, 'login',
[constants.TEST_USERNAME, constants.TEST_PASSWORD]);

sendSpy.reset();
startSessionSpy.reset();
Expand All @@ -217,14 +215,12 @@ describe('JSTP Connection', () => {
const startSessionSpy =
chai.spy.on(serverMock, 'startSession');

const password = 'illegal password';
const packet = {
handshake: [0, constants.TEST_APPLICATION],
login: [constants.TEST_USERNAME, password]
};

const password = 'illegal password';

packet[constants.TEST_USERNAME] = password;

serverTransportMock.emitPacket(packet);

expect(sendSpy).to.have.been.called.with(jstp.stringify({
Expand All @@ -233,7 +229,8 @@ describe('JSTP Connection', () => {
}));

expect(startSessionSpy).to.have.been.called.with(
serverConnection, applicationMock, constants.TEST_USERNAME, password);
serverConnection, applicationMock, 'login',
[constants.TEST_USERNAME, password]);

sendSpy.reset();
startSessionSpy.reset();
Expand Down
24 changes: 18 additions & 6 deletions test/unit/mock/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ ServerMock.prototype.getClients = function() {
ServerMock.prototype.broadcast = function() { };

ServerMock.prototype.startSession =
function(connection, application, username, password, callback) {
function(connection, application, strategy, credentials, callback) {
if (application.name !== constants.TEST_APPLICATION) {
return callback(new jstp.RemoteError(jstp.ERR_APP_NOT_FOUND));
}

if (username &&
(username !== constants.TEST_USERNAME ||
password !== constants.TEST_PASSWORD)) {
return callback(new jstp.RemoteError(jstp.ERR_AUTH_FAILED));
let username = null;
let success = false;

if (strategy === 'anonymous') {
success = true;
}

if (strategy === 'login' &&
credentials[0] === constants.TEST_USERNAME &&
credentials[1] === constants.TEST_PASSWORD) {
success = true;
username = constants.TEST_USERNAME;
}

callback(null, constants.TEST_SESSION_ID);
if (success) {
callback(null, username, constants.TEST_SESSION_ID);
} else {
callback(new jstp.RemoteError(jstp.ERR_AUTH_FAILED));
}
};