From 75a77b3f5c5e0cdecddcc2e44bfeb5443b0e946b Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 28 Jun 2023 18:02:14 +0200 Subject: [PATCH] finalize account handling still missing: publishing --- includes/activity/class-base-object.php | 1 - includes/class-activitypub.php | 8 ++++---- includes/class-signature.php | 2 +- includes/functions.php | 2 +- includes/model/class-application-user.php | 4 ++-- includes/model/class-blog-user.php | 8 ++++++-- includes/model/class-user.php | 19 ++++++++++++++++++- includes/rest/class-users.php | 4 ++++ includes/rest/class-webfinger.php | 1 - ...-class-activitypub-activity-dispatcher.php | 1 + ...typub-rest-post-signature-verification.php | 4 ++-- 11 files changed, 39 insertions(+), 15 deletions(-) diff --git a/includes/activity/class-base-object.php b/includes/activity/class-base-object.php index 0ad4457..c8520ba 100644 --- a/includes/activity/class-base-object.php +++ b/includes/activity/class-base-object.php @@ -472,7 +472,6 @@ class Base_Object { public function get( $key ) { if ( ! $this->has( $key ) ) { return new WP_Error( 'invalid_key', 'Invalid key' ); - } return $this->$key; diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 8e72caa..cd84dc1 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -71,16 +71,16 @@ class Activitypub { * @return string The new path to the JSON template. */ public static function render_json_template( $template ) { + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + return $template; + } + if ( ! is_activitypub_request() ) { return $template; } $json_template = false; - if ( ! \is_author() && ! \is_singular() && ! \is_home() ) { - return $template; - } - // check if user can publish posts if ( \is_author() && ! User_Factory::get_by_id( \get_the_author_meta( 'ID' ) ) ) { return $template; diff --git a/includes/class-signature.php b/includes/class-signature.php index c1ccbdf..b9666cc 100644 --- a/includes/class-signature.php +++ b/includes/class-signature.php @@ -107,7 +107,7 @@ class Signature { */ public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) { $user = User_Factory::get_by_id( $user_id ); - $key = $user->get_private_key(); + $key = $user->get__private_key(); $url_parts = \wp_parse_url( $url ); diff --git a/includes/functions.php b/includes/functions.php index a66d44e..88b0e2b 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -237,7 +237,7 @@ function is_activitypub_request() { * ActivityPub requests are currently only made for * author archives, singular posts, and the homepage. */ - if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( 'REST_REQUEST' ) && ! REST_REQUEST ) { + if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) && ! \REST_REQUEST ) { return false; } diff --git a/includes/model/class-application-user.php b/includes/model/class-application-user.php index 9c53f9d..c4ffaca 100644 --- a/includes/model/class-application-user.php +++ b/includes/model/class-application-user.php @@ -39,7 +39,7 @@ class Application_User extends Blog_User { return $this::get_name(); } - public function get_public_key() { + public function get__public_key() { $key = \get_option( 'activitypub_application_user_public_key' ); if ( $key ) { @@ -58,7 +58,7 @@ class Application_User extends Blog_User { * * @return mixed */ - public function get_private_key() { + public function get__private_key() { $key = \get_option( 'activitypub_application_user_private_key' ); if ( $key ) { diff --git a/includes/model/class-blog-user.php b/includes/model/class-blog-user.php index a4d3374..f3fd095 100644 --- a/includes/model/class-blog-user.php +++ b/includes/model/class-blog-user.php @@ -150,7 +150,7 @@ class Blog_User extends User { return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); } - public function get_public_key() { + public function get__public_key() { $key = \get_option( 'activitypub_blog_user_public_key' ); if ( $key ) { @@ -169,7 +169,7 @@ class Blog_User extends User { * * @return mixed */ - public function get_private_key() { + public function get__private_key() { $key = \get_option( 'activitypub_blog_user_private_key' ); if ( $key ) { @@ -193,4 +193,8 @@ class Blog_User extends User { public function get_attachment() { return array(); } + + public function get_canonical_url() { + return \home_url(); + } } diff --git a/includes/model/class-user.php b/includes/model/class-user.php index 2ee2258..10d10d6 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -123,6 +123,19 @@ class User extends Actor { } public function get_public_key() { + return array( + 'id' => $this->get_id() . '#main-key', + 'owner' => $this->get_id(), + 'publicKeyPem' => $this->get__public_key(), + ); + } + + /** + * @param int $this->get__id() + * + * @return mixed + */ + public function get__public_key() { $key = \get_user_meta( $this->get__id(), 'magic_sig_public_key', true ); if ( $key ) { @@ -139,7 +152,7 @@ class User extends Actor { * * @return mixed */ - public function get_private_key() { + public function get__private_key() { $key = \get_user_meta( $this->get__id(), 'magic_sig_private_key', true ); if ( $key ) { @@ -242,4 +255,8 @@ class User extends Actor { public function get_resource() { return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); } + + public function get_canonical_url() { + return $this->get_url(); + } } diff --git a/includes/rest/class-users.php b/includes/rest/class-users.php index 66637b7..1dd08f8 100644 --- a/includes/rest/class-users.php +++ b/includes/rest/class-users.php @@ -67,6 +67,10 @@ class Users { */ \do_action( 'activitypub_outbox_pre' ); + $user->set_context( + \Activitypub\Model\Activity::CONTEXT + ); + $json = $user->to_array(); $response = new WP_REST_Response( $json, 200 ); diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index ec76686..f124889 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -55,7 +55,6 @@ class Webfinger { $aliases = array( $user->get_url(), - $user->get_canonical_url(), $user->get_at_url(), ); diff --git a/tests/test-class-activitypub-activity-dispatcher.php b/tests/test-class-activitypub-activity-dispatcher.php index 70ed304..61f8c2b 100644 --- a/tests/test-class-activitypub-activity-dispatcher.php +++ b/tests/test-class-activitypub-activity-dispatcher.php @@ -49,6 +49,7 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT remove_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10 ); } + public function test_dispatch_mentions() { $post = \wp_insert_post( array( diff --git a/tests/test-class-activitypub-rest-post-signature-verification.php b/tests/test-class-activitypub-rest-post-signature-verification.php index 2b7fc29..3ee895e 100644 --- a/tests/test-class-activitypub-rest-post-signature-verification.php +++ b/tests/test-class-activitypub-rest-post-signature-verification.php @@ -44,7 +44,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase { $user = Activitypub\User_Factory::get_by_id( 1 ); - $public_key = $user->get_public_key(); + $public_key = $user->get__public_key(); // signature_verification $verified = \openssl_verify( $signed_data, $signature_block['signature'], $public_key, 'rsa-sha256' ) > 0; @@ -56,7 +56,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase { 'pre_get_remote_metadata_by_actor', function( $json, $actor ) { $user = Activitypub\User_Factory::get_by_id( 1 ); - $public_key = $user->get_public_key(); + $public_key = $user->get__public_key(); // return ActivityPub Profile with signature return array( 'id' => $actor,