|
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 Symfony\Component\HttpKernel\Exception\HttpException; |
|
11
|
|
|
|
|
12
|
|
|
class CentrifugoBroadcaster extends Broadcaster |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The Centrifugo SDK instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var Contracts\CentrifugoInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $centrifugo; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new broadcaster instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Centrifugo $centrifugo |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Centrifugo $centrifugo) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->centrifugo = $centrifugo; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Authenticate the incoming request for a given channel. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Illuminate\Http\Request $request |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function auth($request) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($request->user()) { |
|
40
|
|
|
$client = $this->getClientFromRequest($request); |
|
41
|
|
|
$channels = $this->getChannelsFromRequest($request); |
|
42
|
|
|
|
|
43
|
|
|
$response = []; |
|
44
|
|
|
foreach ($channels as $channel) { |
|
45
|
|
|
$channelName = $this->getChannelName($channel); |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
$is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName); |
|
49
|
|
|
} catch (HttpException $e) { |
|
50
|
|
|
$is_access_granted = false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$response[$channel] = $this->makeResponseForClient($is_access_granted, $client); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return response()->json($response); |
|
|
|
|
|
|
57
|
|
|
} else { |
|
58
|
|
|
throw new HttpException(401); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return the valid authentication response. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Illuminate\Http\Request $request |
|
66
|
|
|
* @param mixed $result |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function validAuthenticationResponse($request, $result) |
|
70
|
|
|
{ |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Broadcast the given event. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $channels |
|
78
|
|
|
* @param string $event |
|
79
|
|
|
* @param array $payload |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
|
83
|
|
|
{ |
|
84
|
|
|
$payload['event'] = $event; |
|
85
|
|
|
|
|
86
|
|
|
$response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload); |
|
87
|
|
|
|
|
88
|
|
|
if (is_array($response) && ! isset($response['error'])) { |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new BroadcastException( |
|
93
|
|
|
$response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error'] |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get client from request. |
|
99
|
|
|
* |
|
100
|
|
|
* @param \Illuminate\Http\Request $request |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getClientFromRequest($request) |
|
104
|
|
|
{ |
|
105
|
|
|
return $request->get('client', ''); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get channels from request. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Illuminate\Http\Request $request |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getChannelsFromRequest($request) |
|
115
|
|
|
{ |
|
116
|
|
|
$channels = $request->get('channels', []); |
|
117
|
|
|
|
|
118
|
|
|
return is_array($channels) ? $channels : [$channels]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get channel name without $ symbol (if present). |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $channel |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getChannelName(string $channel) |
|
128
|
|
|
{ |
|
129
|
|
|
return (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make response for client, based on access rights. |
|
134
|
|
|
* |
|
135
|
|
|
* @param bool $access_granted |
|
136
|
|
|
* @param string $client |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
private function makeResponseForClient(bool $access_granted, string $client) |
|
140
|
|
|
{ |
|
141
|
|
|
$info = []; |
|
142
|
|
|
|
|
143
|
|
|
return $access_granted ? [ |
|
144
|
|
|
'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info), |
|
145
|
|
|
'info' => $info, |
|
146
|
|
|
] : [ |
|
147
|
|
|
'status' => 403, |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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: