finalize account handling

still missing: publishing
This commit is contained in:
Matthias Pfefferle 2023-06-28 18:02:14 +02:00
parent c02702f773
commit 75a77b3f5c
11 changed files with 39 additions and 15 deletions

View file

@ -472,7 +472,6 @@ class Base_Object {
public function get( $key ) { public function get( $key ) {
if ( ! $this->has( $key ) ) { if ( ! $this->has( $key ) ) {
return new WP_Error( 'invalid_key', 'Invalid key' ); return new WP_Error( 'invalid_key', 'Invalid key' );
} }
return $this->$key; return $this->$key;

View file

@ -71,16 +71,16 @@ class Activitypub {
* @return string The new path to the JSON template. * @return string The new path to the JSON template.
*/ */
public static function render_json_template( $template ) { public static function render_json_template( $template ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $template;
}
if ( ! is_activitypub_request() ) { if ( ! is_activitypub_request() ) {
return $template; return $template;
} }
$json_template = false; $json_template = false;
if ( ! \is_author() && ! \is_singular() && ! \is_home() ) {
return $template;
}
// check if user can publish posts // check if user can publish posts
if ( \is_author() && ! User_Factory::get_by_id( \get_the_author_meta( 'ID' ) ) ) { if ( \is_author() && ! User_Factory::get_by_id( \get_the_author_meta( 'ID' ) ) ) {
return $template; return $template;

View file

@ -107,7 +107,7 @@ class Signature {
*/ */
public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) { public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) {
$user = User_Factory::get_by_id( $user_id ); $user = User_Factory::get_by_id( $user_id );
$key = $user->get_private_key(); $key = $user->get__private_key();
$url_parts = \wp_parse_url( $url ); $url_parts = \wp_parse_url( $url );

View file

@ -237,7 +237,7 @@ function is_activitypub_request() {
* ActivityPub requests are currently only made for * ActivityPub requests are currently only made for
* author archives, singular posts, and the homepage. * 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; return false;
} }

View file

@ -39,7 +39,7 @@ class Application_User extends Blog_User {
return $this::get_name(); return $this::get_name();
} }
public function get_public_key() { public function get__public_key() {
$key = \get_option( 'activitypub_application_user_public_key' ); $key = \get_option( 'activitypub_application_user_public_key' );
if ( $key ) { if ( $key ) {
@ -58,7 +58,7 @@ class Application_User extends Blog_User {
* *
* @return mixed * @return mixed
*/ */
public function get_private_key() { public function get__private_key() {
$key = \get_option( 'activitypub_application_user_private_key' ); $key = \get_option( 'activitypub_application_user_private_key' );
if ( $key ) { if ( $key ) {

View file

@ -150,7 +150,7 @@ class Blog_User extends User {
return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); 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' ); $key = \get_option( 'activitypub_blog_user_public_key' );
if ( $key ) { if ( $key ) {
@ -169,7 +169,7 @@ class Blog_User extends User {
* *
* @return mixed * @return mixed
*/ */
public function get_private_key() { public function get__private_key() {
$key = \get_option( 'activitypub_blog_user_private_key' ); $key = \get_option( 'activitypub_blog_user_private_key' );
if ( $key ) { if ( $key ) {
@ -193,4 +193,8 @@ class Blog_User extends User {
public function get_attachment() { public function get_attachment() {
return array(); return array();
} }
public function get_canonical_url() {
return \home_url();
}
} }

View file

@ -123,6 +123,19 @@ class User extends Actor {
} }
public function get_public_key() { 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 ); $key = \get_user_meta( $this->get__id(), 'magic_sig_public_key', true );
if ( $key ) { if ( $key ) {
@ -139,7 +152,7 @@ class User extends Actor {
* *
* @return mixed * @return mixed
*/ */
public function get_private_key() { public function get__private_key() {
$key = \get_user_meta( $this->get__id(), 'magic_sig_private_key', true ); $key = \get_user_meta( $this->get__id(), 'magic_sig_private_key', true );
if ( $key ) { if ( $key ) {
@ -242,4 +255,8 @@ class User extends Actor {
public function get_resource() { public function get_resource() {
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
} }
public function get_canonical_url() {
return $this->get_url();
}
} }

View file

@ -67,6 +67,10 @@ class Users {
*/ */
\do_action( 'activitypub_outbox_pre' ); \do_action( 'activitypub_outbox_pre' );
$user->set_context(
\Activitypub\Model\Activity::CONTEXT
);
$json = $user->to_array(); $json = $user->to_array();
$response = new WP_REST_Response( $json, 200 ); $response = new WP_REST_Response( $json, 200 );

View file

@ -55,7 +55,6 @@ class Webfinger {
$aliases = array( $aliases = array(
$user->get_url(), $user->get_url(),
$user->get_canonical_url(),
$user->get_at_url(), $user->get_at_url(),
); );

View file

@ -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 ); remove_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10 );
} }
public function test_dispatch_mentions() { public function test_dispatch_mentions() {
$post = \wp_insert_post( $post = \wp_insert_post(
array( array(

View file

@ -44,7 +44,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
$user = Activitypub\User_Factory::get_by_id( 1 ); $user = Activitypub\User_Factory::get_by_id( 1 );
$public_key = $user->get_public_key(); $public_key = $user->get__public_key();
// signature_verification // signature_verification
$verified = \openssl_verify( $signed_data, $signature_block['signature'], $public_key, 'rsa-sha256' ) > 0; $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', 'pre_get_remote_metadata_by_actor',
function( $json, $actor ) { function( $json, $actor ) {
$user = Activitypub\User_Factory::get_by_id( 1 ); $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 ActivityPub Profile with signature
return array( return array(
'id' => $actor, 'id' => $actor,