Spaces:
Running
Running
Commit ·
c65f7a2
1
Parent(s): 35321eb
feat(sync): secure WebRTC endpoints with board authorization check
Browse files15 unmodified lines1617181920211 unmodified line23242526272829303118 unmodified lines505152535455565758596021 unmodified lines828384858687888990919221 unmodified lines11411511611711811912012112212312417 unmodified lines14214314414514614714814915015115215 unmodified linespublic class SyncController : ControllerBase{private readonly ISyncService _syncService;// In-memory store for WebRTC: ProjectId -> List of signalsprivate static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new();1 unmodified line// In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();public SyncController(ISyncService syncService){_syncService = syncService;}private Guid CurrentUserId18 unmodified lines// --- WebRTC Real-time Presence ---[Authorize][HttpPost("{projectId}/presence")]public IActionResult RegisterPresence(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());var info = new WebRTCPresence21 unmodified lines[Authorize][HttpGet("{projectId}/presence")]public IActionResult GetPresence(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();if (!Presence.TryGetValue(projectId, out var projectPresence)){return Ok(Array.Empty<WebRTCPresence>());21 unmodified lines// --- WebRTC Real-time Signaling ---[Authorize][HttpPost("{projectId}/signal")]public IActionResult SendSignal(string projectId, [FromBody] WebRTCSignalDto dto){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();var message = new WebRTCSignal{SenderId = userId.ToString(),17 unmodified lines[Authorize][HttpGet("{projectId}/signal")]public IActionResult GetSignals(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());var mySignals = new List<WebRTCSignal>();15 unmodified lines161718192021221 unmodified line2425262728293031323318 unmodified lines52535455565758596061626364656621 unmodified lines88899091929394959697989910010110221 unmodified lines12412512612712812913013113213313413513613713817 unmodified lines15615715815916016116216316416516616716816917015 unmodified linespublic class SyncController : ControllerBase{private readonly ISyncService _syncService;private readonly IGraphService _graphService;// In-memory store for WebRTC: ProjectId -> List of signalsprivate static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new();1 unmodified line// In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();public SyncController(ISyncService syncService, IGraphService graphService){_syncService = syncService;_graphService = graphService;}private Guid CurrentUserId18 unmodified lines// --- WebRTC Real-time Presence ---[Authorize][HttpPost("{projectId}/presence")]public async Task<IActionResult> RegisterPresence(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");var graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());var info = new WebRTCPresence21 unmodified lines[Authorize][HttpGet("{projectId}/presence")]public async Task<IActionResult> GetPresence(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");var graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();if (!Presence.TryGetValue(projectId, out var projectPresence)){return Ok(Array.Empty<WebRTCPresence>());21 unmodified lines// --- WebRTC Real-time Signaling ---[Authorize][HttpPost("{projectId}/signal")]public async Task<IActionResult> SendSignal(string projectId, [FromBody] WebRTCSignalDto dto){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");var graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();var message = new WebRTCSignal{SenderId = userId.ToString(),17 unmodified lines[Authorize][HttpGet("{projectId}/signal")]public async Task<IActionResult> GetSignals(string projectId){var userId = CurrentUserId;if (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");var graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());var mySignals = new List<WebRTCSignal>();
FlowAPI/Controllers/SyncController.cs+23-5