Skip to content

Commit f973ece

Browse files
[Mastodon] support Secure Mode instances
1 parent c0ae90e commit f973ece

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

bridges/MastodonBridge.php

+29-6
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class MastodonBridge extends BridgeAbstract {
4545
)
4646
));
4747

48-
const AP_HEADER = array(
49-
'Accept: application/activity+json'
50-
);
51-
5248
public function getName() {
5349
if($this->getInput('canusername')) {
5450
return $this->getInput('canusername');
@@ -89,7 +85,7 @@ public function getURI(){
8985

9086
public function collectData() {
9187
$url = $this->getURI() . '/outbox?page=true';
92-
$content = json_decode(getContents($url, self::AP_HEADER), true);
88+
$content = $this->fetchAP($url);
9389
if ($content['id'] === $url) {
9490
foreach ($content['orderedItems'] as $status) {
9591
$this->items[] = $this->parseItem($status);
@@ -112,7 +108,7 @@ protected function parseItem($content) {
112108
$rtUser = $this->loadCacheValue($rtContent['attributedTo'], 86400);
113109
if (!isset($rtUser)) {
114110
// We fetch the author, since we cannot always assume the format of the URL.
115-
$user = json_decode(getContents($rtContent['attributedTo'], self::AP_HEADER), true);
111+
$user = $this->fetchAP($rtContent['attributedTo']);
116112
preg_match('/https?:\/\/([a-z0-9-\.]{0,})\//', $rtContent['attributedTo'], $matches);
117113
// We assume that the server name as indicated by the path is the actual server name,
118114
// since using webfinger to delegate domains is not officially supported, and it only
@@ -168,4 +164,31 @@ protected function parseObject($object, $item) {
168164
}
169165
return $item;
170166
}
167+
168+
protected function fetchAP($url) {
169+
$d = new DateTime();
170+
$d->setTimezone(new DateTimeZone('GMT'));
171+
$date = $d->format('D, d M Y H:i:s e');
172+
preg_match('/https?:\/\/([a-z0-9-\.]{0,})(\/[^?#]+)/', $url, $matches);
173+
$headers = array(
174+
'Accept: application/activity+json',
175+
'Host: ' . $matches[1],
176+
'Date: ' . $date
177+
);
178+
$privateKey = $this->getOption('private_key');
179+
$keyId = $this->getOption('key_id');
180+
if ($privateKey && $keyId) {
181+
$pkey = openssl_pkey_get_private('file://' . $privateKey);
182+
$toSign = '(request-target): get ' . $matches[2] . "\nhost: " . $matches[1] . "\ndate: " . $date;
183+
$result = openssl_sign($toSign, $signature, $pkey, 'RSA-SHA256');
184+
if ($result) {
185+
Debug::log($toSign);
186+
$sig = 'Signature: keyId="' . $keyId . '",headers="(request-target) host date",signature="' .
187+
base64_encode($signature) . '"';
188+
Debug::log($sig);
189+
array_push($headers, $sig);
190+
}
191+
}
192+
return json_decode(getContents($url, $headers), true);
193+
}
171194
}

0 commit comments

Comments
 (0)