| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_REST_Response; |
| 6 |
use Activitypub\Collection\Users as User_Collection; |
| 7 |
|
| 8 |
/** |
| 9 |
* ActivityPub WebFinger REST-Class |
| 10 |
* |
| 11 |
* @author Matthias Pfefferle |
| 12 |
* |
| 13 |
* @see https://webfinger.net/ |
| 14 |
*/ |
| 15 |
class Webfinger { |
| 16 |
/** |
| 17 |
* Initialize the class, registering WordPress hooks. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); |
| 23 |
\add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 10, 3 ); |
| 24 |
\add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 99, 2 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register routes. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function register_routes() { |
| 33 |
\register_rest_route( |
| 34 |
ACTIVITYPUB_REST_NAMESPACE, |
| 35 |
'/webfinger', |
| 36 |
array( |
| 37 |
array( |
| 38 |
'methods' => \WP_REST_Server::READABLE, |
| 39 |
'callback' => array( self::class, 'webfinger' ), |
| 40 |
'args' => self::request_parameters(), |
| 41 |
'permission_callback' => '__return_true', |
| 42 |
), |
| 43 |
) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* WebFinger endpoint. |
| 49 |
* |
| 50 |
* @param WP_REST_Request $request The request object. |
| 51 |
* |
| 52 |
* @return WP_REST_Response The response object. |
| 53 |
*/ |
| 54 |
public static function webfinger( $request ) { |
| 55 |
/* |
| 56 |
* Action triggerd prior to the ActivityPub profile being created and sent to the client |
| 57 |
*/ |
| 58 |
\do_action( 'activitypub_rest_webfinger_pre' ); |
| 59 |
|
| 60 |
$resource = $request->get_param( 'resource' ); |
| 61 |
$response = self::get_profile( $resource ); |
| 62 |
|
| 63 |
return new WP_REST_Response( $response, 200 ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* The supported parameters |
| 68 |
* |
| 69 |
* @return array list of parameters |
| 70 |
*/ |
| 71 |
public static function request_parameters() { |
| 72 |
$params = array(); |
| 73 |
|
| 74 |
$params['resource'] = array( |
| 75 |
'required' => true, |
| 76 |
'type' => 'string', |
| 77 |
'pattern' => '^acct:(.+)@(.+)$', |
| 78 |
); |
| 79 |
|
| 80 |
return $params; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Add WebFinger discovery links |
| 85 |
* |
| 86 |
* @param array $array the jrd array |
| 87 |
* @param string $resource the WebFinger resource |
| 88 |
* @param WP_User $user the WordPress user |
| 89 |
* |
| 90 |
* @return array the jrd array |
| 91 |
*/ |
| 92 |
public static function add_user_discovery( $array, $resource, $user ) { |
| 93 |
$user = User_Collection::get_by_id( $user->ID ); |
| 94 |
|
| 95 |
$array['links'][] = array( |
| 96 |
'rel' => 'self', |
| 97 |
'type' => 'application/activity+json', |
| 98 |
'href' => $user->get_url(), |
| 99 |
); |
| 100 |
|
| 101 |
return $array; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add WebFinger discovery links |
| 106 |
* |
| 107 |
* @param array $array the jrd array |
| 108 |
* @param string $resource the WebFinger resource |
| 109 |
* @param WP_User $user the WordPress user |
| 110 |
* |
| 111 |
* @return array the jrd array |
| 112 |
*/ |
| 113 |
public static function add_pseudo_user_discovery( $array, $resource ) { |
| 114 |
if ( $array ) { |
| 115 |
return $array; |
| 116 |
} |
| 117 |
|
| 118 |
return self::get_profile( $resource ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get the WebFinger profile. |
| 123 |
* |
| 124 |
* @param string $resource the WebFinger resource. |
| 125 |
* |
| 126 |
* @return array the WebFinger profile. |
| 127 |
*/ |
| 128 |
public static function get_profile( $resource ) { |
| 129 |
$user = User_Collection::get_by_resource( $resource ); |
| 130 |
|
| 131 |
if ( is_wp_error( $user ) ) { |
| 132 |
return $user; |
| 133 |
} |
| 134 |
|
| 135 |
$aliases = array( |
| 136 |
$user->get_url(), |
| 137 |
); |
| 138 |
|
| 139 |
$profile = array( |
| 140 |
'subject' => $resource, |
| 141 |
'aliases' => array_values( array_unique( $aliases ) ), |
| 142 |
'links' => array( |
| 143 |
array( |
| 144 |
'rel' => 'self', |
| 145 |
'type' => 'application/activity+json', |
| 146 |
'href' => $user->get_url(), |
| 147 |
), |
| 148 |
array( |
| 149 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 150 |
'type' => 'text/html', |
| 151 |
'href' => $user->get_url(), |
| 152 |
), |
| 153 |
), |
| 154 |
); |
| 155 |
|
| 156 |
return $profile; |
| 157 |
} |
| 158 |
} |
| 159 |
|