Adding the VAPID keys

This commit is contained in:
Michael 2021-08-15 21:24:23 +00:00
parent d5e9253adb
commit 69f11c4a84
3 changed files with 37 additions and 5 deletions

View file

@ -36,6 +36,6 @@ class Subscription extends BaseFactory
public function createForApplicationIdAndUserId(int $applicationid, int $uid): \Friendica\Object\Api\Mastodon\Subscription public function createForApplicationIdAndUserId(int $applicationid, int $uid): \Friendica\Object\Api\Mastodon\Subscription
{ {
$subscription = DBA::selectFirst('subscription', [], ['application-id' => $applicationid, 'uid' => $uid]); $subscription = DBA::selectFirst('subscription', [], ['application-id' => $applicationid, 'uid' => $uid]);
return new \Friendica\Object\Api\Mastodon\Subscription($subscription, ModelSubscription::getVapidKey()); return new \Friendica\Object\Api\Mastodon\Subscription($subscription, ModelSubscription::getPublicVapidKey());
} }
} }

View file

@ -100,20 +100,42 @@ class Subscription
} }
/** /**
* Fetch a VAPID key * Fetch a VAPID keypair
* *
* @return string * @return array
*/ */
public static function getVapidKey(): string private static function getKeyPair(): array
{ {
$keypair = DI::config()->get('system', 'ec_keypair'); $keypair = DI::config()->get('system', 'ec_keypair');
if (empty($keypair)) { if (empty($keypair)) {
$keypair = Crypto::newECKeypair(); $keypair = Crypto::newECKeypair();
DI::config()->set('system', 'ec_keypair', $keypair); DI::config()->set('system', 'ec_keypair', $keypair);
} }
return $keypair;
}
/**
* Fetch the public VAPID key
*
* @return string
*/
public static function getPublicVapidKey(): string
{
$keypair = self::getKeyPair();
return $keypair['vapid-public']; return $keypair['vapid-public'];
} }
/**
* Fetch the public VAPID key
*
* @return string
*/
public static function getPrivateVapidKey(): string
{
$keypair = self::getKeyPair();
return $keypair['vapid-private'];
}
/** /**
* Prepare push notification * Prepare push notification
* *

View file

@ -23,6 +23,8 @@ namespace Friendica\Worker;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Subscription as ModelSubscription;
use Minishlink\WebPush\WebPush; use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription; use Minishlink\WebPush\Subscription;
@ -41,7 +43,15 @@ class PushSubscription
'payload' => null, 'payload' => null,
]; ];
$webPush = new WebPush(); $auth = [
'VAPID' => [
'subject' => DI::baseUrl()->getHostname(),
'publicKey' => ModelSubscription::getPublicVapidKey(),
'privateKey' => ModelSubscription::getPrivateVapidKey(),
],
];
$webPush = new WebPush($auth);
$report = $webPush->sendOneNotification( $report = $webPush->sendOneNotification(
$notification['subscription'], $notification['subscription'],