Implement webfinger discovery

This commit is contained in:
Jeremy Dormitzer 2018-10-03 08:23:41 -04:00
parent f89dd048a4
commit 8359bce0be
3 changed files with 50 additions and 4 deletions

View File

@ -1,8 +1,6 @@
<?php
namespace actors;
define( 'PTEROTYPE_BLOG_ACTOR_SLUG', '-blog' );
function get_actor( $id ) {
global $wpdb;
$row = $wpdb->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( '/' ),
);

View File

@ -1,6 +1,8 @@
<?php
namespace webfinger;
require_once plugin_dir_path( __FILE__ ) . 'actors.php';
function generate_rewrite_rules( $wp_rewrite ) {
$dot_well_known = array(
'.well-known/webfinger' => '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;
}
?>

View File

@ -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' );