Skip to content

Commit c88fc9b

Browse files
Notifications over WebSockets, see #105
1 parent 7162a2c commit c88fc9b

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

solid/appinfo/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
['name' => 'solidWebhook#register', 'url' => '/webhook/register', 'verb' => 'POST'],
5656
['name' => 'solidWebhook#unregister', 'url' => '/webhook/unregister', 'verb' => 'POST'],
5757

58-
['name' => 'solidWebsocket#register', 'url' => '/websocket/register', 'verb' => 'POST'],
58+
['name' => 'solidWebhook#registerWs', 'url' => '/websocket/register', 'verb' => 'POST'],
5959

6060
['name' => 'app#appLauncher', 'url' => '/', 'verb' => 'GET'],
6161
]

solid/composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solid/lib/Controller/SolidWebhookController.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ public function listWebhooks(): DataResponse {
8383
return new DataResponse($this->webhookService->findAll($this->webId));
8484
}
8585

86+
/**
87+
* @PublicPage
88+
* @NoAdminRequired
89+
* @NoCSRFRequired
90+
*/
91+
public function registerWs(string $topic): DataResponse {
92+
$toSub = "http://pubsub:8081";
93+
$toPub = "http://pubsub:8082";
94+
// FIXME: is this secure enough?
95+
// https://www.php.net/manual/en/function.random-bytes.php says it
96+
// generates "cryptographically secure pseudo-random bytes"
97+
$token = bin2hex(random_bytes(20));
98+
$target = "$toPub/$token?" . urlencode($topic);
99+
if ($this->checkReadAccess($topic)) {
100+
$webhook = $this->webhookService->create($this->webId, $topic, $target);
101+
return new DataResponse([
102+
"@context" => "https://www.w3.org/ns/solid/notification/v1",
103+
"type" => "WebSocketSubscription2021",
104+
"source" => "$toSub/$token"
105+
]);
106+
} else {
107+
return new DataResponse("Error: denied access", 401);
108+
}
109+
}
110+
86111
/**
87112
* @PublicPage
88113
* @NoAdminRequired

0 commit comments

Comments
 (0)