From 8359bce0bec7731cc9a6f6283bf3123279299785 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 3 Oct 2018 08:23:41 -0400 Subject: [PATCH] Implement webfinger discovery --- includes/server/actors.php | 4 +-- includes/server/webfinger.php | 48 ++++++++++++++++++++++++++++++++++- pterotype.php | 2 ++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/includes/server/actors.php b/includes/server/actors.php index 0daed00..c0d98ba 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -1,8 +1,6 @@ get_row( $wpdb->prepare( @@ -69,7 +67,7 @@ function get_blog_actor() { ), 'name' => get_bloginfo( 'name' ), // TODO in the future, make this configurable, both here and in the Webfinger handler - 'preferredUsername' => 'blog', + 'preferredUsername' => PTEROTYPE_BLOG_ACTOR_USERNAME, 'summary' => get_bloginfo( 'description' ), 'url' => network_site_url( '/' ), ); diff --git a/includes/server/webfinger.php b/includes/server/webfinger.php index e411010..4023383 100644 --- a/includes/server/webfinger.php +++ b/includes/server/webfinger.php @@ -1,6 +1,8 @@ 'index.php?well-known=webfinger' @@ -19,11 +21,55 @@ function parse_request( $req ) { function query_vars( $query_vars ) { $query_vars[] = 'well-known'; + $query_vars[] = 'resource'; return $query_vars; } function handle( $query ) { - echo var_dump( $query ); + if ( ! array_key_exists( 'resource', $query ) ) { + header( 'HTTP/1.1 400 Bad Request', true, 400 ); + echo __( 'Expected a "resource" parameter', 'pterotype' ); + exit; + } + $resource = $query['resource']; + $matches = array(); + $matched = preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches ); + if ( ! $matched ) { + header( 'HTTP/1.1 404 Not Found', true, 404 ); + echo __( 'Resource not found', 'pterotype' ); + exit; + } + $account_name = $matches[1]; + $account_host = $matches[2]; + if ( $account_host !== $_SERVER['HTTP_HOST'] ) { + header( 'HTTP/1.1 404 Not Found', true, 404 ); + echo __( 'Resource not found', 'pterotype' ); + exit; + } + if ( $account_name === PTEROTYPE_BLOG_ACTOR_USERNAME ) { + $account_name = PTEROTYPE_BLOG_ACTOR_SLUG; + } + get_webfinger_json( $resource, $account_name ); + exit; +} + +function get_webfinger_json( $resource, $actor_slug ) { + $actor = \actors\get_actor_by_slug( $actor_slug ); + if ( is_wp_error( $actor ) ) { + header( 'HTTP/1.1 404 Not Found', true, 404 ); + echo __( 'Resource not found', 'pterotype' ); + exit; + } + $json = array( + 'subject' => $resource, + 'links' => array( + 'rel' => 'self', + 'type' => 'application/activity+json', + 'href' => $actor['id'], + ), + ); + header( 'Content-Type: application/jrd+json', true ); + echo wp_json_encode( $json ); exit; } ?> diff --git a/pterotype.php b/pterotype.php index 1cb848a..1970b66 100644 --- a/pterotype.php +++ b/pterotype.php @@ -5,6 +5,8 @@ Plugin Name: Pterotype require_once plugin_dir_path( __FILE__ ) . 'includes/init.php'; define( 'PTEROTYPE_VERSION', '0.0.4' ); +define( 'PTEROTYPE_BLOG_ACTOR_SLUG', '-blog' ); +define( 'PTEROTYPE_BLOG_ACTOR_USERNAME', 'blog' ); function pterotype_init() { do_action( 'pterotype_init' );