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\Credentials; |
14
|
|
|
|
15
|
|
|
use Fresh\CentrifugoBundle\Service\Jwt\JwtGenerator; |
16
|
|
|
use Fresh\CentrifugoBundle\Token\JwtPayload; |
17
|
|
|
use Fresh\CentrifugoBundle\Token\JwtPayloadForPrivateChannel; |
18
|
|
|
use Fresh\CentrifugoBundle\User\CentrifugoUserInterface; |
19
|
|
|
use Fresh\DateTime\DateTimeHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* CredentialsGenerator. |
23
|
|
|
* |
24
|
|
|
* @author Artem Henvald <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CredentialsGenerator |
27
|
|
|
{ |
28
|
|
|
private JwtGenerator $jwtGenerator; |
|
|
|
|
29
|
|
|
private DateTimeHelper $dateTimeHelper; |
30
|
|
|
private ?int $centrifugoJwtTtl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param JwtGenerator $jwtGenerator |
34
|
|
|
* @param DateTimeHelper $dateTimeHelper |
35
|
|
|
* @param int|null $centrifugoJwtTtl |
36
|
|
|
*/ |
37
|
|
|
public function __construct(JwtGenerator $jwtGenerator, DateTimeHelper $dateTimeHelper, ?int $centrifugoJwtTtl = null) |
38
|
|
|
{ |
39
|
|
|
$this->jwtGenerator = $jwtGenerator; |
40
|
|
|
$this->dateTimeHelper = $dateTimeHelper; |
41
|
|
|
$this->centrifugoJwtTtl = $centrifugoJwtTtl; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param CentrifugoUserInterface $user |
46
|
|
|
* @param string|null $base64info |
47
|
|
|
* @param array $channels |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function generateJwtTokenForUser(CentrifugoUserInterface $user, ?string $base64info = null, array $channels = []): string |
52
|
|
|
{ |
53
|
|
|
$jwtPayload = new JwtPayload( |
54
|
|
|
$user->getCentrifugoSubject(), |
55
|
|
|
$user->getCentrifugoUserInfo(), |
56
|
|
|
$this->getExpirationTime(), |
57
|
|
|
$base64info, |
58
|
|
|
$channels |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return $this->jwtGenerator->generateToken($jwtPayload); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|null $base64info |
66
|
|
|
* @param array $channels |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function generateJwtTokenForAnonymous(?string $base64info = null, array $channels = []): string |
71
|
|
|
{ |
72
|
|
|
$jwtPayload = new JwtPayload( |
73
|
|
|
'', |
74
|
|
|
[], |
75
|
|
|
$this->getExpirationTime(), |
76
|
|
|
$base64info, |
77
|
|
|
$channels |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $this->jwtGenerator->generateToken($jwtPayload); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $client |
85
|
|
|
* @param string $channel |
86
|
|
|
* @param string|null $base64info |
87
|
|
|
* @param bool|null $eto |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function generateJwtTokenForPrivateChannel(string $client, string $channel, ?string $base64info = null, ?bool $eto = null): string |
92
|
|
|
{ |
93
|
|
|
$jwtPayload = new JwtPayloadForPrivateChannel( |
94
|
|
|
$client, |
95
|
|
|
$channel, |
96
|
|
|
[], |
97
|
|
|
$this->getExpirationTime(), |
98
|
|
|
$base64info, |
99
|
|
|
$eto |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return $this->jwtGenerator->generateToken($jwtPayload); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int|null |
107
|
|
|
*/ |
108
|
|
|
private function getExpirationTime(): ?int |
109
|
|
|
{ |
110
|
|
|
$expireAt = null; |
111
|
|
|
|
112
|
|
|
if (null !== $this->centrifugoJwtTtl) { |
113
|
|
|
$now = $this->dateTimeHelper->getCurrentDatetime(); |
114
|
|
|
$now->add(new \DateInterval("PT{$this->centrifugoJwtTtl}S")); |
115
|
|
|
$expireAt = $now->getTimestamp(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $expireAt; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|