/
Subscriber.php
133 lines (117 loc) · 3.58 KB
/
Subscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Concrete\Core\Notification\Events;
use Concrete\Core\Application\Application;
use Concrete\Core\Cookie\ResponseCookieJar;
use Concrete\Core\Notification\Events\ServerEvent\ServerEventInterface;
use Concrete\Core\Notification\Events\Topic\TopicInterface;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256 as RS256;
use Lcobucci\JWT\Signer\Hmac\Sha256 as HS256;
use Lcobucci\JWT\Token\Builder as TokenBuilder;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Token\Plain;
class Subscriber
{
/**
* Topic URLs to subscribe to in Mercure
*
* @var array
*/
protected $topics = [];
/**
* @var ResponseCookieJar
*/
protected $cookieJar;
/**
* @var Application
*/
protected $app;
public function __construct(Application $app, ResponseCookieJar $cookieJar)
{
$this->app = $app;
$this->cookieJar = $cookieJar;
}
public function addTopics(array $topics)
{
foreach ($topics as $topic) {
$this->topics[] = (string) $topic;
}
}
/**
* @param string|TopicInterface $topic
*/
public function addTopic($topic)
{
$this->topics[] = (string) $topic;
}
/**
* @return array
*/
public function getTopics(): array
{
return $this->topics;
}
public function removeTopics(array $topicsToRemove)
{
$newTopics = [];
foreach ($this->topics as $topic) {
if (!in_array($topic, $topicsToRemove)) {
$newTopics[] = $topic;
}
}
$this->topics = $newTopics;
}
private function getSubscriberJwt(): string
{
$config = $this->app->make('config');
$dbConfig = $this->app->make('config/database');
if (class_exists(TokenBuilder::class)) {
$builder = new TokenBuilder(new JoseEncoder(), ChainedFormatter::default());
} else {
$builder = new Builder();
}
$connectionMethod = $config->get('concrete.notification.mercure.default.connection_method') ?? null;
if ($connectionMethod === 'rsa_dual') {
$keyString = file_get_contents(
$config->get('concrete.notification.mercure.default.subscriber_private_key_path')
);
$signer = new RS256();
} else {
$keyString = $dbConfig->get('concrete.notification.mercure.default.jwt_key');
$signer = new HS256();
}
if (class_exists(InMemory::class)) {
$key = InMemory::plainText($keyString, '');
} else {
$key = new Key($keyString, '');
}
$expires = new \DateTimeImmutable($config->get('concrete.notification.mercure.jwt.subscriber.expires_at'));
$token = $builder
->withClaim('mercure', ['subscribe' => $this->getTopics()])
->expiresAt($expires)
->getToken(
$signer,
$key
);
if ($token instanceof Plain) {
return $token->toString();
} else {
return (string)$token;
}
}
public function refreshAuthorizationCookie()
{
$config = $this->app->make('config');
$cookieDomain = $config->get('concrete.notification.mercure.default.cookie_domain');
return $this->cookieJar->addCookie(
'mercureAuthorization',
$this->getSubscriberJwt(),
0,
DIR_REL,
$cookieDomain
);
}
}