Skip to content

Commit

Permalink
Added ability to abort with details fixed minor issues with abort and…
Browse files Browse the repository at this point in the history
… client getting abort details
  • Loading branch information
mbonneau committed Jan 26, 2016
1 parent d199224 commit 442b2b8
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 8 deletions.
42 changes: 39 additions & 3 deletions src/Thruway/Authentication/AuthenticationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private function onHelloAuthHandler($authMethod, $authMethodInfo, Realm $realm,
// this is handling the return of the onhello RPC call

if (isset($res[0]) && $res[0] == "FAILURE") {
$session->abort(new \stdClass(), "thruway.error.authentication_failure");
$this->abortSessionUsingResponse($session, $res);

return;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ private function onHelloAuthHandler($authMethod, $authMethodInfo, Realm $realm,

$onHelloError = function () use ($session) {
Logger::error($this, "onhello rejected the promise");
$session->abort("thruway.error.unknown");
$session->abort(new \stdClass(), "thruway.error.unknown");
};

$onHelloAuthHandler = $authMethodInfo['handlers']->onhello;
Expand Down Expand Up @@ -408,14 +408,16 @@ private function onAuthenticateHandler($authMethod, $authMethodInfo, Realm $real
$session->setAuthenticated(true);
$session->sendMessage(new WelcomeMessage($session->getSessionId(), $welcomeDetails));

} elseif (isset($res[0]) && $res[0] == "FAILURE") {
$this->abortSessionUsingResponse($session, $res);
} else {
$session->abort(new \stdClass(), "thruway.error.authentication_failure");
}
};

$onAuthenticateError = function () use ($session) {
Logger::error($this, "onauthenticate rejected the promise");
$session->abort("thruway.error.unknown");
$session->abort(new \stdClass(), "thruway.error.unknown");
};

$extra = new \stdClass();
Expand Down Expand Up @@ -574,4 +576,38 @@ public function readyToAuthenticate()
return $this->getReady();
}

/**
* Send an abort message to the session if the Authenticator sent a FAILURE response
* Returns true if the abort was sent, false otherwise
*
* @param Session $session
* @param $response
* @return bool
* @throws \Exception
*/
private function abortSessionUsingResponse(Session $session, $response) {
// $response needs to be a failure
if (!isset($response[0]) || $response[0] !== 'FAILURE') {
return false;
}

if (!isset($response[1]) || !is_object($response[1])) {
// there are no other details to send - just fail it
$session->abort(new \stdClass(), "thruway.error.authentication_failure");
return true;
}

$details = new \stdClass();
if (isset($response[1]->details) && is_object($response[1]->details)) {
$details = $response[1]->details;
}

$abortUri = "thruway.error.authentication_failure";
if (isset($response[1]->abort_uri) && is_scalar($response[1]->abort_uri)) {
$abortUri = $response[1]->abort_uri;
}

$session->abort($details, $abortUri);
return true;
}
}
4 changes: 2 additions & 2 deletions src/Thruway/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private function handleOnClose()
*/
private function handleOnError()
{
$this->client->on('error', function ($reason) {
$this->emit('error', [$reason]);
$this->client->on('error', function () {
$this->emit('error', func_get_args());
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Thruway/Peer/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function processWelcome(ClientSession $session, WelcomeMessage $msg)
*/
public function processAbort(ClientSession $session, AbortMessage $msg)
{
$this->emit('error', [$msg->getResponseURI()]);
$this->emit('error', [$msg->getResponseURI(), $msg]);
$session->shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class AbortAfterAuthenticateWithDetailsAuthProviderClient extends \Thruway\Authentication\AbstractAuthProviderClient {
/**
* @return string
*/
public function getMethodName()
{
return 'abortafterauthenticatewithdetails';
}

public function preProcessAuthenticate(array $args) {
return [
"FAILURE",
[
"abort_uri" => "my.custom.abort.uri.2",
"details" => [
"message" => "My custom abort message 2"
]
]
];
}

}
24 changes: 24 additions & 0 deletions tests/Clients/AbortAfterHelloWithDetailsAuthProviderClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class AbortAfterHelloWithDetailsAuthProviderClient extends \Thruway\Authentication\AbstractAuthProviderClient {
/**
* @return string
*/
public function getMethodName()
{
return 'abortafterhellowithdetails';
}

public function processHello(array $args) {
return [
"FAILURE",
[
"abort_uri" => "my.custom.abort.uri",
"details" => [
"message" => "My custom abort message"
]
]
];
}

}
5 changes: 4 additions & 1 deletion tests/TestServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/Clients/InternalClient.php';
require_once __DIR__ . '/Clients/SimpleAuthProviderClient.php';
require_once __DIR__ . '/Clients/AbortAfterAuthenticateWithDetailsAuthProviderClient.php';
require_once __DIR__ . '/Clients/AbortAfterHelloAuthProviderClient.php';
require_once __DIR__ . '/Clients/AbortAfterHelloWithDetailsAuthProviderClient.php';
require_once __DIR__ . '/Clients/DisclosePublisherClient.php';
require_once __DIR__ . '/Clients/QueryParamAuthProviderClient.php';
require_once __DIR__ . '/UserDb.php';
Expand Down Expand Up @@ -49,7 +51,8 @@

// provide aborting auth provider
$router->addInternalClient(new AbortAfterHelloAuthProviderClient(["abortafterhello"]));

$router->addInternalClient(new AbortAfterHelloWithDetailsAuthProviderClient(["abortafterhellowithdetails"]));
$router->addInternalClient(new AbortAfterAuthenticateWithDetailsAuthProviderClient(["aaawd"]));

$router->addInternalClient(new QueryParamAuthProviderClient(["query_param_auth_realm"]));

Expand Down
78 changes: 77 additions & 1 deletion tests/WAMP/AbortingRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public function testAbortFollowingHello() {
"realm" => 'abortafterhello',
"url" => 'ws://127.0.0.1:8090',
"max_retries" => 0,
"authmethods" => ["abortafterhello"],
"onChallenge" => function () {}
)
);

Expand All @@ -30,6 +32,80 @@ function ($reason) {

$this->_conn->open();

$this->assertEquals("wamp.error.not_authorized", $this->_testResult);
$this->assertEquals("thruway.error.authentication_failure", $this->_testResult);
}

public function testAbortFollowingHelloWithDetails() {
$this->_testResult = null;
$this->_error = null;

$this->_conn = new \Thruway\Connection(
array(
"realm" => 'abortafterhellowithdetails',
"url" => 'ws://127.0.0.1:8090',
"max_retries" => 0,
"authmethods" => ["abortafterhellowithdetails"],
"onChallenge" => function () {}
)
);

$this->_conn->on(
'open',
function (\Thruway\ClientSession $session) {
$this->_testResult = "connection opened.";
$this->_conn->close();
}
);

$this->_conn->on(
'error',
function ($reason, \Thruway\Message\AbortMessage $msg = null) {
$this->assertInstanceOf('\Thruway\Message\AbortMessage', $msg);
$this->assertEquals("my.custom.abort.uri", $msg->getResponseURI());
$this->assertEquals("My custom abort message", $msg->getDetails()->message);
$this->_testResult = $reason;
}
);

$this->_conn->open();

$this->assertEquals("my.custom.abort.uri", $this->_testResult);
}

public function testAbortFollowingAuthenticateWithDetails() {
$this->_testResult = null;
$this->_error = null;

$this->_conn = new \Thruway\Connection(
array(
"realm" => 'aaawd',
"url" => 'ws://127.0.0.1:8090',
"max_retries" => 0,
"authmethods" => ["abortafterauthenticatewithdetails"],
"onChallenge" => function () { return 'asdf'; }
)
);

$this->_conn->on(
'open',
function (\Thruway\ClientSession $session) {
$this->_testResult = "connection opened.";
$this->_conn->close();
}
);

$this->_conn->on(
'error',
function ($reason, \Thruway\Message\AbortMessage $msg = null) {
$this->assertInstanceOf('\Thruway\Message\AbortMessage', $msg);
$this->assertEquals("my.custom.abort.uri.2", $msg->getResponseURI());
$this->assertEquals("My custom abort message 2", $msg->getDetails()->message);
$this->_testResult = $reason;
}
);

$this->_conn->open();

$this->assertEquals("my.custom.abort.uri.2", $this->_testResult);
}
}

0 comments on commit 442b2b8

Please sign in to comment.