-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMcpTokenController.cs
More file actions
68 lines (58 loc) · 2.7 KB
/
McpTokenController.cs
File metadata and controls
68 lines (58 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Security.Claims;
using EssentialCSharp.Web.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace EssentialCSharp.Web.Controllers;
[ApiController]
[Route("api/[controller]")]
[Authorize]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public class McpTokenController(McpApiTokenService tokenService) : ControllerBase
{
public record CreateTokenRequest(string? Name, DateOnly? ExpiresOn = null);
[HttpPost]
public async Task<IActionResult> CreateToken(
[FromBody] CreateTokenRequest? request,
CancellationToken cancellationToken)
{
string? userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userId))
return Unauthorized(new { Error = "User must be logged in to generate an MCP token." });
string name = string.IsNullOrWhiteSpace(request?.Name) ? "default" : request.Name.Trim();
if (name.Length > 256)
return BadRequest(new { Error = "Token name must be 256 characters or fewer." });
DateTime? expiresAt = null;
DateOnly maxExpiresOn = McpApiTokenService.GetDefaultExpiryDate();
if (request?.ExpiresOn is DateOnly expiresOn)
{
if (expiresOn < DateOnly.FromDateTime(DateTime.UtcNow))
return BadRequest(new { Error = "ExpiresOn must be today or in the future." });
if (expiresOn > maxExpiresOn)
return BadRequest(new { Error = McpApiTokenService.MaxExpiryValidationMessage });
// Convert date-only boundary to end-of-day UTC instant before persisting
expiresAt = expiresOn.ToDateTime(TimeOnly.MaxValue, DateTimeKind.Utc);
}
var (rawToken, entity) = await tokenService.CreateTokenAsync(
userId, name, expiresAt, cancellationToken);
return Ok(new
{
TokenId = entity.Id,
Token = rawToken,
Name = entity.Name,
ExpiresAt = entity.ExpiresAt,
CreatedAt = entity.CreatedAt,
Usage = "Add to your MCP client config: { \"url\": \"<site-url>/mcp\", \"headers\": { \"Authorization\": \"Bearer <token>\" } }"
});
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> RevokeToken(Guid id, CancellationToken cancellationToken)
{
string? userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userId))
return Unauthorized();
bool revoked = await tokenService.RevokeTokenAsync(id, userId, cancellationToken);
if (!revoked)
return NotFound(new { Error = "Token not found or already revoked." });
return NoContent();
}
}