Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multi auth requests #63

Merged
merged 5 commits into from
Jan 29, 2020
Merged
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
45 changes: 41 additions & 4 deletions Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,51 @@ public function authAction(Request $request)
throw new \Exception('The authenticator service does not exsit.');
}

$responseData = array();
$authenticator = $this->container->get('lopi_pusher.authenticator');
$socketId = $request->get('socket_id');
$channelName = $request->get('channel_name');

$channelNames = $request->get('channel_name');
if (is_array($channelNames)) {
$combineResponse = array();
foreach ($channelNames as $channelName) {
$responseData = $this->authenticateChannel($socketId, $channelName, $authenticator);

if (!$responseData) {
$combineResponse[$channelName]['status'] = 403;

continue;
}

$combineResponse[$channelName]['status'] = 200;
$combineResponse[$channelName]['data'] = $responseData;
}

return new Response(json_encode($combineResponse), 200, array('Content-Type' => 'application/json'));
}

$responseData = $this->authenticateChannel($socketId, $channelNames, $authenticator);
if (!$responseData) {
throw new AccessDeniedException('Request authentication denied');
}

return new Response(json_encode($responseData), 200, array('Content-Type' => 'application/json'));
}

/**
* Perform channel autentication.
*
* @param string $socketId The socket id
* @param string $channelName Name of the channel to validate.
*
* @return array Response auth data or null on access denied.
*/
private function authenticateChannel($socketId, $channelName, $authenticator): ?array
{
$responseData = array();
$data = $socketId.':'.$channelName;

if (!$authenticator->authenticate($socketId, $channelName)) {
throw new AccessDeniedException('Request authentication denied');
return null;
}

if (strpos($channelName, 'presence') === 0 && $authenticator instanceof ChannelAuthenticatorPresenceInterface) {
Expand All @@ -59,7 +96,7 @@ public function authAction(Request $request)

$responseData['auth'] = $this->container->getParameter('lopi_pusher.config')['key'].':'.$this->getCode($data);

return new Response(json_encode($responseData), 200, array('Content-Type' => 'application/json'));
return $responseData;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Twig/PusherExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(Pusher $pusher)
/**
* @return array
*/
public function getGlobals()
public function getGlobals(): array
{
return [
'pusher_key' => $this->pusher->getSettings()['auth_key'],
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": ">=7.2",
"pusher/pusher-php-server": "^4.0",
"twig/twig": "~2.7"
"twig/twig": "~2.7|~3.0"
},
"require-dev": {
"symfony/config": "~3.4",
Expand Down