danylokhodus commited on
Commit
c65f7a2
·
1 Parent(s): 35321eb

feat(sync): secure WebRTC endpoints with board authorization check

Browse files
FlowAPI/Controllers/SyncController.cs CHANGED
@@ -16,6 +16,7 @@ namespace FlowAPI.Controllers
16
  public class SyncController : ControllerBase
17
  {
18
  private readonly ISyncService _syncService;
 
19
 
20
  // In-memory store for WebRTC: ProjectId -> List of signals
21
  private static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new();
@@ -23,9 +24,10 @@ namespace FlowAPI.Controllers
23
  // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
24
  private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
25
 
26
- public SyncController(ISyncService syncService)
27
  {
28
  _syncService = syncService;
 
29
  }
30
 
31
  private Guid CurrentUserId
@@ -50,11 +52,15 @@ namespace FlowAPI.Controllers
50
  // --- WebRTC Real-time Presence ---
51
  [Authorize]
52
  [HttpPost("{projectId}/presence")]
53
- public IActionResult RegisterPresence(string projectId)
54
  {
55
  var userId = CurrentUserId;
56
  if (userId == Guid.Empty) return Unauthorized();
57
 
 
 
 
 
58
  var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());
59
 
60
  var info = new WebRTCPresence
@@ -82,11 +88,15 @@ namespace FlowAPI.Controllers
82
 
83
  [Authorize]
84
  [HttpGet("{projectId}/presence")]
85
- public IActionResult GetPresence(string projectId)
86
  {
87
  var userId = CurrentUserId;
88
  if (userId == Guid.Empty) return Unauthorized();
89
 
 
 
 
 
90
  if (!Presence.TryGetValue(projectId, out var projectPresence))
91
  {
92
  return Ok(Array.Empty<WebRTCPresence>());
@@ -114,11 +124,15 @@ namespace FlowAPI.Controllers
114
  // --- WebRTC Real-time Signaling ---
115
  [Authorize]
116
  [HttpPost("{projectId}/signal")]
117
- public IActionResult SendSignal(string projectId, [FromBody] WebRTCSignalDto dto)
118
  {
119
  var userId = CurrentUserId;
120
  if (userId == Guid.Empty) return Unauthorized();
121
 
 
 
 
 
122
  var message = new WebRTCSignal
123
  {
124
  SenderId = userId.ToString(),
@@ -142,11 +156,15 @@ namespace FlowAPI.Controllers
142
 
143
  [Authorize]
144
  [HttpGet("{projectId}/signal")]
145
- public IActionResult GetSignals(string projectId)
146
  {
147
  var userId = CurrentUserId;
148
  if (userId == Guid.Empty) return Unauthorized();
149
 
 
 
 
 
150
  var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());
151
 
152
  var mySignals = new List<WebRTCSignal>();
 
16
  public class SyncController : ControllerBase
17
  {
18
  private readonly ISyncService _syncService;
19
+ private readonly IGraphService _graphService;
20
 
21
  // In-memory store for WebRTC: ProjectId -> List of signals
22
  private static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new();
 
24
  // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
25
  private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
26
 
27
+ public SyncController(ISyncService syncService, IGraphService graphService)
28
  {
29
  _syncService = syncService;
30
+ _graphService = graphService;
31
  }
32
 
33
  private Guid CurrentUserId
 
52
  // --- WebRTC Real-time Presence ---
53
  [Authorize]
54
  [HttpPost("{projectId}/presence")]
55
+ public async Task<IActionResult> RegisterPresence(string projectId)
56
  {
57
  var userId = CurrentUserId;
58
  if (userId == Guid.Empty) return Unauthorized();
59
 
60
+ if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
61
+ var graph = await _graphService.GetByIdAsync(graphId, userId);
62
+ if (graph is null) return Forbid();
63
+
64
  var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());
65
 
66
  var info = new WebRTCPresence
 
88
 
89
  [Authorize]
90
  [HttpGet("{projectId}/presence")]
91
+ public async Task<IActionResult> GetPresence(string projectId)
92
  {
93
  var userId = CurrentUserId;
94
  if (userId == Guid.Empty) return Unauthorized();
95
 
96
+ if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
97
+ var graph = await _graphService.GetByIdAsync(graphId, userId);
98
+ if (graph is null) return Forbid();
99
+
100
  if (!Presence.TryGetValue(projectId, out var projectPresence))
101
  {
102
  return Ok(Array.Empty<WebRTCPresence>());
 
124
  // --- WebRTC Real-time Signaling ---
125
  [Authorize]
126
  [HttpPost("{projectId}/signal")]
127
+ public async Task<IActionResult> SendSignal(string projectId, [FromBody] WebRTCSignalDto dto)
128
  {
129
  var userId = CurrentUserId;
130
  if (userId == Guid.Empty) return Unauthorized();
131
 
132
+ if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
133
+ var graph = await _graphService.GetByIdAsync(graphId, userId);
134
+ if (graph is null) return Forbid();
135
+
136
  var message = new WebRTCSignal
137
  {
138
  SenderId = userId.ToString(),
 
156
 
157
  [Authorize]
158
  [HttpGet("{projectId}/signal")]
159
+ public async Task<IActionResult> GetSignals(string projectId)
160
  {
161
  var userId = CurrentUserId;
162
  if (userId == Guid.Empty) return Unauthorized();
163
 
164
+ if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
165
+ var graph = await _graphService.GetByIdAsync(graphId, userId);
166
+ if (graph is null) return Forbid();
167
+
168
  var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());
169
 
170
  var mySignals = new List<WebRTCSignal>();