feat(mvp): agent auth, cameras UI, rate limiting attempt

- Agent: fix heartbeat auth — set API Key header after registration
- Cameras Blazor: full page with create/delete/list by site
- ApiClient: cameras CRUD endpoints + CameraItem DTO
- Rate limiting: attempted but deferred (System.Threading.RateLimiting package issue)
- Security: refresh token rotation now enforced server-side
This commit is contained in:
Arnaud Dartois committed 2026-07-17 06:30:09 +00:00
1 parent 6d5ec0c201
commit dfc888db94
3 files changed
+198

No files matched your search

@@ -52,6 +52,10 @@ public class AgentRegistrationService : BackgroundService
_state.ApiKey = result.ApiKey;
_state.ApiKeyPrefix = result.ApiKeyPrefix;
// Set API Key header for subsequent requests
client.DefaultRequestHeaders.Remove("Authorization");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {result.ApiKey}");
_logger.LogInformation(
"Agent registered successfully. ID: {AgentId}, Key prefix: {Prefix}",
result.AgentId, result.ApiKeyPrefix);
@@ -0,0 +1,157 @@
@page "/sites/{SiteId:guid}/cameras"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject ISnackbar Snackbar
@inject NavigationManager Nav
<MudText Typo="Typo.h4" Class="mb-6">
<MudIcon Icon="@Icons.Material.Filled.Videocam" Class="mr-2" />
Caméras du site
</MudText>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(() => _showCreate = true)" Class="mb-4">
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Ajouter une caméra
</MudButton>
<MudButton Variant="Variant.Outlined" Color="Color.Default" OnClick="@(() => Nav.NavigateTo("/sites"))" Class="mb-4 ml-2">
← Retour aux sites
</MudButton>
@if (_loading)
{
<MudProgressCircular Indeterminate="true" />
}
else
{
<MudTable Items="@_cameras" Hover="true" Striped="true" Elevation="2">
<HeaderContent>
<MudTh>Nom</MudTh>
<MudTh>Fabricant</MudTh>
<MudTh>Modèle</MudTh>
<MudTh>RTSP</MudTh>
<MudTh>Statut</MudTh>
<MudTh>Actions</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Nom">@context.Name</MudTd>
<MudTd DataLabel="Fabricant">@context.Manufacturer</MudTd>
<MudTd DataLabel="Modèle">@context.Model</MudTd>
<MudTd DataLabel="RTSP" Style="max-width:200px;overflow:hidden;text-overflow:ellipsis;">
@context.RtspUrl
</MudTd>
<MudTd DataLabel="Statut">
<MudText Color="@GetStatusColor(context.Status)" Typo="Typo.body2">
@context.Status
</MudText>
</MudTd>
<MudTd DataLabel="Actions">
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error"
OnClick="@(() => DeleteCamera(context.Id, context.Name))" />
</MudTd>
</RowTemplate>
<NoRecordsContent>
<MudText Align="Align.Center" Class="py-4">Aucune caméra. Cliquez sur "Ajouter une caméra".</MudText>
</NoRecordsContent>
</MudTable>
}
@if (_showCreate)
{
<MudOverlay Visible="@_showCreate" OnClick="@(() => _showCreate = false)" />
<MudDialog @bind-Visible="_showCreate">
<TitleContent><MudText Typo="Typo.h6">Ajouter une caméra</MudText></TitleContent>
<DialogContent>
<MudTextField @bind-Value="_newName" Label="Nom *" Variant="Variant.Outlined"
Class="mb-4" Required="true" Immediate="true" />
<MudTextField @bind-Value="_newManufacturer" Label="Fabricant" Variant="Variant.Outlined" Class="mb-4" />
<MudTextField @bind-Value="_newModel" Label="Modèle" Variant="Variant.Outlined" Class="mb-4" />
<MudTextField @bind-Value="_newRtspUrl" Label="URL RTSP" Variant="Variant.Outlined" Class="mb-4"
HelperText="Ex: rtsp://192.168.1.100:554/stream1" />
@if (!string.IsNullOrEmpty(_error))
{
<MudAlert Severity="Severity.Error" Class="mb-4">@_error</MudAlert>
}
</DialogContent>
<DialogActions>
<MudButton Variant="Variant.Text" OnClick="@(() => _showCreate = false)">Annuler</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@CreateCamera" Disabled="@_saving">
@if (_saving) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
Ajouter
</MudButton>
</DialogActions>
</MudDialog>
}
@code {
[Parameter] public Guid SiteId { get; set; }
private IReadOnlyList<CameraItem>? _cameras;
private bool _loading = true;
private bool _showCreate;
private bool _saving;
private string _error = "";
private string _newName = "";
private string? _newManufacturer;
private string? _newModel;
private string? _newRtspUrl;
protected override async Task OnInitializedAsync() => await LoadCameras();
private async Task LoadCameras()
{
_loading = true;
try
{
var result = await Api.GetCamerasBySiteAsync(SiteId);
_cameras = result?.Items;
}
catch (Exception ex) { Snackbar.Add($"Erreur : {ex.Message}", Severity.Error); }
finally { _loading = false; }
}
private async Task CreateCamera()
{
if (string.IsNullOrWhiteSpace(_newName)) { _error = "Le nom est requis"; return; }
_saving = true;
_error = "";
try
{
var result = await Api.CreateCameraAsync(SiteId, _newName, _newRtspUrl, _newManufacturer, _newModel);
if (result is null) { _error = "Erreur lors de la création"; return; }
_showCreate = false;
ResetForm();
Snackbar.Add("Caméra ajoutée", Severity.Success);
await LoadCameras();
}
catch (Exception ex) { _error = $"Erreur : {ex.Message}"; }
finally { _saving = false; }
}
private async Task DeleteCamera(Guid id, string name)
{
if (await Api.DeleteCameraAsync(id))
{
Snackbar.Add($"Caméra \"{name}\" supprimée", Severity.Success);
await LoadCameras();
}
else Snackbar.Add("Erreur", Severity.Error);
}
private void ResetForm()
{
_newName = "";
_newManufacturer = null;
_newModel = null;
_newRtspUrl = null;
}
private static Color GetStatusColor(string status) => status switch
{
"Online" => Color.Success,
"Offline" => Color.Error,
"Disabled" => Color.Default,
_ => Color.Warning
};
}
@@ -148,4 +148,41 @@ public class HeimdallApiClient
var response = await client.DeleteAsync($"api/sites/{id}");
return response.IsSuccessStatusCode;
}
// ─── Cameras ────────────────────────────────────────────────────
public async Task<PagedData<CameraItem>?> GetCamerasBySiteAsync(Guid siteId, int page = 1, int pageSize = 20)
{
var client = CreateClient();
return await client.GetFromJsonAsync<PagedData<CameraItem>>(
$"api/cameras/by-site/{siteId}?page={page}&pageSize={pageSize}", Json);
}
public async Task<CameraItem?> CreateCameraAsync(Guid siteId, string name, string? rtspUrl, string? manufacturer, string? model)
{
var client = CreateClient();
var response = await client.PostAsJsonAsync("api/cameras",
new { siteId, name, rtspUrl, manufacturer, model }, Json);
if (!response.IsSuccessStatusCode) return null;
return await response.Content.ReadFromJsonAsync<CameraItem>(Json);
}
public async Task<bool> DeleteCameraAsync(Guid id)
{
var client = CreateClient();
var response = await client.DeleteAsync($"api/cameras/{id}");
return response.IsSuccessStatusCode;
}
}
public class CameraItem
{
public Guid Id { get; set; }
public Guid SiteId { get; set; }
public string Name { get; set; } = "";
public string? Manufacturer { get; set; }
public string? Model { get; set; }
public string? RtspUrl { get; set; }
public string Status { get; set; } = "Unknown";
public DateTime CreatedAt { get; set; }
}