Skip to content

Commit

Permalink
Added support for optional "authextra" in hello packets (specified as…
Browse files Browse the repository at this point in the history
… an option to the constructor or via a setter);

Enabled onChallenge to optionally specify auth extra in the challenge response (modeled after autobahn's behavior).
  • Loading branch information
boenrobot committed Jul 11, 2022
1 parent 9547ecd commit 7c0a373
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function __construct(Array $options, LoopInterface $loop = null, Connecto
if (isset($options['authid'])) {
$this->client->setAuthId($options['authid']);
}
//Set Authextra
if (isset($options['authextra'])) {
$this->client->setAuthextra($options['authextra']);
}

//Register Handlers
$this->handleOnChallenge();
Expand Down Expand Up @@ -167,8 +171,12 @@ private function handleOnChallenge()
$this->client->setAuthMethods($options['authmethods']);

$this->client->on('challenge', function (ClientSession $session, ChallengeMessage $msg) use ($options) {
$extra = null;
$token = call_user_func($options['onChallenge'], $session, $msg->getAuthMethod(), $msg);
$session->sendMessage(new AuthenticateMessage($token));
if (is_array($token)) {
[$token, $extra] = $token;
}
$session->sendMessage(new AuthenticateMessage($token, $extra));
});
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/Peer/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class Client implements EventEmitterInterface, ClientInterface
*/
private $attemptRetry = true;

/**
* @var \stdClass|null
*/
private $authextra;


/**
* Constructor
Expand All @@ -143,6 +148,7 @@ public function __construct($realm, LoopInterface $loop = null)
$this->session = null;
$this->clientAuthenticators = [];
$this->authId = 'anonymous';
$this->authextra = null;

$this->reconnectOptions = [
'max_retries' => 15,
Expand Down Expand Up @@ -271,6 +277,9 @@ public function startSession(ClientSession $session)

$details->authmethods = $this->authMethods;
$details->authid = $this->authId;
if ($this->authextra !== null) {
$details->authextra = $this->authextra;
}

$session->setRealm($this->realm);

Expand Down Expand Up @@ -608,6 +617,22 @@ public function setAuthMethods($authMethods)
$this->authMethods = $authMethods;
}

/**
* @return \stdClass|null
*/
public function getAuthextra()
{
return $this->authextra;
}

/**
* @param \stdClass|null $authextra
*/
public function setAuthextra($authextra)
{
$this->authextra = $authextra === null ? null : (object)$authextra;
}

/**
* Get client session
*
Expand Down

0 comments on commit 7c0a373

Please sign in to comment.