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