Skip to content

Commit

Permalink
Merge pull request #63 from Pushappy/master
Browse files Browse the repository at this point in the history
Support for multi auth requests
  • Loading branch information
laupiFrpar authored Jan 29, 2020
2 parents a562d4e + c0c1fec commit 141766c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
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

0 comments on commit 141766c

Please sign in to comment.