-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistoryCosign.php
More file actions
102 lines (95 loc) · 3.06 KB
/
Copy pathHistoryCosign.php
File metadata and controls
102 lines (95 loc) · 3.06 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
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\RequestHandlers\Api;
use FediE2EE\PKD\Crypto\Exceptions\NotImplementedException;
use FediE2EE\PKDServer\Exceptions\{
CacheException,
DependencyException,
TableException
};
use FediE2EE\PKDServer\Meta\Route;
use FediE2EE\PKDServer\Tables\MerkleState;
use FediE2EE\PKDServer\Traits\ReqTrait;
use JsonException;
use Override;
use Psr\Http\Message\{
ResponseInterface,
ServerRequestInterface
};
use Psr\Http\Server\RequestHandlerInterface;
use SodiumException;
use Throwable;
use TypeError;
use function in_array, is_object, json_decode, property_exists;
class HistoryCosign implements RequestHandlerInterface
{
use ReqTrait;
protected MerkleState $merkleState;
/**
* @throws DependencyException
* @throws TableException
* @throws CacheException
*/
public function __construct()
{
$merkleState = $this->table('MerkleState');
if (!($merkleState instanceof MerkleState)) {
throw new TypeError('Could not load MerkleState table at runtime');
}
$this->merkleState = $merkleState;
}
/**
* @throws DependencyException
* @throws JsonException
* @throws NotImplementedException
* @throws SodiumException
*/
#[Override]
#[Route("/history/cosign/{hash}")]
public function handle(ServerRequestInterface $request): ResponseInterface
{
if ($request->getMethod() !== 'POST') {
return $this->error('This endpoint only responds to POST requests');
}
$merkleRoot = $request->getAttribute('hash') ?? '';
if (empty($merkleRoot)) {
return $this->error('No hash provided');
}
$contentTypes = $request->getHeader('Content-Type');
if (!in_array('application/json', $contentTypes, true)) {
return $this->error('Content-Type must be application/json');
}
$body = $request->getBody()->getContents();
if (empty($body)) {
return $this->error('Empty body provided');
}
$decoded = json_decode($body);
if (!is_object($decoded)) {
return $this->error('Invalid JSON request body');
}
if (!property_exists($decoded, 'witness')) {
return $this->error('Required property not found: witness');
}
if (!property_exists($decoded, 'cosigned')) {
return $this->error('Required property not found: cosigned');
}
try {
$status = $this->merkleState->addWitnessCosignature(
$decoded->witness,
$merkleRoot,
$decoded->cosigned
);
return $this->json([
'!pkd-context' => 'fedi-e2ee:v1/api/history/cosign',
'success' => $status,
'time' => $this->time(),
]);
} catch (Throwable $ex) {
$this->config()->getLogger()->error(
$ex->getMessage(),
$ex->getTrace()
);
return $this->error($ex->getMessage());
}
}
}