Skip to content

Commit

Permalink
Fix #194 terminate unauthenticate connections
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserf committed Jul 27, 2016
1 parent 739cac3 commit 3403ae0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exports.EVENT = {};
exports.EVENT.CONNECTION_ERROR = 'connectionError';
exports.EVENT.CONNECTION_STATE_CHANGED = 'connectionStateChanged';
exports.EVENT.MAX_RECONNECTION_ATTEMPTS_REACHED = 'MAX_RECONNECTION_ATTEMPTS_REACHED';
exports.EVENT.CONNECTION_AUTHENTICATION_TIMEOUT = 'CONNECTION_AUTHENTICATION_TIMEOUT';
exports.EVENT.ACK_TIMEOUT = 'ACK_TIMEOUT';
exports.EVENT.NO_RPC_PROVIDER = 'NO_RPC_PROVIDER';
exports.EVENT.RESPONSE_TIMEOUT = 'RESPONSE_TIMEOUT';
Expand Down
10 changes: 9 additions & 1 deletion src/message/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var Connection = function( client, url, options ) {
this._deliberateClose = false;
this._redirecting = false;
this._tooManyAuthAttempts = false;
this._connectionAuthenticationTimeout = false;
this._challengeDenied = false;
this._queuedMessages = [];
this._reconnectTimeout = null;
Expand Down Expand Up @@ -64,7 +65,7 @@ Connection.prototype.authenticate = function( authParams, callback ) {
this._authParams = authParams;
this._authCallback = callback;

if( this._tooManyAuthAttempts || this._challengeDenied ) {
if( this._tooManyAuthAttempts || this._challengeDenied || this._connectionAuthenticationTimeout ) {
this._client._$onError( C.TOPIC.ERROR, C.EVENT.IS_CLOSED, 'this client\'s connection was closed' );
return;
}
Expand Down Expand Up @@ -368,6 +369,13 @@ Connection.prototype._handleConnectionResponse = function( message ) {
this._redirecting = true;
this._endpoint.close();
}
else if( message.action === C.ACTIONS.ERROR ) {
if( message.data[ 0 ] === C.EVENT.CONNECTION_AUTHENTICATION_TIMEOUT ) {
this._deliberateClose = true;
this._connectionAuthenticationTimeout = true;
this._client._$onError( C.TOPIC.CONNECTION, message.data[ 0 ], message.data[ 1 ] );
}
}
};

/**
Expand Down
20 changes: 20 additions & 0 deletions test-e2e/specs/loginSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global describe, it, expect, jasmine */
var DeepstreamServer = require( 'deepstream.io' ),
C = require( '../../src/constants/constants' ),
deepstreamClient = require( '../../src/client' ),
TestLogger = require( '../tools/test-logger' );

Expand Down Expand Up @@ -32,10 +33,29 @@ describe( 'login', function() {
deepstreamServer.set( 'showLogo', false );
deepstreamServer.set( 'maxAuthAttempts', 2 );
deepstreamServer.set( 'authenticationHandler', authenticationHandler );
deepstreamServer.set( 'unauthenticatedClientTimeout', 200 );
deepstreamServer.start();
});

/**************** TESTS ****************/
it( 'does not login in the expected time', function( done ) {
clientA = deepstreamClient( 'localhost:6021' );
clientA.once( 'error', function( data, event, topic ){
expect( topic ).toBe( C.TOPIC.CONNECTION );
expect( event ).toBe( C.EVENT.CONNECTION_AUTHENTICATION_TIMEOUT );
done();
});
});

it( 'can\'t login after connection times out', function( done ) {
clientA.once( 'error', function( data, event, topic ){
expect( topic ).toBe( C.TOPIC.ERROR );
expect( event ).toBe( C.EVENT.IS_CLOSED );
done();
});
clientA.login();
});

it( 'tries to log in with an invalid user', function( done ) {
clientA = deepstreamClient( 'localhost:6021' );
clientA.login({ username: 'Egon'}, function( success, data ){
Expand Down

0 comments on commit 3403ae0

Please sign in to comment.