From 3aa7789f5d744368164654079038ec4d1430560f Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Mon, 11 Jul 2022 19:16:36 +0300 Subject: [PATCH] Added support for optional "authextra" in hello packets (specified as 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). --- src/Connection.php | 10 +++++++++- src/Peer/Client.php | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index 98365f8..401e0db 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -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(); @@ -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) && count($token) >= 2) { + [$token, $extra] = $token; + } + $session->sendMessage(new AuthenticateMessage($token, $extra)); }); } } diff --git a/src/Peer/Client.php b/src/Peer/Client.php index 38c20fe..86537d9 100644 --- a/src/Peer/Client.php +++ b/src/Peer/Client.php @@ -124,6 +124,11 @@ class Client implements EventEmitterInterface, ClientInterface */ private $attemptRetry = true; + /** + * @var \stdClass|null + */ + private $authextra; + /** * Constructor @@ -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, @@ -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); @@ -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 *