Skip to content

Commit

Permalink
Merge pull request #2117 from nextcloud/feat/noid/output-even-when-ca…
Browse files Browse the repository at this point in the history
…ched

feat(occ): Output token validity even when age is cached
  • Loading branch information
nickvergessen authored Dec 11, 2024
2 parents 4e7c8a2 + b5d4e47 commit da6d87a
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
foreach ($devices as $device) {
$device['token'] = (int)$device['token'];
$this->printInfo('');
$this->printInfo('Device token:' . $device['token']);
$this->printInfo('Device token: ' . $device['token']);

if (!$this->validateToken($device['token'], $maxAge)) {
// Token does not exist anymore
Expand Down Expand Up @@ -530,27 +530,29 @@ protected function sendNotificationsToProxies(): void {

protected function validateToken(int $tokenId, int $maxAge): bool {
$age = $this->cache->get('t' . $tokenId);
if ($age !== null) {
return $age > $maxAge;
}

try {
// Check if the token is still valid...
$token = $this->tokenProvider->getTokenById($tokenId);
$this->cache->set('t' . $tokenId, $token->getLastCheck(), 600);
if ($token->getLastCheck() > $maxAge) {
$this->printInfo('Device token is valid');
} else {
$this->printInfo('Device token "last checked" is older than 60 days: ' . $token->getLastCheck());
if ($age === null) {
try {
// Check if the token is still valid...
$token = $this->tokenProvider->getTokenById($tokenId);
$this->cache->set('t' . $tokenId, $token->getLastCheck(), 600);
$age = $token->getLastCheck();
} catch (InvalidTokenException) {
// Token does not exist anymore, should drop the push device entry
$this->printInfo('InvalidTokenException is thrown');
$this->deletePushToken($tokenId);
$this->cache->set('t' . $tokenId, 0, 600);
return false;
}
return $token->getLastCheck() > $maxAge;
} catch (InvalidTokenException) {
// Token does not exist anymore, should drop the push device entry
$this->printInfo('InvalidTokenException is thrown');
$this->deletePushToken($tokenId);
$this->cache->set('t' . $tokenId, 0, 600);
return false;
}

if ($age > $maxAge) {
$this->printInfo('Device token is valid');
return true;
}

$this->printInfo('Device token "last checked" is older than 60 days: ' . $age);
return false;
}

/**
Expand Down

0 comments on commit da6d87a

Please sign in to comment.