diff --git a/lib/Push.php b/lib/Push.php index ad9c2a3d..620b1e68 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -30,6 +30,7 @@ use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; +use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IUserStatus; use OCP\Util; @@ -81,6 +82,7 @@ public function __construct( protected IUserStatusManager $userStatusManager, protected IFactory $l10nFactory, protected ITimeFactory $timeFactory, + protected ISecureRandom $random, protected LoggerInterface $log, ) { $this->cache = $cacheFactory->createDistributed('pushtokens'); @@ -426,7 +428,11 @@ protected function sendNotificationsToProxies(): void { if ($subscriptionAwareServer === 'https://push-notifications.nextcloud.com') { $subscriptionKey = $this->config->getAppValue('support', 'subscription_key'); } else { - $subscriptionKey = $this->config->getSystemValueString('instanceid'); + $subscriptionKey = $this->config->getAppValue(Application::APP_ID, 'push_subscription_key'); + if ($subscriptionKey === '') { + $subscriptionKey = $this->createPushSubscriptionKey(); + $this->config->setAppValue(Application::APP_ID, 'push_subscription_key', $subscriptionKey); + } } $client = $this->clientService->newClient(); @@ -733,4 +739,9 @@ protected function deletePushTokenByDeviceIdentifier(string $deviceIdentifier): protected function createFakeUserObject(string $userId): IUser { return new FakeUser($userId); } + + protected function createPushSubscriptionKey(): string { + $key = $this->random->generate(25, ISecureRandom::CHAR_ALPHANUMERIC); + return implode('-', str_split($key, 5)); + } } diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index 12f0e84c..6df9870f 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -28,6 +28,7 @@ use OCP\L10N\IFactory; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\ResponseInterface; @@ -53,6 +54,7 @@ class PushTest extends TestCase { protected IUserStatusManager&MockObject $userStatusManager; protected IFactory&MockObject $l10nFactory; protected ITimeFactory&MockObject $timeFactory; + protected ISecureRandom&MockObject $random; protected LoggerInterface&MockObject $logger; protected function setUp(): void { @@ -69,6 +71,7 @@ protected function setUp(): void { $this->userStatusManager = $this->createMock(IUserStatusManager::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->random = $this->createMock(ISecureRandom::class); $this->logger = $this->createMock(LoggerInterface::class); $this->cacheFactory->method('createDistributed') @@ -93,6 +96,7 @@ protected function getPush(array $methods = []): Push|MockObject { $this->userStatusManager, $this->l10nFactory, $this->timeFactory, + $this->random, $this->logger, ]) ->onlyMethods($methods) @@ -110,6 +114,7 @@ protected function getPush(array $methods = []): Push|MockObject { $this->userStatusManager, $this->l10nFactory, $this->timeFactory, + $this->random, $this->logger, ); }