Expire each authorization after 60 minutes.

This commit is contained in:
CalDescent 2022-01-02 20:11:38 +00:00
parent c4a782301d
commit 5157ccf7c0
2 changed files with 45 additions and 12 deletions

View File

@ -478,6 +478,7 @@ public class Controller extends Thread {
ArbitraryDataBuildManager.getInstance().start(); ArbitraryDataBuildManager.getInstance().start();
ArbitraryDataCleanupManager.getInstance().start(); ArbitraryDataCleanupManager.getInstance().start();
ArbitraryDataStorageManager.getInstance().start(); ArbitraryDataStorageManager.getInstance().start();
ArbitraryDataRenderManager.getInstance().start();
// Auto-update service? // Auto-update service?
if (Settings.getInstance().isAutoUpdateEnabled()) { if (Settings.getInstance().isAutoUpdateEnabled()) {
@ -1069,6 +1070,7 @@ public class Controller extends Thread {
ArbitraryDataBuildManager.getInstance().shutdown(); ArbitraryDataBuildManager.getInstance().shutdown();
ArbitraryDataCleanupManager.getInstance().shutdown(); ArbitraryDataCleanupManager.getInstance().shutdown();
ArbitraryDataStorageManager.getInstance().shutdown(); ArbitraryDataStorageManager.getInstance().shutdown();
ArbitraryDataRenderManager.getInstance().shutdown();
if (blockMinter != null) { if (blockMinter != null) {
LOGGER.info("Shutting down block minter"); LOGGER.info("Shutting down block minter");

View File

@ -1,20 +1,22 @@
package org.qortal.controller.arbitrary; package org.qortal.controller.arbitrary;
import org.qortal.arbitrary.ArbitraryDataResource; import org.qortal.arbitrary.ArbitraryDataResource;
import org.qortal.utils.NTP;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class ArbitraryDataRenderManager { public class ArbitraryDataRenderManager extends Thread {
private static ArbitraryDataRenderManager instance; private static ArbitraryDataRenderManager instance;
private volatile boolean isStopping = false;
/** /**
* List to keep track of authorized resources for rendering. * Map to keep track of authorized resources for rendering.
* Keyed by resource ID, with the authorization time as the value.
*/ */
private List<ArbitraryDataResource> authorizedResources = Collections.synchronizedList(new ArrayList<>()); private Map<String, Long> authorizedResources = Collections.synchronizedMap(new HashMap<>());
private static long AUTHORIZATION_TIMEOUT = 60 * 60 * 1000L; // 1 hour
public ArbitraryDataRenderManager() { public ArbitraryDataRenderManager() {
@ -28,17 +30,46 @@ public class ArbitraryDataRenderManager {
return instance; return instance;
} }
@Override
public void run() {
Thread.currentThread().setName("Arbitrary Data Manager");
try {
while (!isStopping) {
Thread.sleep(60000);
Long now = NTP.getTime();
this.cleanup(now);
}
} catch (InterruptedException e) {
// Fall-through to exit thread...
}
}
public void shutdown() {
isStopping = true;
this.interrupt();
}
public void cleanup(Long now) {
if (now == null) {
return;
}
final long minimumTimestamp = now - AUTHORIZATION_TIMEOUT;
this.authorizedResources.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue() < minimumTimestamp);
}
public boolean isAuthorized(ArbitraryDataResource resource) { public boolean isAuthorized(ArbitraryDataResource resource) {
ArbitraryDataResource broadResource = new ArbitraryDataResource(resource.getResourceId(), null, null, null); ArbitraryDataResource broadResource = new ArbitraryDataResource(resource.getResourceId(), null, null, null);
for (ArbitraryDataResource authorizedResource : this.authorizedResources) { for (String authorizedResourceKey : this.authorizedResources.keySet()) {
if (authorizedResource != null && resource != null) { if (authorizedResourceKey != null && resource != null) {
// Check for exact match // Check for exact match
if (Objects.equals(authorizedResource.getUniqueKey(), resource.getUniqueKey())) { if (Objects.equals(authorizedResourceKey, resource.getUniqueKey())) {
return true; return true;
} }
// Check for a broad authorization (which applies to all services and identifiers under an authorized name) // Check for a broad authorization (which applies to all services and identifiers under an authorized name)
if (Objects.equals(authorizedResource.getUniqueKey(), broadResource.getUniqueKey())) { if (Objects.equals(authorizedResourceKey, broadResource.getUniqueKey())) {
return true; return true;
} }
} }
@ -48,7 +79,7 @@ public class ArbitraryDataRenderManager {
public void addToAuthorizedResources(ArbitraryDataResource resource) { public void addToAuthorizedResources(ArbitraryDataResource resource) {
if (!this.isAuthorized(resource)) { if (!this.isAuthorized(resource)) {
this.authorizedResources.add(resource); this.authorizedResources.put(resource.getUniqueKey(), NTP.getTime());
} }
} }