1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace denis660\Centrifugo; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster; |
9
|
|
|
use Illuminate\Broadcasting\BroadcastException; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
12
|
|
|
|
13
|
|
|
class CentrifugoBroadcaster extends Broadcaster |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The Centrifugo SDK instance. |
17
|
|
|
* |
18
|
|
|
* @var Contracts\CentrifugoInterface |
19
|
|
|
*/ |
20
|
|
|
protected $centrifugo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new broadcaster instance. |
24
|
|
|
* |
25
|
|
|
* @param Centrifugo $centrifugo |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Centrifugo $centrifugo) |
28
|
|
|
{ |
29
|
|
|
$this->centrifugo = $centrifugo; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Authenticate the incoming request for a given channel. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function auth($request) |
39
|
|
|
{ |
40
|
|
|
if ($request->user()) { |
41
|
|
|
$client = $this->getClientFromRequest($request); |
42
|
|
|
$channels = $this->getChannelsFromRequest($request); |
43
|
|
|
|
44
|
|
|
$response = []; |
45
|
|
|
foreach ($channels as $channel) { |
46
|
|
|
$channelName = $this->getChannelName($channel); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName); |
50
|
|
|
} catch (HttpException $e) { |
51
|
|
|
$is_access_granted = false; |
52
|
|
|
} |
53
|
|
|
if ($this->isPrivateChannel($channel)) { |
54
|
|
|
$response['channels'][] = $this->makeResponseForPrivateChannel($is_access_granted, $client, $channel); |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
$response[$channel] = $this->makeResponseForClient($is_access_granted, $client); |
58
|
|
|
} |
59
|
|
|
return response()->json($response); |
|
|
|
|
60
|
|
|
} else { |
61
|
|
|
throw new HttpException(401); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the valid authentication response. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Http\Request $request |
69
|
|
|
* @param mixed $result |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function validAuthenticationResponse($request, $result) |
73
|
|
|
{ |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Broadcast the given event. |
79
|
|
|
* |
80
|
|
|
* @param array $channels |
81
|
|
|
* @param string $event |
82
|
|
|
* @param array $payload |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
86
|
|
|
{ |
87
|
|
|
$payload['event'] = $event; |
88
|
|
|
|
89
|
|
|
$response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload); |
90
|
|
|
|
91
|
|
|
if (is_array($response) && !isset($response['error'])) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new BroadcastException( |
96
|
|
|
$response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error'] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get client from request. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Http\Request $request |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
private function getClientFromRequest($request) |
107
|
|
|
{ |
108
|
|
|
return $request->get('client', ''); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get channels from request. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Http\Request $request |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
private function getChannelsFromRequest($request) |
118
|
|
|
{ |
119
|
|
|
$channels = $request->get('channels', []); |
120
|
|
|
|
121
|
|
|
return is_array($channels) ? $channels : [$channels]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get channel name without $ symbol (if present). |
126
|
|
|
* |
127
|
|
|
* @param string $channel |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
private function getChannelName(string $channel) |
131
|
|
|
{ |
132
|
|
|
return (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Make response for client, based on access rights. |
137
|
|
|
* |
138
|
|
|
* @param bool $access_granted |
139
|
|
|
* @param string $client |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
private function makeResponseForClient(bool $access_granted, string $client) |
143
|
|
|
{ |
144
|
|
|
$info = []; |
145
|
|
|
|
146
|
|
|
return $access_granted ? [ |
147
|
|
|
'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info), |
148
|
|
|
'info' => $info, |
149
|
|
|
] : [ |
150
|
|
|
'status' => 403, |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function makeResponseForPrivateChannel(bool $access_granted, string $client, string $channel): array |
155
|
|
|
{ |
156
|
|
|
return $access_granted ? [ |
157
|
|
|
'channel' => $channel, |
158
|
|
|
'token' => $this->centrifugo->generatePrivateChannelToken($client, $channel), |
159
|
|
|
] : [ |
160
|
|
|
'status' => 403, |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function isPrivateChannel(string $channel): bool |
165
|
|
|
{ |
166
|
|
|
return Str::contains($channel, "$"); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: