PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-server.php

class-server.php in ActivityPub 1.0.0, at includes/rest/class-server.php

105 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Rest;
3
4 use stdClass;
5 use WP_REST_Response;
6 use Activitypub\Signature;
7 use Activitypub\Model\Application_User;
8
9 /**
10 * ActivityPub Server REST-Class
11 *
12 * @author Django Doucet
13 *
14 * @see https://www.w3.org/TR/activitypub/#security-verification
15 */
16 class Server {
17 /**
18 * Initialize the class, registering WordPress hooks
19 */
20 public static function init() {
21 \add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
22 \add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 );
23 }
24
25 /**
26 * Register routes
27 */
28 public static function register_routes() {
29 \register_rest_route(
30 ACTIVITYPUB_REST_NAMESPACE,
31 '/application',
32 array(
33 array(
34 'methods' => \WP_REST_Server::READABLE,
35 'callback' => array( self::class, 'application_actor' ),
36 'permission_callback' => '__return_true',
37 ),
38 )
39 );
40 }
41
42 /**
43 * Render Application actor profile
44 *
45 * @return WP_REST_Response The JSON profile of the Application Actor.
46 */
47 public static function application_actor() {
48 $user = new Application_User();
49
50 $user->set_context(
51 \Activitypub\Activity\Activity::CONTEXT
52 );
53
54 $json = $user->to_array();
55
56 $response = new WP_REST_Response( $json, 200 );
57
58 $response->header( 'Content-Type', 'application/activity+json' );
59
60 return $response;
61 }
62
63 /**
64 * Callback function to authorize each api requests
65 *
66 * @see WP_REST_Request
67 *
68 * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
69 * Usually a WP_REST_Response or WP_Error.
70 * @param array $handler Route handler used for the request.
71 * @param WP_REST_Request $request Request used to generate the response.
72 *
73 * @return mixed|WP_Error The response, error, or modified response.
74 */
75 public static function authorize_activitypub_requests( $response, $handler, $request ) {
76 $route = $request->get_route();
77
78 // check if it is an activitypub request and exclude webfinger and nodeinfo endpoints
79 if (
80 ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) ||
81 \str_starts_with( $route, '/' . \trailingslashit( ACTIVITYPUB_REST_NAMESPACE ) . 'webfinger' ) ||
82 \str_starts_with( $route, '/' . \trailingslashit( ACTIVITYPUB_REST_NAMESPACE ) . 'nodeinfo' )
83 ) {
84 return $response;
85 }
86
87 // POST-Requets are always signed
88 if ( 'post' === \strtolower( $request->get_method() ) ) {
89 $verified_request = Signature::verify_http_signature( $request );
90 if ( \is_wp_error( $verified_request ) ) {
91 return $verified_request;
92 }
93 } elseif ( 'get' === \strtolower( $request->get_method() ) ) { // GET-Requests are only signed in secure mode
94 if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
95 $verified_request = Signature::verify_http_signature( $request );
96 if ( \is_wp_error( $verified_request ) ) {
97 return $verified_request;
98 }
99 }
100 }
101
102 return $response;
103 }
104 }
105