-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic anonymous authentication
- Loading branch information
Showing
9 changed files
with
293 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Auth; | ||
|
||
use Octamp\Wamp\Connection\WithEventDispatcherInterface; | ||
use Octamp\Wamp\Helper\IDHelper; | ||
use Octamp\Wamp\Promise\Deferred; | ||
use Octamp\Wamp\Promise\PromiseInterface; | ||
use Octamp\Wamp\Realm\RealmManager; | ||
use Octamp\Wamp\Session\Event\MessageEvent; | ||
use Thruway\Message\CallMessage; | ||
use Thruway\Message\HelloMessage; | ||
use Thruway\Message\Message; | ||
use Thruway\Message\ResultMessage; | ||
|
||
abstract class AbstractDynamicAuthenticator extends AbstractAuthenticator implements WithRealmManagerInterface | ||
{ | ||
protected ?RealmManager $realmManager; | ||
|
||
protected function sendMessageToAuthenticator(HelloMessage $message, array $details = []): PromiseInterface | ||
{ | ||
$helloDetails = $message->getDetails(); | ||
$args = [ | ||
'authmethod' => $this->getMethod(), | ||
]; | ||
if ($helloDetails->authid) { | ||
$args['authid'] = $helloDetails->authid; | ||
} | ||
if (isset($helloDetails->authextra)) { | ||
$args['authextra'] = $helloDetails->authextra; | ||
} | ||
|
||
$procedureName = $this->config['authenticator']; | ||
$realm = $this->realmManager->getRealm($this->config['authenticator-realm']); | ||
$realmSession = $realm->getMetaSession(); | ||
$requestId = IDHelper::incrementSessionWampID($realmSession); | ||
|
||
$callMessage = new CallMessage($requestId, [], $procedureName, [ | ||
$realm->name, | ||
$args['authid'] ?? null, | ||
array_merge($args, $details), | ||
]); | ||
|
||
$deferred = new Deferred(); | ||
|
||
$connection = $realmSession->getTransport()->getConnection(); | ||
if ($connection instanceof WithEventDispatcherInterface) { | ||
$connection->once('Message:' . Message::MSG_CALL . ':' . $callMessage->getRequestId(), function (MessageEvent $event): void { | ||
$this->realmManager->dispatch($event->session, $event->message); | ||
}); | ||
$connection->once( | ||
'Message:' . Message::MSG_RESULT . ':' . $callMessage->getRequestId(), | ||
function (MessageEvent $event) use ($connection, $callMessage, $deferred): void { | ||
$connection->removeListenersForEvent('Message:' . Message::MSG_ERROR . ':' .$callMessage->getRequestId()); | ||
/** @var ResultMessage $message */ | ||
$message = $event->message; | ||
$data = $message->getArguments(); | ||
if (empty($data)) { | ||
$deferred->reject([]); | ||
return; | ||
} | ||
|
||
$result = $data[0]; | ||
$success = $result['status'] ?? true; | ||
|
||
if (!$success) { | ||
$deferred->reject($result); | ||
} else { | ||
$deferred->resolve($result); | ||
} | ||
} | ||
); | ||
$connection->once( | ||
'Message:' . Message::MSG_ERROR . ':' . $callMessage->getRequestId(), | ||
function (MessageEvent $event) use ($connection, $callMessage, $deferred): void { | ||
$connection->removeListenersForEvent('Message:' . Message::MSG_RESULT . ':' .$callMessage->getRequestId()); | ||
/** @var ErrorMessage $message */ | ||
$message = $event->message; | ||
|
||
$deferred->resolve([ | ||
'error_uri' => $message->getErrorURI(), | ||
'error_details' => $message->getDetails(), | ||
]); | ||
} | ||
); | ||
} | ||
|
||
$realmSession->sendMessage($callMessage); | ||
|
||
return $deferred->promise(); | ||
} | ||
|
||
public function setRealmManager(RealmManager $realmManager): void | ||
{ | ||
$this->realmManager = $realmManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Auth; | ||
|
||
use Octamp\Wamp\Promise\Promise; | ||
use Octamp\Wamp\Promise\PromiseInterface; | ||
use Octamp\Wamp\Realm\RealmManager; | ||
use Octamp\Wamp\Session\Session; | ||
use Thruway\Message\AuthenticateMessage; | ||
use Thruway\Message\HelloMessage; | ||
|
||
class AnonymousDynamicAuthenticator extends AbstractDynamicAuthenticator | ||
{ | ||
protected ?RealmManager $realmManager; | ||
|
||
public function processHello(Session $session, HelloMessage $message): PromiseInterface | ||
{ | ||
$promise = $this->sendMessageToAuthenticator($message, []); | ||
return $promise->then(function ($result) { | ||
$authDetails = []; | ||
if (isset($result['authid'])) { | ||
$authDetails['authid'] = $result['authid']; | ||
} | ||
return [ | ||
'status' => AuthManager::STATUS_NO_CHALLENGE, | ||
'auth_details' => $authDetails, | ||
'verify_details' => $result, | ||
'challenge_details' => [ | ||
'challenge_method' => $this->getMethod(), | ||
], | ||
]; | ||
}, function ($result) { | ||
$response = [ | ||
'status' => AuthManager::STATUS_FAILURE, | ||
]; | ||
if (isset($result['error_uri'])) { | ||
$response['error_uri'] = $result['error_uri']; | ||
} | ||
|
||
if (isset($result['error_details'])) { | ||
$response['error_details'] = $result['error_details']; | ||
} | ||
|
||
return $response; | ||
}); | ||
} | ||
|
||
public function processAuthenticate(Session $session, AuthenticateMessage $message): PromiseInterface | ||
{ | ||
return new Promise(function (callable $resolve) { | ||
$resolve(['status' => AuthManager::STATUS_SUCCESS]); | ||
}); | ||
} | ||
|
||
public function getMethod(): string | ||
{ | ||
return 'anonymous'; | ||
} | ||
|
||
public function setRealmManager(RealmManager $realmManager): void | ||
{ | ||
$this->realmManager = $realmManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Auth\Exception; | ||
|
||
class AuthenticationException extends \Exception | ||
{ | ||
public function __construct(protected string $uri, protected array $errorDetails, ?string $message = null, int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct($message ?? $this->uri, $code, $previous); | ||
} | ||
|
||
public function getUri(): string | ||
{ | ||
return $this->uri; | ||
} | ||
|
||
public function getErrorDetails(): array | ||
{ | ||
return $this->errorDetails; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.