forked from gongxuegit/tiku-backend.net
feat: harden SaaS authentication and authorization
This commit is contained in:
@@ -225,13 +225,60 @@ public sealed class DirectContentEndpointTests
|
||||
Assert.Single(recordsJson.RootElement.GetProperty("items").EnumerateArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RegionBackedDirectContent_UsesRestrictedScopeAndReturns404ForOutsideWrite()
|
||||
{
|
||||
await using var factory = new ApiTestFactory();
|
||||
var seed = await SeedAdminAsync(factory);
|
||||
var allowedRegionId = Guid.NewGuid();
|
||||
var outsideRegionId = Guid.NewGuid();
|
||||
var allowedSchool = new School
|
||||
{
|
||||
TenantId = seed.TenantId,
|
||||
RegionId = allowedRegionId,
|
||||
Name = "Allowed School"
|
||||
};
|
||||
var outsideSchool = new School
|
||||
{
|
||||
TenantId = seed.TenantId,
|
||||
RegionId = outsideRegionId,
|
||||
Name = "Outside School"
|
||||
};
|
||||
await factory.SeedAsync(
|
||||
new Region { Id = allowedRegionId, TenantId = seed.TenantId, Name = "Allowed Region" },
|
||||
new Region { Id = outsideRegionId, TenantId = seed.TenantId, Name = "Outside Region" },
|
||||
allowedSchool,
|
||||
outsideSchool);
|
||||
await SetDataScopeAsync(factory, seed.TenantId, new
|
||||
{
|
||||
mode = "restricted",
|
||||
regionIds = new[] { allowedRegionId },
|
||||
includesSelf = false
|
||||
});
|
||||
|
||||
using var client = factory.CreateClient();
|
||||
await LoginAsync(client, seed);
|
||||
using var listResponse = await client.GetAsync("/api/tenant-content/scoreline/schools");
|
||||
using var list = await ReadJsonAsync(listResponse);
|
||||
using var deniedUpdate = await client.PutAsJsonAsync(
|
||||
"/api/tenant-content/scoreline/schools",
|
||||
new DirectSchoolDto
|
||||
{
|
||||
Id = outsideSchool.Id,
|
||||
RegionId = outsideRegionId,
|
||||
Name = "Hidden Update"
|
||||
});
|
||||
|
||||
Assert.Equal([allowedSchool.Id], list.RootElement.GetProperty("items").EnumerateArray()
|
||||
.Select(item => item.GetProperty("id").GetGuid()));
|
||||
Assert.Equal(HttpStatusCode.NotFound, deniedUpdate.StatusCode);
|
||||
}
|
||||
|
||||
private static async Task<(Guid TenantId, Guid UserId, string Phone)> SeedAdminAsync(ApiTestFactory factory)
|
||||
{
|
||||
var tenantId = Guid.NewGuid();
|
||||
var userId = Guid.NewGuid();
|
||||
var phone = $"137{Random.Shared.Next(10000000, 99999999)}";
|
||||
var passwordHash = new PasswordHasher().Hash("passw0rd!");
|
||||
|
||||
await factory.SeedAsync(
|
||||
new Tenant
|
||||
{
|
||||
@@ -246,21 +293,13 @@ public sealed class DirectContentEndpointTests
|
||||
Id = userId,
|
||||
Phone = phone,
|
||||
Name = "Tenant Admin"
|
||||
},
|
||||
}.WithTestPassword(),
|
||||
new TenantMembership
|
||||
{
|
||||
TenantId = tenantId,
|
||||
UserId = userId,
|
||||
Role = TenantRole.TenantAdmin,
|
||||
Status = MembershipStatus.Active
|
||||
},
|
||||
new UserIdentity
|
||||
{
|
||||
UserId = userId,
|
||||
Provider = "password",
|
||||
ProviderSubject = phone,
|
||||
Phone = phone,
|
||||
SecretPayload = CreateSecretPayload(passwordHash)
|
||||
});
|
||||
|
||||
return (tenantId, userId, phone);
|
||||
@@ -270,20 +309,17 @@ public sealed class DirectContentEndpointTests
|
||||
HttpClient client,
|
||||
(Guid TenantId, Guid UserId, string Phone) seed)
|
||||
{
|
||||
var loginResponse = await client.PostAsJsonAsync(
|
||||
"/api/auth/login/password",
|
||||
new PasswordLoginDto
|
||||
{
|
||||
TenantCode = seed.TenantId.ToString("N"),
|
||||
Phone = seed.Phone,
|
||||
Password = "passw0rd!"
|
||||
});
|
||||
var loginJson = await ReadJsonAsync(loginResponse);
|
||||
var accessToken = loginJson.RootElement
|
||||
.GetProperty("tokens")
|
||||
.GetProperty("accessToken")
|
||||
.GetString();
|
||||
client.DefaultRequestHeaders.Authorization = new("Bearer", accessToken);
|
||||
client.UseAccessToken(await client.LoginAsTenantAsync(seed.TenantId, seed.Phone));
|
||||
}
|
||||
|
||||
private static async Task SetDataScopeAsync(ApiTestFactory factory, Guid tenantId, object value)
|
||||
{
|
||||
using var scope = factory.CreateSystemScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<TikuDbContext>();
|
||||
var role = dbContext.TenantBackendRoles.Single(item =>
|
||||
item.TenantId == tenantId && item.Code == "integration_test_admin");
|
||||
role.DataScope = JsonSerializer.SerializeToElement(value);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private static async Task<JsonDocument> ReadJsonAsync(HttpResponseMessage response)
|
||||
@@ -292,10 +328,4 @@ public sealed class DirectContentEndpointTests
|
||||
return await JsonDocument.ParseAsync(stream);
|
||||
}
|
||||
|
||||
private static JsonElement CreateSecretPayload(string passwordHash)
|
||||
{
|
||||
using var document = JsonDocument.Parse(
|
||||
$$"""{"passwordHash":{{JsonSerializer.Serialize(passwordHash)}}}""");
|
||||
return document.RootElement.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user