From 0c2a5b5eb737b476e6ff26fa566698719834b790 Mon Sep 17 00:00:00 2001 From: veselcraft Date: Tue, 28 Dec 2021 17:21:26 +0300 Subject: [PATCH] ActivityPub: Add a Public PEM key, little fixes to the JSON Encoder --- README.md | 3 ++- Web/Presenters/OpenVKPresenter.php | 18 +++++++++++++++++- Web/Presenters/UserPresenter.php | 5 +++++ data/.gitignore | 3 +++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100755 data/.gitignore diff --git a/README.md b/README.md index 72991be0..69707554 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,8 @@ ln -s /path/to/chandler/extensions/available/openvk /path/to/chandler/extensions 6. Copy `openvk-example.yml` to `openvk.yml` and change options 7. Run `composer install` in OpenVK directory 8. Move to `Web/static/js` and execute `yarn install` -9. Set `openvk` as your root app in `chandler.yml` +9. Generate the private and public OpenSSL keys by running `openssl genrsa -out data/private.pem 2048` and `openssl rsa -in private.pem -outform PEM -pubout -out data/public.pem` in OpenVK directory +10. Set `openvk` as your root app in `chandler.yml` Once you are done, you can login as a system administrator on the network itself (no registration required): * **Login**: `admin@localhost.localdomain6` diff --git a/Web/Presenters/OpenVKPresenter.php b/Web/Presenters/OpenVKPresenter.php index 43800deb..14685412 100755 --- a/Web/Presenters/OpenVKPresenter.php +++ b/Web/Presenters/OpenVKPresenter.php @@ -256,13 +256,15 @@ abstract class OpenVKPresenter extends SimplePresenter protected function returnJson(array $json): void { - $payload = json_encode($json); + $payload = json_encode($json, JSON_UNESCAPED_UNICODE); $size = strlen($payload); header("Content-Type: application/json"); header("Content-Length: $size"); exit($payload); } + /* ActivityPub quicks :DDD */ + function isActivityPubClient(): bool { $accept = explode(";", $_SERVER['HTTP_ACCEPT']); @@ -304,4 +306,18 @@ abstract class OpenVKPresenter extends SimplePresenter ]'); // Гришк) } + + /** + * @param private If true, it will return the private key. Otherwise, it will return the public key. + */ + function getKey(bool $private = false) + { + if(!file_exists(OPENVK_ROOT . ($private ? "/data/private.pem" : "/data/public.pem"))) + throw new ISE("private.pem and public.pem files are missing. Please, check 10th step for a installation guide in README file."); + + $key = file(OPENVK_ROOT . ($private ? "/data/private.pem" : "/data/public.pem")); + $key[0] = null; + $key[count($key)-1] = null; + return "-----BEGIN PUBLIC KEY-----\n" . str_replace("\n", '', implode('', $key)) . "\n-----END PUBLIC KEY-----\n"; + } } diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 97edddd8..5d4b8670 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -44,6 +44,11 @@ final class UserPresenter extends OpenVKPresenter "inbox" => $user->getFullURL() . "/inbox", "outbox" => $user->getFullURL() . "/outbox", "endpoints" => array("sharedInbox" => ovk_scheme(true) . $_SERVER['SERVER_NAME']), + "publicKey" => array( + "id" => $user->getFullURL(true) . "#main-key", + "owner" => $user->getFullURL(true), + "publicKeyPem" => $this->getKey() + ), "wall" => ovk_scheme(true) . $_SERVER['SERVER_NAME'] . "/wall" . $user->getId(), "firstName" => $user->getFirstName(), "lastName" => $user->getLastName(), diff --git a/data/.gitignore b/data/.gitignore new file mode 100755 index 00000000..01b57162 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,3 @@ +private.pem +public.pem +knowledgebase/*