Heimdall Gitea / Build and tests (pull_request) Successful in 15m33s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 6m28s
Heimdall Gitea / Build and scan server image (pull_request) Successful in 7m39s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
83 lines
3.8 KiB
C#
83 lines
3.8 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Heimdall.IntegrationTests.Controllers;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.IntegrationTests.Scenarios;
|
|
|
|
[Collection("Postgres")]
|
|
public sealed class CameraAuthorizationScenarios : PostgresTestBase
|
|
{
|
|
public CameraAuthorizationScenarios(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
[Fact]
|
|
public async Task Direct_camera_grant_is_scoped_and_soft_delete_revokes_api_access()
|
|
{
|
|
var admin = await Factory.CreateAdminClientAsync();
|
|
var viewer = await Factory.CreateViewerClientAsync();
|
|
var viewerId = UserIdFromAccessToken(viewer);
|
|
|
|
var siteResponse = await admin.PostAsJsonAsync("api/sites", new
|
|
{
|
|
name = $"Access matrix {Guid.NewGuid():N}",
|
|
timezone = "UTC"
|
|
});
|
|
siteResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
|
|
var siteId = (await siteResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
|
|
|
|
var allowedCameraResponse = await admin.PostAsJsonAsync("api/cameras", new
|
|
{
|
|
siteId,
|
|
name = "Granted camera",
|
|
rtspUrl = "rtsp://127.0.0.1:554/stream"
|
|
});
|
|
allowedCameraResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
|
|
var allowedCameraId = (await allowedCameraResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
|
|
|
|
var deniedCameraResponse = await admin.PostAsJsonAsync("api/cameras", new
|
|
{
|
|
siteId,
|
|
name = "Unassigned camera",
|
|
rtspUrl = "rtsp://127.0.0.2:554/stream"
|
|
});
|
|
deniedCameraResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
|
|
var deniedCameraId = (await deniedCameraResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
|
|
|
|
var grantResponse = await admin.PostAsJsonAsync("api/user-camera-access", new
|
|
{
|
|
userId = viewerId,
|
|
siteId,
|
|
cameraId = allowedCameraId
|
|
});
|
|
grantResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
|
|
|
|
// The authorized route reaches the query and returns its normal empty result;
|
|
// a same-site camera without a grant is stopped by the resource boundary.
|
|
(await viewer.GetAsync($"api/assignments/camera/{allowedCameraId}")).StatusCode
|
|
.ShouldBe(HttpStatusCode.NotFound);
|
|
(await viewer.GetAsync($"api/assignments/camera/{deniedCameraId}")).StatusCode
|
|
.ShouldBe(HttpStatusCode.Forbidden);
|
|
|
|
var groupResponse = await admin.PostAsJsonAsync("api/groups", new { name = $"Private group {Guid.NewGuid():N}" });
|
|
groupResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
|
|
var groupId = (await groupResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
|
|
(await viewer.GetAsync($"api/groups/{groupId}/cameras")).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
|
|
(await viewer.PostAsJsonAsync($"api/groups/{groupId}/cameras", new { groupId, cameraId = deniedCameraId }))
|
|
.StatusCode.ShouldBe(HttpStatusCode.Forbidden);
|
|
|
|
(await admin.DeleteAsync($"api/cameras/{allowedCameraId}")).StatusCode.ShouldBe(HttpStatusCode.NoContent);
|
|
(await viewer.GetAsync($"api/assignments/camera/{allowedCameraId}")).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
|
|
}
|
|
|
|
private static Guid UserIdFromAccessToken(HttpClient client)
|
|
{
|
|
var token = client.DefaultRequestHeaders.Authorization?.Parameter
|
|
?? throw new InvalidOperationException("The test client has no access token");
|
|
var payload = token.Split('.')[1].Replace('-', '+').Replace('_', '/');
|
|
payload = payload.PadRight((payload.Length + 3) / 4 * 4, '=');
|
|
using var document = JsonDocument.Parse(Convert.FromBase64String(payload));
|
|
return document.RootElement.GetProperty("sub").GetGuid();
|
|
}
|
|
}
|