time expiration private requests

This commit is contained in:
legop3
2026-06-01 20:52:58 -04:00
parent af5ddfb11d
commit 5e3a6a2851
6 changed files with 84 additions and 7 deletions
@@ -12,6 +12,7 @@ const { getSocketIp, normalizeIp } = require('../../helpers/ipResolver');
const { normalizeCookieUserId } = require('../identityService'); const { normalizeCookieUserId } = require('../identityService');
const { const {
REQUEST_COOLDOWN_MS, REQUEST_COOLDOWN_MS,
GRANT_TTL_MS,
requestEvents, requestEvents,
pendingRequests, pendingRequests,
pendingByRequesterRover, pendingByRequesterRover,
@@ -28,6 +29,30 @@ const {
getSocketByRequesterKey, getSocketByRequesterKey,
} = require('./helpers'); } = require('./helpers');
function isGrantExpired(grant, now = Date.now()) {
const expiresAt = Number(grant?.expiresAt || 0);
return expiresAt > 0 && expiresAt <= now;
}
function pruneExpiredGrants(now = Date.now()) {
let removed = 0;
for (const [key, grant] of grants.entries()) {
if (!isGrantExpired(grant, now)) continue;
grants.delete(key);
removed += 1;
}
return removed;
}
function pruneExpiredGrantsAndRefresh(reason = 'grant_expired') {
const removed = pruneExpiredGrants();
if (!removed) return 0;
refreshAllSocketGrantCaches();
roverManager.broadcastRoster();
requestEvents.emit('change', { reason, expiredGrants: removed });
return removed;
}
function listPendingForRequester(socket) { function listPendingForRequester(socket) {
const requesterKey = buildRequesterKey(socket); const requesterKey = buildRequesterKey(socket);
const pending = []; const pending = [];
@@ -47,6 +72,7 @@ function listPendingForRequester(socket) {
} }
function listGrantedRoversForRequester(requesterKey) { function listGrantedRoversForRequester(requesterKey) {
pruneExpiredGrants();
const key = normalizeRequesterKey(requesterKey); const key = normalizeRequesterKey(requesterKey);
if (!key) return []; if (!key) return [];
const roverIds = []; const roverIds = [];
@@ -70,7 +96,14 @@ function refreshAllSocketGrantCaches() {
function getGrantForRequester(requesterKey, roverId) { function getGrantForRequester(requesterKey, roverId) {
if (!requesterKey || !roverId) return null; if (!requesterKey || !roverId) return null;
return grants.get(buildGrantKey(requesterKey, roverId)) || null; const grantKey = buildGrantKey(requesterKey, roverId);
const grant = grants.get(grantKey) || null;
if (!grant) return null;
if (isGrantExpired(grant)) {
grants.delete(grantKey);
return null;
}
return grant;
} }
function hasClosedPrivateAccessForSocket(socket, roverId) { function hasClosedPrivateAccessForSocket(socket, roverId) {
@@ -79,11 +112,17 @@ function hasClosedPrivateAccessForSocket(socket, roverId) {
} }
function getStateForSocket(socket) { function getStateForSocket(socket) {
pruneExpiredGrants();
const requesterKey = buildRequesterKey(socket); const requesterKey = buildRequesterKey(socket);
const grantedRovers = []; const grantedRovers = [];
for (const grant of grants.values()) { for (const grant of grants.values()) {
if (grant.requesterKey !== requesterKey) continue; if (grant.requesterKey !== requesterKey) continue;
grantedRovers.push({ roverId: grant.roverId, grantedAt: grant.grantedAt, requestId: grant.requestId || null }); grantedRovers.push({
roverId: grant.roverId,
grantedAt: grant.grantedAt,
expiresAt: grant.expiresAt || null,
requestId: grant.requestId || null,
});
} }
grantedRovers.sort((a, b) => b.grantedAt - a.grantedAt); grantedRovers.sort((a, b) => b.grantedAt - a.grantedAt);
return { return {
@@ -181,11 +220,13 @@ function approveRequest(requestId, actorDiscordId = null) {
pendingByRequesterRover.delete(`${request.requesterKey}:${request.roverId}`); pendingByRequesterRover.delete(`${request.requesterKey}:${request.roverId}`);
const grantKey = buildGrantKey(request.requesterKey, request.roverId); const grantKey = buildGrantKey(request.requesterKey, request.roverId);
const grantedAt = Date.now();
grants.set(grantKey, { grants.set(grantKey, {
requesterKey: request.requesterKey, requesterKey: request.requesterKey,
roverId: request.roverId, roverId: request.roverId,
requestId: request.id, requestId: request.id,
grantedAt: Date.now(), grantedAt,
expiresAt: grantedAt + GRANT_TTL_MS,
grantedBy: request.resolvedBy, grantedBy: request.resolvedBy,
}); });
@@ -217,6 +258,7 @@ function approveRequest(requestId, actorDiscordId = null) {
requesterKey: request.requesterKey, requesterKey: request.requesterKey,
resolvedBy: request.resolvedBy, resolvedBy: request.resolvedBy,
resolvedAt: request.resolvedAt, resolvedAt: request.resolvedAt,
grantExpiresAt: grantedAt + GRANT_TTL_MS,
assignedSocketId, assignedSocketId,
}, },
}); });
@@ -317,5 +359,6 @@ module.exports = {
denyRequest, denyRequest,
applySocketGrantCache, applySocketGrantCache,
refreshAllSocketGrantCaches, refreshAllSocketGrantCaches,
pruneExpiredGrantsAndRefresh,
clearPendingForRover, clearPendingForRover,
}; };
@@ -5,8 +5,26 @@ const io = require('../../globals/io');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const { requestEvents, grants } = require('./state'); const { requestEvents, grants } = require('./state');
const GRANT_PRUNE_INTERVAL_MS = 60 * 1000;
let grantPruneTimer = null;
function registerPrivateRoverAccessHooks(deps) { function registerPrivateRoverAccessHooks(deps) {
const { applySocketGrantCache, refreshAllSocketGrantCaches, createRequest, clearPendingForRover } = deps; const {
applySocketGrantCache,
refreshAllSocketGrantCaches,
pruneExpiredGrantsAndRefresh,
createRequest,
clearPendingForRover,
} = deps;
if (!grantPruneTimer) {
grantPruneTimer = setInterval(() => {
pruneExpiredGrantsAndRefresh('grant_expired');
}, GRANT_PRUNE_INTERVAL_MS);
if (typeof grantPruneTimer.unref === 'function') {
grantPruneTimer.unref();
}
}
roverManager.managerEvents.on('private', ({ roverId, open } = {}) => { roverManager.managerEvents.on('private', ({ roverId, open } = {}) => {
if (!roverId) return; if (!roverId) return;
@@ -12,6 +12,7 @@ const {
denyRequest, denyRequest,
applySocketGrantCache, applySocketGrantCache,
refreshAllSocketGrantCaches, refreshAllSocketGrantCaches,
pruneExpiredGrantsAndRefresh,
clearPendingForRover, clearPendingForRover,
} = require('./core'); } = require('./core');
const { registerPrivateRoverAccessHooks } = require('./hooks'); const { registerPrivateRoverAccessHooks } = require('./hooks');
@@ -19,6 +20,7 @@ const { registerPrivateRoverAccessHooks } = require('./hooks');
registerPrivateRoverAccessHooks({ registerPrivateRoverAccessHooks({
applySocketGrantCache, applySocketGrantCache,
refreshAllSocketGrantCaches, refreshAllSocketGrantCaches,
pruneExpiredGrantsAndRefresh,
createRequest, createRequest,
clearPendingForRover, clearPendingForRover,
}); });
@@ -4,6 +4,7 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const REQUEST_COOLDOWN_MS = 15 * 1000; const REQUEST_COOLDOWN_MS = 15 * 1000;
const GRANT_TTL_MS = 60 * 60 * 1000;
const DM_APPROVE_EMOJI = '✅'; const DM_APPROVE_EMOJI = '✅';
const DM_DENY_EMOJI = '❌'; const DM_DENY_EMOJI = '❌';
@@ -16,6 +17,7 @@ const grants = new Map();
module.exports = { module.exports = {
REQUEST_COOLDOWN_MS, REQUEST_COOLDOWN_MS,
GRANT_TTL_MS,
DM_APPROVE_EMOJI, DM_APPROVE_EMOJI,
DM_DENY_EMOJI, DM_DENY_EMOJI,
requestEvents, requestEvents,
@@ -70,11 +70,16 @@ function createPrivateAccessPolicy(deps) {
if (!isPrivateRecord(record)) return true; if (!isPrivateRecord(record)) return true;
if (isPrivateOpen(record)) return true; if (isPrivateOpen(record)) return true;
if (isLockdownAdmin(socket)) return true; if (isLockdownAdmin(socket)) return true;
if (socket?.id && record.drivers?.has(socket.id)) return true;
return socketHasClosedPrivateAccess(socket, record.id); return socketHasClosedPrivateAccess(socket, record.id);
} }
function getControlDenialReason(record, socket, options = {}) { function getControlDenialReason(record, socket, options = {}) {
const { allowUser = false, allowClosedPrivateGrantInLockdown = false } = options; const {
allowUser = false,
allowClosedPrivateGrantInLockdown = false,
allowClosedPrivateCurrentDriver = false,
} = options;
if (!record) return 'Unknown rover'; if (!record) return 'Unknown rover';
if (!allowUser && !isAdmin(socket)) return 'Only admins can request control'; if (!allowUser && !isAdmin(socket)) return 'Only admins can request control';
if (record.locked && !isAdmin(socket)) return 'Rover locked'; if (record.locked && !isAdmin(socket)) return 'Rover locked';
@@ -89,6 +94,11 @@ function createPrivateAccessPolicy(deps) {
isPrivateRecord(record) && isPrivateRecord(record) &&
!isPrivateOpen(record) && !isPrivateOpen(record) &&
socketHasClosedPrivateAccess(socket, record.id) socketHasClosedPrivateAccess(socket, record.id)
) &&
!(
allowClosedPrivateCurrentDriver &&
isPrivateRecord(record) &&
!isPrivateOpen(record)
) )
) { ) {
return 'Server in lockdown'; return 'Server in lockdown';
@@ -98,6 +108,7 @@ function createPrivateAccessPolicy(deps) {
if (!isPrivateRecord(record)) return null; if (!isPrivateRecord(record)) return null;
if (!isPrivateOpen(record)) { if (!isPrivateOpen(record)) {
if (!isLockdownAdmin(socket)) { if (!isLockdownAdmin(socket)) {
if (allowClosedPrivateCurrentDriver) return null;
if (socketHasClosedPrivateAccess(socket, record.id)) return null; if (socketHasClosedPrivateAccess(socket, record.id)) return null;
return 'Private rover is closed'; return 'Private rover is closed';
} }
@@ -85,14 +85,15 @@ function createRoverLifecycle(deps) {
function canDrive(roverId, socket) { function canDrive(roverId, socket) {
const record = rovers.get(roverId); const record = rovers.get(roverId);
if (!record) return false; if (!record) return false;
if (isAdmin(socket)) return true;
if (!socket || !isDriver(roverId, socket)) return false;
const denied = getControlDenialReason(record, socket, { const denied = getControlDenialReason(record, socket, {
allowUser: true, allowUser: true,
allowClosedPrivateGrantInLockdown: true, allowClosedPrivateGrantInLockdown: true,
allowClosedPrivateCurrentDriver: true,
}); });
if (denied) return false; if (denied) return false;
const _mode = getMode(); const _mode = getMode();
if (isAdmin(socket)) return true;
if (!socket || !isDriver(roverId, socket)) return false;
return turnService.canDrive(roverId, socket); return turnService.canDrive(roverId, socket);
} }