From 9aa7e017e2c45b9713f5efb7e114026122d53043 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Sun, 4 Oct 2026 16:38:57 +0700 Subject: [PATCH] Add secure admin-key invitation endpoint Adds a new invitation creation endpoint that requires a configured admin key before generating an invitation code. The controller now reads the expected key from configuration, returns 503 when it is missing, and rejects invalid attempts with 401. This also includes the request contract for the admin-key payload. --- .../AdminStuff/InviteUserController.cs | 40 ++++++++++++++++++- Govor.API/Program.cs | 7 +++- .../CreateInvitationWithKeyRequest.cs | 10 +++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs diff --git a/Govor.API/Controllers/AdminStuff/InviteUserController.cs b/Govor.API/Controllers/AdminStuff/InviteUserController.cs index 0fbfcfa..f26bcdd 100644 --- a/Govor.API/Controllers/AdminStuff/InviteUserController.cs +++ b/Govor.API/Controllers/AdminStuff/InviteUserController.cs @@ -14,13 +14,15 @@ public class InviteUserController : Controller private readonly IInvitationGetter _invitationGetter; private readonly IInvitationGenerator _invitationGenerator; private readonly ILogger _logger; - + private readonly IConfiguration _configuration; public InviteUserController(IInvitationGenerator invitationGenerator, IInvitationGetter invitationGetter, + IConfiguration configuration, ILogger logger) { _invitationGenerator = invitationGenerator; _logger = logger; + _configuration = configuration; _invitationGetter = invitationGetter; } @@ -43,6 +45,42 @@ public class InviteUserController : Controller } } + [HttpPost("InvitationWithKey")] + [AllowAnonymous] + public async Task InvitationWithKey([FromBody] CreateInvitationWithKeyRequest request) + { + try + { + var expectedKey = _configuration["InvitationAdmin:Key"]; + + if (string.IsNullOrWhiteSpace(expectedKey)) + { + _logger.LogWarning("InvitationAdmin:Key is not configured"); + return StatusCode(StatusCodes.Status503ServiceUnavailable, "Admin key is not configured"); + } + + if (string.IsNullOrWhiteSpace(request.AdminKey) || + !string.Equals(request.AdminKey, expectedKey, StringComparison.Ordinal)) + { + _logger.LogWarning("Invalid admin key attempt for invitation creation"); + return Unauthorized("Invalid admin key"); + } + + var result = await _invitationGenerator.GenerateInvitationCode( + request.EndDate, + request.MaxParticipants, + request.IsAdmin, + request.Description); + + return Ok(result); + } + catch (Exception e) + { + _logger.LogError(e, e.Message); + return BadRequest($"An error occured: {e.Message}"); + } + } + [HttpGet("[action]")] public async Task GetAllActiveInvitations() { diff --git a/Govor.API/Program.cs b/Govor.API/Program.cs index cff9cb5..95df209 100644 --- a/Govor.API/Program.cs +++ b/Govor.API/Program.cs @@ -164,11 +164,14 @@ if (migrateOnly) return; } -if (app.Environment.IsDevelopment()) +/*if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); -} +}*/ + +app.UseSwagger(); +app.UseSwaggerUI(); //app.UseHttpsRedirection(); diff --git a/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs b/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs new file mode 100644 index 0000000..c023c06 --- /dev/null +++ b/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs @@ -0,0 +1,10 @@ +namespace Govor.Contracts.Requests; + +public class CreateInvitationWithKeyRequest +{ + public DateTime EndDate { get; set; } + public int MaxParticipants { get; set; } + public bool IsAdmin { get; set; } + public string? Description { get; set; } + public string AdminKey { get; set; } = string.Empty; +} \ No newline at end of file