Skip to content
Open
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
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
<directory>test/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
63 changes: 63 additions & 0 deletions src/Authentication/ClientJwtAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Thruway\Authentication;

use Thruway\Message\AuthenticateMessage;
use Thruway\Message\ChallengeMessage;

class ClientJwtAuthenticator implements ClientAuthenticationInterface
{

private $authId;
/**
* @var string
*/
private $jwt;

public function __construct($jwt, $authid)
{
$this->jwt = $jwt;
$this->authId = $authid;
}

/**
* Get AuthID
*
* @return mixed
*/
public function getAuthId()
{
return $this->authId;
}

/**
* Set AuthID
*
* @param mixed $authid
*/
public function setAuthId($authid)
{
$this->authId = $authid;
}

/**
* Get list supported authentication method
*
* @return array
*/
public function getAuthMethods()
{
return ['jwt'];
}

/**
* Get authentication message from challenge message
*
* @param ChallengeMessage $msg
* @return AuthenticateMessage
*/
public function getAuthenticateFromChallenge(ChallengeMessage $msg)
{
return new AuthenticateMessage($this->jwt);
}
}
21 changes: 21 additions & 0 deletions test/Peer/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Thruway\Peer;

use React\EventLoop\Factory;
use Thruway\Authentication\ClientJwtAuthenticator;
use Thruway\Authentication\ClientWampCraAuthenticator;
use Thruway\Transport\ClientTestTransportProvider;

class ClientTest extends \PHPUnit_Framework_TestCase
Expand All @@ -26,4 +28,23 @@ public function testClientSendsHelloMessage()

$client->addTransportProvider(new ClientTestTransportProvider());
}

public function testAddAuthenticators()
{
$client = new Client('some.realm', Factory::create());

$this->assertEmpty($client->getAuthMethods());

$client->addClientAuthenticator(new ClientJwtAuthenticator('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoaWQiOiJhZG1pbiIsImF1dGhyb2xlcyI6WyJhZG1pbiJdfQ.rgRR61pbyg-qimH1zdh_naLwHz3UOoYRaJA0JPuutC4',
'someId'));

$this->assertContains('jwt', $client->getAuthMethods());
$this->assertNotContains('wampcra', $client->getAuthMethods());

$client->addClientAuthenticator(new ClientWampCraAuthenticator('someId'));

$this->assertContains('jwt', $client->getAuthMethods());
$this->assertContains('wampcra', $client->getAuthMethods());
}

}