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
|
|
|
private CredentialsGenerator $credentialsGenerator; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** @var ChannelAuthenticatorInterface[]|iterable */ |
29
|
|
|
private iterable $channelAuthenticators; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param CredentialsGenerator $credentialsGenerator |
33
|
|
|
* @param ChannelAuthenticatorInterface[]|iterable $channelAuthenticators |
34
|
|
|
*/ |
35
|
|
|
public function __construct(CredentialsGenerator $credentialsGenerator, iterable $channelAuthenticators) |
36
|
|
|
{ |
37
|
|
|
$this->credentialsGenerator = $credentialsGenerator; |
38
|
|
|
$this->channelAuthenticators = $channelAuthenticators; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Request $request |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function authChannelsForClientFromRequest(Request $request): array |
47
|
|
|
{ |
48
|
|
|
$authData = []; |
49
|
|
|
|
50
|
|
|
[$client, $channels] = $this->processRequest($request); |
51
|
|
|
|
52
|
|
|
foreach ($channels as $channel) { |
53
|
|
|
$token = $this->authChannelForClient($client, (string) $channel, $request); |
54
|
|
|
|
55
|
|
|
if (\is_string($token)) { |
56
|
|
|
$authData[] = [ |
57
|
|
|
'channel' => (string) $channel, |
58
|
|
|
'token' => $token, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return ['channels' => $authData]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $client |
68
|
|
|
* @param string $channel |
69
|
|
|
* @param Request $request |
70
|
|
|
* |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
private function authChannelForClient(string $client, string $channel, Request $request): ?string |
74
|
|
|
{ |
75
|
|
|
$token = null; |
76
|
|
|
|
77
|
|
|
$channelAuthenticator = $this->findAppropriateChannelAuthenticator($channel, $request); |
78
|
|
|
if ($channelAuthenticator instanceof ChannelAuthenticatorInterface && $channelAuthenticator->hasAccessToChannel($channel, $request)) { |
79
|
|
|
$token = $this->credentialsGenerator->generateJwtTokenForPrivateChannel($client, $channel); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $token; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $channel |
87
|
|
|
* @param Request $request |
88
|
|
|
* |
89
|
|
|
* @return ChannelAuthenticatorInterface|null |
90
|
|
|
*/ |
91
|
|
|
private function findAppropriateChannelAuthenticator(string $channel, Request $request): ?ChannelAuthenticatorInterface |
92
|
|
|
{ |
93
|
|
|
foreach ($this->channelAuthenticators as $channelAuthenticator) { |
94
|
|
|
if ($channelAuthenticator->supports($channel, $request)) { |
95
|
|
|
return $channelAuthenticator; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Request $request |
104
|
|
|
* |
105
|
|
|
* @throws BadRequestHttpException |
106
|
|
|
* @throws \Exception |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private function processRequest(Request $request): array |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
$content = \json_decode((string) $request->getContent(), true, 512, \JSON_THROW_ON_ERROR); |
114
|
|
|
} catch (\JsonException $e) { |
115
|
|
|
throw new BadRequestHttpException('Invalid JSON.'); |
116
|
|
|
} catch (\Exception $e) { |
117
|
|
|
throw $e; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$result = []; |
121
|
|
|
|
122
|
|
|
if (!isset($content['client']) || !\is_string($content['client'])) { |
123
|
|
|
throw new BadRequestHttpException('Client must be set in request.'); |
124
|
|
|
} |
125
|
|
|
$result[] = $content['client']; |
126
|
|
|
|
127
|
|
|
if (!isset($content['channels']) || !\is_array($content['channels']) || empty($content['channels'])) { |
128
|
|
|
throw new BadRequestHttpException('Channels must be set in request.'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
foreach ($content['channels'] as $channel) { |
132
|
|
|
if (!\is_string($channel)) { |
133
|
|
|
throw new BadRequestHttpException('Channel must be a string.'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$result[] = $content['channels']; |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|