-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-prepare.php
114 lines (107 loc) · 3.98 KB
/
publish-prepare.php
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
103
104
105
106
107
108
109
110
111
112
113
114
<?php
if (!defined('CORRECT_ENTRY')) {
http_response_code(500);
echo 'Internal server error';
exit();
}
if(!isset($post_data['timestamp']) || !is_string($post_data['timestamp']) ) {
http_response_code(403);
echo 'Bad post data';
exit();
}
$timestamp = $post_data['timestamp'];
$request_timestamp = spxp_parse_datetime($timestamp);
if(!$request_timestamp) {
http_response_code(403);
echo 'Invalid timestamp';
exit();
}
$request_age = $now->getTimestamp() - $request_timestamp->getTimestamp();
if($request_age < -1*60 || $request_age > 5*60) {
http_response_code(403);
echo 'Request timeout elapsed';
exit();
}
if(!isset($post_data['signature']) || !is_array($post_data['signature']) ||
!isset($post_data['signature']['key']) || !is_string($post_data['signature']['key']) ||
!isset($post_data['signature']['sig']) || !is_string($post_data['signature']['sig']) ) {
http_response_code(403);
echo 'Invalid signature';
exit();
}
$signature_key = $post_data['signature']['key'];
$signature_sig = $post_data['signature']['sig'];
if(!is_base64urlsafe($signature_key) || strlen($signature_key) < 5 || strlen($signature_key) > 50) {
http_response_code(403);
echo 'Invalid signature';
exit();
}
$publishKeyStatement = $pdo->prepare('SELECT rnd, data FROM profile_keys WHERE profile=? and grp=? and enc_with_grp=? and enc_with_rnd is null');
$publishKeyStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$publishKeyStatement->bindValue(2, $signature_key, PDO::PARAM_STR);
$publishKeyStatement->bindValue(3, '@publish@', PDO::PARAM_STR);
try {
$publishKeyStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$publishKey = $publishKeyStatement->fetch();
$publishKeyStatement->closeCursor();
if(!$publishKey) {
http_response_code(403);
echo 'Invalid signature';
exit();
}
$publishJwk = json_decode($publishKey['data'], true);
if(!isset($publishJwk) || !is_array($publishJwk) || !isset($publishJwk['x']) || !is_string($publishJwk['x'])) {
http_response_code(403);
echo 'Invalid signature';
exit();
}
unset($post_data['signature']);
ksort($post_data);
$signStr = json_encode($post_data, JSON_UNESCAPED_SLASHES);
$pub = base64url_decode($publishJwk['x']);
$sig = base64url_decode($signature_sig);
if(!sodium_crypto_sign_verify_detached($sig, $signStr, $pub)) {
http_response_code(403);
echo 'Invalid signature';
exit();
}
$token = generateRandomKeyIdLong();
$createStatement = $pdo->prepare('INSERT INTO `publish` (`profile`,`key_id`,`last`,`token`,`scope`) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE `last`=VALUES(`last`), `token`=VALUES(`token`), `scope`=VALUES(`scope`)');
$createStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$createStatement->bindValue(2, $signature_key, PDO::PARAM_STR);
$createStatement->bindValue(3, datetime_to_microtime($now), PDO::PARAM_INT);
$createStatement->bindValue(4, $token, PDO::PARAM_STR);
$createStatement->bindValue(5, $publishKey['rnd'], PDO::PARAM_STR);
try {
$createStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$response = array();
$response['token'] = $token;
if(isset($post_data['group']) && is_string($post_data['group']) ) {
$latestRoundStatement = $pdo->prepare('SELECT `rnd` FROM `profile_keys` WHERE `profile`=? and `grp`=? ORDER BY `published` desc LIMIT 1');
$latestRoundStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$latestRoundStatement->bindValue(2, $post_data['group'], PDO::PARAM_STR);
try {
$latestRoundStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$latestRound = $latestRoundStatement->fetch();
$latestRoundStatement->closeCursor();
if($latestRound) {
$response['groupRound'] = $latestRound['rnd'];
}
}
header('Content-Type: application/json');
echo json_encode($response);