2023-04-21 23:25:39 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-05-05 20:02:12 +02:00
|
|
|
use WP_REST_Response;
|
2023-04-21 23:25:39 +02:00
|
|
|
use Activitypub\Signature;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Server REST-Class
|
|
|
|
*
|
|
|
|
* @author Django Doucet
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#security-verification
|
|
|
|
*/
|
|
|
|
class Server {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-05-05 20:02:12 +02:00
|
|
|
\add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 );
|
|
|
|
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
|
|
|
'activitypub/1.0',
|
2023-05-05 20:09:12 +02:00
|
|
|
'/application',
|
2023-05-05 20:02:12 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-05-05 20:09:12 +02:00
|
|
|
'callback' => array( self::class, 'application_actor' ),
|
2023-05-05 20:02:12 +02:00
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-05 20:09:12 +02:00
|
|
|
* Render Application actor profile
|
2023-05-05 20:02:12 +02:00
|
|
|
*
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2023-05-05 20:09:12 +02:00
|
|
|
public static function application_actor() {
|
2023-05-05 20:02:12 +02:00
|
|
|
$json = new \stdClass();
|
|
|
|
|
|
|
|
$json->{'@context'} = \Activitypub\get_context();
|
2023-05-05 20:09:12 +02:00
|
|
|
$json->id = \get_rest_url( null, 'activitypub/1.0/application' );
|
2023-05-05 20:02:12 +02:00
|
|
|
$json->type = 'Application';
|
|
|
|
$json->preferredUsername = parse_url( get_site_url(), PHP_URL_HOST );
|
|
|
|
$json->name = get_bloginfo( 'name' );
|
2023-05-05 20:09:12 +02:00
|
|
|
$json->summary = "WordPress-ActivityPub application actor";
|
2023-05-05 20:02:12 +02:00
|
|
|
$json->manuallyApprovesFollowers = TRUE;
|
|
|
|
$json->icon = [ get_site_icon_url() ];
|
|
|
|
$json->publicKey = (object) array(
|
2023-05-05 20:09:12 +02:00
|
|
|
'id' => \get_rest_url( null, 'activitypub/1.0/application#main-key' ),
|
|
|
|
'owner' => \get_rest_url( null, 'activitypub/1.0/application' ),
|
2023-05-05 20:02:12 +02:00
|
|
|
'publicKeyPem' => Signature::get_public_key( -1 ),
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = new WP_REST_Response( $json, 200 );
|
|
|
|
|
|
|
|
$response->header( 'Content-Type', 'application/activity+json' );
|
|
|
|
|
|
|
|
return $response;
|
2023-04-21 23:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to authorize each api requests
|
|
|
|
*
|
|
|
|
* @see \WP_REST_Request
|
|
|
|
*
|
|
|
|
* @param $response
|
|
|
|
* @param $handler
|
|
|
|
* @param \WP_REST_Request $request
|
|
|
|
*
|
|
|
|
* @return mixed|\WP_Error
|
|
|
|
*/
|
|
|
|
public static function authorize_activitypub_requests( $response, $handler, $request ) {
|
|
|
|
|
|
|
|
$maybe_activitypub = $request->get_route();
|
|
|
|
if ( str_starts_with( $maybe_activitypub, '/activitypub' ) ) {
|
|
|
|
if ( 'POST' === $request->get_method() ) {
|
|
|
|
$verified_request = Signature::verify_http_signature( $request );
|
|
|
|
|
|
|
|
if ( \is_wp_error( $verified_request ) ) {
|
|
|
|
return $verified_request;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SecureMode/Authorized fetch.
|
2023-04-26 23:54:09 +02:00
|
|
|
// $secure_mode = \get_option( 'activitypub_use_secure_mode', '0' );
|
|
|
|
// if ( $secure_mode ) {
|
|
|
|
// $verified_request = Signature::verify_http_signature( $request );
|
|
|
|
// if ( \is_wp_error( $verified_request ) ) {
|
|
|
|
// return $verified_request;
|
|
|
|
// }
|
|
|
|
// }
|
2023-04-21 23:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|