Skip to content

Commit 24194f7

Browse files
committed
Update FederationController, add webfinger support for actor uri. Fixes #5068
1 parent 8177ed1 commit 24194f7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

app/Http/Controllers/FederationController.php

+34-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ public function webfinger(Request $request)
4545
$resource = $request->input('resource');
4646
$domain = config('pixelfed.domain.app');
4747

48-
if (config('federation.activitypub.sharedInbox') &&
49-
$resource == 'acct:'.$domain.'@'.$domain) {
48+
// Instance Actor
49+
if (
50+
config('federation.activitypub.sharedInbox') &&
51+
$resource == 'acct:'.$domain.'@'.$domain
52+
) {
5053
$res = [
5154
'subject' => 'acct:'.$domain.'@'.$domain,
5255
'aliases' => [
@@ -68,6 +71,33 @@ public function webfinger(Request $request)
6871

6972
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
7073
}
74+
75+
if(str_starts_with($resource, 'https://')) {
76+
if(str_starts_with($resource, 'https://' . $domain . '/users/')) {
77+
$username = str_replace('https://' . $domain . '/users/', '', $resource);
78+
if(strlen($username) > 15) {
79+
return response('', 400);
80+
}
81+
$stripped = str_replace(['_', '.', '-'], '', $username);
82+
if(!ctype_alnum($stripped)) {
83+
return response('', 400);
84+
}
85+
$key = 'federation:webfinger:sha256:url-username:'.$username;
86+
if ($cached = Cache::get($key)) {
87+
return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
88+
}
89+
$profile = Profile::whereUsername($username)->first();
90+
if (! $profile || $profile->status !== null || $profile->domain) {
91+
return response('', 400);
92+
}
93+
$webfinger = (new Webfinger($profile))->generate();
94+
Cache::put($key, $webfinger, 1209600);
95+
return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
96+
->header('Access-Control-Allow-Origin', '*');
97+
} else {
98+
return response('', 400);
99+
}
100+
}
71101
$hash = hash('sha256', $resource);
72102
$key = 'federation:webfinger:sha256:'.$hash;
73103
if ($cached = Cache::get($key)) {
@@ -81,8 +111,8 @@ public function webfinger(Request $request)
81111
return response('', 400);
82112
}
83113
$username = $parsed['username'];
84-
$profile = Profile::whereNull('domain')->whereUsername($username)->first();
85-
if (! $profile || $profile->status !== null) {
114+
$profile = Profile::whereUsername($username)->first();
115+
if (! $profile || $profile->status !== null || $profile->domain) {
86116
return response('', 400);
87117
}
88118
$webfinger = (new Webfinger($profile))->generate();

0 commit comments

Comments
 (0)