mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-16 20:56:03 +00:00
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.
This commit is contained in:
@@ -14,13 +14,15 @@ public class InviteUserController : Controller
|
||||
private readonly IInvitationGetter _invitationGetter;
|
||||
private readonly IInvitationGenerator _invitationGenerator;
|
||||
private readonly ILogger<InviteUserController> _logger;
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
public InviteUserController(IInvitationGenerator invitationGenerator,
|
||||
IInvitationGetter invitationGetter,
|
||||
IConfiguration configuration,
|
||||
ILogger<InviteUserController> logger)
|
||||
{
|
||||
_invitationGenerator = invitationGenerator;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_invitationGetter = invitationGetter;
|
||||
}
|
||||
|
||||
@@ -43,6 +45,42 @@ public class InviteUserController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("InvitationWithKey")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> 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<IActionResult> GetAllActiveInvitations()
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user