-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinger.php
More file actions
90 lines (83 loc) · 2.66 KB
/
Copy pathFinger.php
File metadata and controls
90 lines (83 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\RequestHandlers\ActivityPub;
use GuzzleHttp\Exception\GuzzleException;
use FediE2EE\PKD\Crypto\Exceptions\{
JsonException,
NetworkException,
NotImplementedException
};
use FediE2EE\PKDServer\{
Exceptions\DependencyException,
Meta\Route,
Traits\ActivityStreamsTrait,
Traits\ReqTrait
};
use JsonException as BaseJsonException;
use Override;
use ParagonIE\Certainty\Exception\CertaintyException;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\{
ServerRequestInterface,
ResponseInterface
};
use SodiumException;
use function array_key_exists, hash_equals, preg_match;
class Finger implements RequestHandlerInterface
{
use ActivityStreamsTrait;
use ReqTrait;
/**
* @throws BaseJsonException
* @throws CertaintyException
* @throws DependencyException
* @throws GuzzleException
* @throws NotImplementedException
* @throws SodiumException
*/
#[Route("/.well-known/webfinger")]
#[Override]
public function handle(ServerRequestInterface $request): ResponseInterface
{
$params = $request->getQueryParams();
if (!array_key_exists("resource", $params)) {
return $this->error('missing resource parameter', 400);
}
$resource = $params["resource"];
$matches = [];
if (!preg_match('#^acct:([^@]+)@(.+)$#', $resource, $matches)) {
return $this->error('invalid resource format', 400);
}
$user = $matches[1];
$domain = $matches[2];
$serverParams = $this->config()->getParams();
// Handle third-party lookups
if (!hash_equals($serverParams->hostname, $domain)) {
try {
return $this->json(
$this->webfinger($this->config()->getGuzzle())
->fetch($user . '@' . $domain)
);
} catch (NetworkException $e) {
return $this->error($e->getMessage());
}
}
// We only return a WebFinger for the valid configured user:
if (!hash_equals($serverParams->actorUsername, $user)) {
return $this->error('User not found', 404);
}
// Return the one valid user for this host:
$actorUrl = "https://{$domain}/users/{$user}";
return $this->json([
'subject' => "acct:{$user}@{$domain}",
'aliases' => [$actorUrl],
'links' => [
[
'rel' => 'self',
'type' => 'application/activity+json',
'href' => $actorUrl,
]
]
]);
}
}