1
0
mirror of https://github.com/flarum/core.git synced 2025-07-16 14:26:25 +02:00
Files
php-flarum/extensions/pusher/src/Api/Controller/AuthController.php
2021-04-12 17:50:59 -04:00

62 lines
1.7 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Pusher\Api\Controller;
use Flarum\Http\RequestUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Pusher;
class AuthController implements RequestHandlerInterface
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$userChannel = 'private-user'.RequestUtil::getActor($request)->id;
$body = $request->getParsedBody();
if (Arr::get($body, 'channel_name') === $userChannel) {
$pusher = new Pusher(
$this->settings->get('flarum-pusher.app_key'),
$this->settings->get('flarum-pusher.app_secret'),
$this->settings->get('flarum-pusher.app_id'),
['cluster' => $this->settings->get('flarum-pusher.app_cluster')]
);
$payload = json_decode($pusher->socket_auth($userChannel, Arr::get($body, 'socket_id')), true);
return new JsonResponse($payload);
}
return new EmptyResponse(403);
}
}