aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorwinter2024-12-17 21:04:20 +0000
committerwinter2024-12-17 21:04:20 +0000
commitc58caced838a9a7954e7fefa60c01567bb6bc107 (patch)
treec68d6bb8f7c8b9636485e3f52f9a7b856316e0de
parent10793d22df72f12f2f14c730c09b5018b5b6e63a (diff)
winter REPORTEDLY forgot to implement subscribe routes
-rw-r--r--Digitigrade/HttpResponseStatus/BadRequest.php2
-rw-r--r--Digitigrade/HttpResponseStatus/Forbidden.php2
-rw-r--r--Digitigrade/HttpResponseStatus/NotFound.php2
-rw-r--r--Digitigrade/HttpResponseStatus/Unauthorized.php2
-rw-r--r--Digitigrade/Model/Instance.php4
-rw-r--r--misc/get_remote_object_authenticated.php7
-rw-r--r--routes/subscribe.php16
7 files changed, 26 insertions, 9 deletions
diff --git a/Digitigrade/HttpResponseStatus/BadRequest.php b/Digitigrade/HttpResponseStatus/BadRequest.php
index e605f70..7dd4d00 100644
--- a/Digitigrade/HttpResponseStatus/BadRequest.php
+++ b/Digitigrade/HttpResponseStatus/BadRequest.php
@@ -4,7 +4,7 @@ namespace Digitigrade\HttpResponseStatus;
use Digitigrade\HttpResponseStatus;
class BadRequest extends \Exception implements HttpResponseStatus {
- public function __construct(?string $reason) {
+ public function __construct(?string $reason = null) {
$this->message = $reason ?? 'Request does not conform to expectations';
}
diff --git a/Digitigrade/HttpResponseStatus/Forbidden.php b/Digitigrade/HttpResponseStatus/Forbidden.php
index 4c3588f..662b9b3 100644
--- a/Digitigrade/HttpResponseStatus/Forbidden.php
+++ b/Digitigrade/HttpResponseStatus/Forbidden.php
@@ -4,7 +4,7 @@ namespace Digitigrade\HttpResponseStatus;
use Digitigrade\HttpResponseStatus;
class Forbidden extends \Exception implements HttpResponseStatus {
- public function __construct(?string $reason) {
+ public function __construct(?string $reason = null) {
$this->message = $reason ?? 'The provided authorization is invalid for this request';
}
diff --git a/Digitigrade/HttpResponseStatus/NotFound.php b/Digitigrade/HttpResponseStatus/NotFound.php
index 7da4187..1f51f88 100644
--- a/Digitigrade/HttpResponseStatus/NotFound.php
+++ b/Digitigrade/HttpResponseStatus/NotFound.php
@@ -4,7 +4,7 @@ namespace Digitigrade\HttpResponseStatus;
use Digitigrade\HttpResponseStatus;
class NotFound extends \Exception implements HttpResponseStatus {
- public function __construct(?string $reason) {
+ public function __construct(?string $reason = null) {
$this->message = $reason ?? 'Request was handled but no appropriate resource found';
}
diff --git a/Digitigrade/HttpResponseStatus/Unauthorized.php b/Digitigrade/HttpResponseStatus/Unauthorized.php
index ab8b080..9e8163d 100644
--- a/Digitigrade/HttpResponseStatus/Unauthorized.php
+++ b/Digitigrade/HttpResponseStatus/Unauthorized.php
@@ -4,7 +4,7 @@ namespace Digitigrade\HttpResponseStatus;
use Digitigrade\HttpResponseStatus;
class Unauthorized extends \Exception implements HttpResponseStatus {
- public function __construct(?string $reason) {
+ public function __construct(?string $reason = null) {
$this->message = $reason ?? 'This request requires authorization';
}
diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php
index 2fc2f17..168a1de 100644
--- a/Digitigrade/Model/Instance.php
+++ b/Digitigrade/Model/Instance.php
@@ -143,10 +143,12 @@ class Instance extends FetchableModel {
throw new \RuntimeException("can't (un)subscribe to $this->domain because i don't have an outbound token for it");
}
$context = stream_context_create(['http' => [
+ 'method' => 'POST',
'header' => 'Authorization: Bearer ' . $this->auth->outboundToken
]]);
file_get_contents($isUnsubscribe ? $this->endpoints->unsubscribe : $this->endpoints->subscribe, context: $context);
- return str_contains($http_response_header[0], '200');
+ // it ought to return 204 but i'll allow 200 for compatibility's sake
+ return str_contains($http_response_header[0], '204') || str_contains($http_response_header[0], '200');
}
/**
diff --git a/misc/get_remote_object_authenticated.php b/misc/get_remote_object_authenticated.php
index 49aaa59..3c67005 100644
--- a/misc/get_remote_object_authenticated.php
+++ b/misc/get_remote_object_authenticated.php
@@ -4,12 +4,13 @@ use Digitigrade\GlobalConfig;
use Digitigrade\Model\Instance;
/**
- * Makes an HTTPS GET request to a remote instance, using the corresponding outbound auth token (if any)
+ * Makes an HTTPS request to a remote instance, using the corresponding outbound auth token (if any)
* @param string $uri
* @param bool $dontLookUpInstance if true, will skip looking up the Instance, and consequently the auth token
+ * @param string $method HTTP verb
* @return false|string the response data, or false if it failed
*/
-function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance = false): false|string {
+function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance = false, string $method = 'GET'): false|string {
if (!str_starts_with($uri, 'https://'))
return false;
@@ -26,6 +27,7 @@ function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance =
if ($instance?->auth?->outboundToken == null) {
// hopefully we can make the request anyway?
+ $context = stream_context_create(['http' => ['method' => $method]]);
$resp = file_get_contents($uri);
// $http_response_header just poofs into existence .
// i don't like this api. i should probably use curl instead. hmm
@@ -41,6 +43,7 @@ function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance =
}
$context = stream_context_create(['http' => [
+ 'method' => $method,
'header' => 'Authorization: Bearer ' . $instance->auth->outboundToken
]]);
return file_get_contents($uri, context: $context);
diff --git a/routes/subscribe.php b/routes/subscribe.php
index c18d484..91a611f 100644
--- a/routes/subscribe.php
+++ b/routes/subscribe.php
@@ -1,11 +1,23 @@
<?php
+use Digitigrade\HttpResponseStatus\Unauthorized;
+use Digitigrade\Model\Instance;
use Digitigrade\Router;
Router::getInstance()->mount('/subscribe', function (array $args) {
-
+ $instance = Instance::findByRequestHeaders();
+ if ($instance == null) {
+ throw new Unauthorized();
+ }
+ $instance->auth->outboundPushEnabled = true;
+ http_response_code(204); // "No Content"
});
Router::getInstance()->mount('/unsubscribe', function (array $args) {
-
+ $instance = Instance::findByRequestHeaders();
+ if ($instance == null) {
+ throw new Unauthorized();
+ }
+ $instance->auth->outboundPushEnabled = false;
+ http_response_code(204); // "No Content"
}); \ No newline at end of file