Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/MetaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SimpleSAML\Module\metarefresh;

use DateInterval;
use DateTimeImmutable;
use Exception;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
Expand Down Expand Up @@ -388,6 +390,26 @@ private function createContext(array $source): array
if (array_key_exists($source['src'], $this->state)) {
$sourceState = $this->state[$source['src']];

/*
* If an expireAfter value exists, we effectively altered the metadata's validUntil ourselves
* so we may need to refresh the metadata even if the source indicates it has not changed.
*/
if (isset($source['expireAfter']) && isset($sourceState['requested_at'])) {
$requestedAt = new DateTimeImmutable($sourceState['requested_at']);
$expiresAt = $requestedAt->add(new DateInterval('PT' . $source['expireAfter'] . 'S'));
$refreshThreshold = $expiresAt->sub(new DateInterval('PT1H')); // 1 hour based on default cron tags
$now = new DateTimeImmutable();
if ($now >= $refreshThreshold) {
Logger::info(sprintf(
'Cached metadata for %s expires soon - forcing refresh even if not changed',
$source['src'],
));
unset($sourceState['last-modified']);
unset($sourceState['etag']);
$rawheader .= 'Cache-Control: max-age=0' . "\r\n";
}
}

if (isset($sourceState['last-modified'])) {
$rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
}
Expand Down
7 changes: 6 additions & 1 deletion src/MetaRefresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function runRefresh(string $crontag = null): void
$source['whitelist'] = $whitelist;
}

# Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
// Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
if (isset($source['attributewhitelist'])) {
$source['attributewhitelist'] = array_merge($source['attributewhitelist'], $attributewhitelist);
} else {
Expand All @@ -123,6 +123,11 @@ public function runRefresh(string $crontag = null): void
$source['conditionalGET'] = $conditionalGET;
}

// make our cache expiry available to the loader if we're conditionally GETting
if ($source['conditionalGET'] && isset($expireAfter)) {
$source['expireAfter'] = $expireAfter;
}

Logger::debug('[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
$metaloader->loadSource($source);
}
Expand Down