-
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.
- Loading branch information
Showing
33 changed files
with
1,698 additions
and
39 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
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,37 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Auth; | ||
|
||
use Octamp\Wamp\Session\Session; | ||
use Thruway\Message\HelloMessage; | ||
|
||
abstract class AbstractAuthenticator implements AuthenticatorInterface | ||
{ | ||
protected array $realms; | ||
|
||
public function __construct(protected array $config) | ||
{ | ||
$this->realms = $this->config['realms'] ?? []; | ||
$this->init(); | ||
} | ||
|
||
protected function init(): void | ||
{ | ||
// overwrite this method for custom implementation | ||
} | ||
|
||
public function getRealms(): array | ||
{ | ||
return $this->realms; | ||
} | ||
|
||
public function supportRealm(string $realmName): bool | ||
{ | ||
return empty($this->realms) || in_array('*', $this->realms) || in_array($realmName, $this->realms); | ||
} | ||
|
||
public function canAuthenticate(Session $session, HelloMessage $message, array $methods): bool | ||
{ | ||
return in_array($this->getMethod(), $methods) && $this->supportRealm($session->getRealm()->name); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Octamp\Wamp\Auth; | ||
|
||
use Octamp\Wamp\Promise\Promise; | ||
use Octamp\Wamp\Promise\PromiseInterface; | ||
use Octamp\Wamp\Session\Session; | ||
use Thruway\Message\AuthenticateMessage; | ||
use Thruway\Message\HelloMessage; | ||
|
||
class AnonymousStaticAuthenticator extends AbstractAuthenticator | ||
{ | ||
public function processHello(Session $session, HelloMessage $message): PromiseInterface | ||
{ | ||
return new Promise(function (callable $resolve) { | ||
$resolve(['status' => AuthManager::STATUS_NO_CHALLENGE, 'auth_details' => [ | ||
'authid' => $this->config['authid'] ?? 'anonymous', | ||
'authrole' => $this->config['role'] ?? 'anonymous', | ||
]]); | ||
}); | ||
} | ||
|
||
public function getMethod(): string | ||
{ | ||
return 'anonymous'; | ||
} | ||
|
||
public function processAuthenticate(Session $session, AuthenticateMessage $message): PromiseInterface | ||
{ | ||
return new Promise(function (callable $resolve) { | ||
$resolve(['status' => AuthManager::STATUS_SUCCESS]); | ||
}); | ||
} | ||
} |
Oops, something went wrong.