Spaces:
Sleeping
Sleeping
Commit ·
3c872cf
1
Parent(s): 3105a78
Auth: Implement cryptographic one-time claim token verification and ownership transfer
Browse files
FlowAPI/Controllers/AgentCanvasController.cs
CHANGED
|
@@ -5,6 +5,7 @@ using FlowAPI.Application.Services;
|
|
| 5 |
using FlowAPI.Domain.Entities;
|
| 6 |
using Microsoft.AspNetCore.Authorization;
|
| 7 |
using Microsoft.AspNetCore.Mvc;
|
|
|
|
| 8 |
using System;
|
| 9 |
using System.Security.Claims;
|
| 10 |
using System.Threading.Tasks;
|
|
@@ -19,12 +20,21 @@ namespace FlowAPI.Controllers
|
|
| 19 |
private readonly IGraphService _graphService;
|
| 20 |
private readonly ITokenService _tokenService;
|
| 21 |
private readonly IUserRepository _userRepo;
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
public AgentCanvasController(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
{
|
| 25 |
_graphService = graphService;
|
| 26 |
_tokenService = tokenService;
|
| 27 |
_userRepo = userRepo;
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
[AllowAnonymous]
|
|
@@ -106,12 +116,17 @@ namespace FlowAPI.Controllers
|
|
| 106 |
return BadRequest(new { message = "Failed to store generated task nodes in database." });
|
| 107 |
}
|
| 108 |
|
| 109 |
-
// 4.
|
|
|
|
|
|
|
|
|
|
| 110 |
return Ok(new
|
| 111 |
{
|
| 112 |
success = true,
|
| 113 |
graphId = graphResponse.Id,
|
| 114 |
-
canvasUrl = $"/canvas/{graphResponse.Id}"
|
|
|
|
|
|
|
| 115 |
});
|
| 116 |
}
|
| 117 |
catch (Exception ex)
|
|
@@ -120,6 +135,96 @@ namespace FlowAPI.Controllers
|
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
private Guid CurrentUserId
|
| 124 |
{
|
| 125 |
get
|
|
@@ -129,4 +234,9 @@ namespace FlowAPI.Controllers
|
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
|
|
|
| 5 |
using FlowAPI.Domain.Entities;
|
| 6 |
using Microsoft.AspNetCore.Authorization;
|
| 7 |
using Microsoft.AspNetCore.Mvc;
|
| 8 |
+
using Microsoft.Extensions.Configuration;
|
| 9 |
using System;
|
| 10 |
using System.Security.Claims;
|
| 11 |
using System.Threading.Tasks;
|
|
|
|
| 20 |
private readonly IGraphService _graphService;
|
| 21 |
private readonly ITokenService _tokenService;
|
| 22 |
private readonly IUserRepository _userRepo;
|
| 23 |
+
private readonly IGraphRepository _graphRepo;
|
| 24 |
+
private readonly IConfiguration _config;
|
| 25 |
|
| 26 |
+
public AgentCanvasController(
|
| 27 |
+
IGraphService graphService,
|
| 28 |
+
ITokenService tokenService,
|
| 29 |
+
IUserRepository userRepo,
|
| 30 |
+
IGraphRepository graphRepo,
|
| 31 |
+
IConfiguration config)
|
| 32 |
{
|
| 33 |
_graphService = graphService;
|
| 34 |
_tokenService = tokenService;
|
| 35 |
_userRepo = userRepo;
|
| 36 |
+
_graphRepo = graphRepo;
|
| 37 |
+
_config = config;
|
| 38 |
}
|
| 39 |
|
| 40 |
[AllowAnonymous]
|
|
|
|
| 116 |
return BadRequest(new { message = "Failed to store generated task nodes in database." });
|
| 117 |
}
|
| 118 |
|
| 119 |
+
// 4. Generate a secure, signed claim token valid for 24 hours
|
| 120 |
+
var claimToken = GenerateClaimToken(graphResponse.Id, DateTime.UtcNow.AddHours(24));
|
| 121 |
+
var claimUrl = $"https://danildrotik-flowweb.hf.space/claim?token={claimToken}";
|
| 122 |
+
|
| 123 |
return Ok(new
|
| 124 |
{
|
| 125 |
success = true,
|
| 126 |
graphId = graphResponse.Id,
|
| 127 |
+
canvasUrl = $"/canvas/{graphResponse.Id}",
|
| 128 |
+
claimToken = claimToken,
|
| 129 |
+
claimUrl = claimUrl
|
| 130 |
});
|
| 131 |
}
|
| 132 |
catch (Exception ex)
|
|
|
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
+
[HttpPost("claim")]
|
| 139 |
+
public async Task<IActionResult> ClaimCanvas([FromBody] ClaimRequestDto dto)
|
| 140 |
+
{
|
| 141 |
+
if (CurrentUserId == Guid.Empty)
|
| 142 |
+
return Unauthorized(new { message = "User authentication required." });
|
| 143 |
+
|
| 144 |
+
if (dto == null || string.IsNullOrWhiteSpace(dto.Token))
|
| 145 |
+
return BadRequest(new { message = "Claim token is required." });
|
| 146 |
+
|
| 147 |
+
var graphId = VerifyClaimToken(dto.Token);
|
| 148 |
+
if (graphId == null)
|
| 149 |
+
return BadRequest(new { message = "Claim token is invalid or has expired." });
|
| 150 |
+
|
| 151 |
+
try
|
| 152 |
+
{
|
| 153 |
+
var dbGraph = await _graphRepo.GetByIdAsync(graphId.Value);
|
| 154 |
+
if (dbGraph == null)
|
| 155 |
+
return NotFound(new { message = "Board not found." });
|
| 156 |
+
|
| 157 |
+
// If the user already owns the board, return success immediately
|
| 158 |
+
if (dbGraph.UserId == CurrentUserId)
|
| 159 |
+
{
|
| 160 |
+
return Ok(new { success = true, graphId = graphId.Value, message = "You already own this board!" });
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// Verify that the board is currently owned by the agent system user (unclaimed)
|
| 164 |
+
var agentId = Guid.Parse("33d1ae31-bf51-4b12-89d0-8e574e7916ab");
|
| 165 |
+
if (dbGraph.UserId != agentId)
|
| 166 |
+
{
|
| 167 |
+
return BadRequest(new { message = "This board has already been claimed by another user." });
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Transfer ownership in the database!
|
| 171 |
+
dbGraph.UserId = CurrentUserId;
|
| 172 |
+
await _graphRepo.UpdateAsync(dbGraph);
|
| 173 |
+
|
| 174 |
+
return Ok(new { success = true, graphId = graphId.Value });
|
| 175 |
+
}
|
| 176 |
+
catch (Exception ex)
|
| 177 |
+
{
|
| 178 |
+
return BadRequest(new { message = $"Failed to claim board: {ex.Message}" });
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
private string GenerateClaimToken(Guid graphId, DateTime expiry)
|
| 183 |
+
{
|
| 184 |
+
var key = _config["Jwt:Key"] ?? "TaskFlow_Secret_Key_For_JWT_Authentication_2026_!@#";
|
| 185 |
+
var payload = $"{graphId}|{expiry.Ticks}";
|
| 186 |
+
using (var hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(key)))
|
| 187 |
+
{
|
| 188 |
+
var hash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payload));
|
| 189 |
+
var signature = Convert.ToBase64String(hash);
|
| 190 |
+
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{payload}|{signature}"));
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
private Guid? VerifyClaimToken(string claimToken)
|
| 195 |
+
{
|
| 196 |
+
try
|
| 197 |
+
{
|
| 198 |
+
var key = _config["Jwt:Key"] ?? "TaskFlow_Secret_Key_For_JWT_Authentication_2026_!@#";
|
| 199 |
+
var decodedBytes = Convert.FromBase64String(claimToken);
|
| 200 |
+
var decodedString = System.Text.Encoding.UTF8.GetString(decodedBytes);
|
| 201 |
+
var parts = decodedString.Split('|');
|
| 202 |
+
if (parts.Length != 3) return null;
|
| 203 |
+
|
| 204 |
+
var graphIdStr = parts[0];
|
| 205 |
+
var ticksStr = parts[1];
|
| 206 |
+
var signature = parts[2];
|
| 207 |
+
|
| 208 |
+
var payload = $"{graphIdStr}|{ticksStr}";
|
| 209 |
+
using (var hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(key)))
|
| 210 |
+
{
|
| 211 |
+
var hash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payload));
|
| 212 |
+
var expectedSignature = Convert.ToBase64String(hash);
|
| 213 |
+
if (signature != expectedSignature) return null;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
if (!long.TryParse(ticksStr, out var ticks)) return null;
|
| 217 |
+
var expiry = new DateTime(ticks, DateTimeKind.Utc);
|
| 218 |
+
if (DateTime.UtcNow > expiry) return null;
|
| 219 |
+
|
| 220 |
+
return Guid.TryParse(graphIdStr, out var graphId) ? graphId : null;
|
| 221 |
+
}
|
| 222 |
+
catch
|
| 223 |
+
{
|
| 224 |
+
return null;
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
private Guid CurrentUserId
|
| 229 |
{
|
| 230 |
get
|
|
|
|
| 234 |
}
|
| 235 |
}
|
| 236 |
}
|
| 237 |
+
|
| 238 |
+
public class ClaimRequestDto
|
| 239 |
+
{
|
| 240 |
+
public string Token { get; set; } = string.Empty;
|
| 241 |
+
}
|
| 242 |
}
|