Update FederationController, add webfinger support for actor uri. Fixes #5068

This commit is contained in:
Daniel Supernault 2024-05-22 23:55:39 -06:00
parent 8177ed11ea
commit 24194f7d16
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -45,8 +45,11 @@ class FederationController extends Controller
$resource = $request->input('resource');
$domain = config('pixelfed.domain.app');
if (config('federation.activitypub.sharedInbox') &&
$resource == 'acct:'.$domain.'@'.$domain) {
// Instance Actor
if (
config('federation.activitypub.sharedInbox') &&
$resource == 'acct:'.$domain.'@'.$domain
) {
$res = [
'subject' => 'acct:'.$domain.'@'.$domain,
'aliases' => [
@ -68,6 +71,33 @@ class FederationController extends Controller
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
}
if(str_starts_with($resource, 'https://')) {
if(str_starts_with($resource, 'https://' . $domain . '/users/')) {
$username = str_replace('https://' . $domain . '/users/', '', $resource);
if(strlen($username) > 15) {
return response('', 400);
}
$stripped = str_replace(['_', '.', '-'], '', $username);
if(!ctype_alnum($stripped)) {
return response('', 400);
}
$key = 'federation:webfinger:sha256:url-username:'.$username;
if ($cached = Cache::get($key)) {
return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
}
$profile = Profile::whereUsername($username)->first();
if (! $profile || $profile->status !== null || $profile->domain) {
return response('', 400);
}
$webfinger = (new Webfinger($profile))->generate();
Cache::put($key, $webfinger, 1209600);
return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
->header('Access-Control-Allow-Origin', '*');
} else {
return response('', 400);
}
}
$hash = hash('sha256', $resource);
$key = 'federation:webfinger:sha256:'.$hash;
if ($cached = Cache::get($key)) {
@ -81,8 +111,8 @@ class FederationController extends Controller
return response('', 400);
}
$username = $parsed['username'];
$profile = Profile::whereNull('domain')->whereUsername($username)->first();
if (! $profile || $profile->status !== null) {
$profile = Profile::whereUsername($username)->first();
if (! $profile || $profile->status !== null || $profile->domain) {
return response('', 400);
}
$webfinger = (new Webfinger($profile))->generate();