1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshCentrifugoBundle. |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\CentrifugoBundle\Service\ChannelAuthenticator; |
14
|
|
|
|
15
|
|
|
use Fresh\CentrifugoBundle\Service\Credentials\CredentialsGenerator; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PrivateChannelAuthenticator. |
21
|
|
|
* |
22
|
|
|
* @author Artem Henvald <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class PrivateChannelAuthenticator |
25
|
|
|
{ |
26
|
|
|
/** @var CredentialsGenerator */ |
27
|
|
|
private $credentialsGenerator; |
28
|
|
|
|
29
|
|
|
/** @var ChannelAuthenticatorInterface[]|iterable */ |
30
|
|
|
private $channelAuthenticators; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param CredentialsGenerator $credentialsGenerator |
34
|
|
|
* @param ChannelAuthenticatorInterface[]|iterable $channelAuthenticators |
35
|
|
|
*/ |
36
|
|
|
public function __construct(CredentialsGenerator $credentialsGenerator, iterable $channelAuthenticators) |
37
|
|
|
{ |
38
|
|
|
$this->credentialsGenerator = $credentialsGenerator; |
39
|
|
|
$this->channelAuthenticators = $channelAuthenticators; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Request $request |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function authChannelsForClientFromRequest(Request $request): array |
48
|
|
|
{ |
49
|
|
|
$authData = []; |
50
|
|
|
|
51
|
|
|
[$client, $channels] = $this->processRequest($request); |
|
|
|
|
52
|
|
|
|
53
|
|
|
foreach ($channels as $channel) { |
54
|
|
|
$token = $this->authChannelForClient($client, (string) $channel); |
55
|
|
|
|
56
|
|
|
if (\is_string($token)) { |
57
|
|
|
$authData[] = [ |
58
|
|
|
'channel' => (string) $channel, |
59
|
|
|
'token' => $token, |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return ['channels' => $authData]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $client |
69
|
|
|
* @param string $channel |
70
|
|
|
* |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
private function authChannelForClient(string $client, string $channel): ?string |
74
|
|
|
{ |
75
|
|
|
$token = null; |
76
|
|
|
|
77
|
|
|
$channelAuthenticator = $this->findAppropriateChannelAuthenticator($channel); |
78
|
|
|
if ($channelAuthenticator instanceof ChannelAuthenticatorInterface && $channelAuthenticator->hasAccessToChannel($channel)) { |
79
|
|
|
$token = $this->credentialsGenerator->generateJwtTokenForPrivateChannel($client, $channel); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $token; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $channel |
87
|
|
|
* |
88
|
|
|
* @return ChannelAuthenticatorInterface|null |
89
|
|
|
*/ |
90
|
|
|
private function findAppropriateChannelAuthenticator(string $channel): ?ChannelAuthenticatorInterface |
91
|
|
|
{ |
92
|
|
|
foreach ($this->channelAuthenticators as $channelAuthenticator) { |
93
|
|
|
if ($channelAuthenticator->supports($channel)) { |
94
|
|
|
return $channelAuthenticator; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Request $request |
103
|
|
|
* |
104
|
|
|
* @throws BadRequestHttpException |
105
|
|
|
* @throws \Exception |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
private function processRequest(Request $request): array |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$content = \json_decode((string) $request->getContent(), true, 512, \JSON_THROW_ON_ERROR); |
113
|
|
|
} catch (\JsonException $e) { |
114
|
|
|
throw new BadRequestHttpException('Invalid JSON.'); |
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
throw $e; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$result = []; |
120
|
|
|
|
121
|
|
|
if (!isset($content['client']) || !\is_string($content['client'])) { |
122
|
|
|
throw new BadRequestHttpException('Client must be set in request.'); |
123
|
|
|
} |
124
|
|
|
$result[] = $content['client']; |
125
|
|
|
|
126
|
|
|
if (!isset($content['channels']) || !\is_array($content['channels']) || empty($content['channels'])) { |
127
|
|
|
throw new BadRequestHttpException('Channels must be set in request.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach ($content['channels'] as $channel) { |
131
|
|
|
if (!\is_string($channel)) { |
132
|
|
|
throw new BadRequestHttpException('Channel must be a string.'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$result[] = $content['channels']; |
137
|
|
|
|
138
|
|
|
return $result; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.