2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Contracts;
|
2026-02-08 18:00:24 +01:00
|
|
|
using IdentityShroud.Core.Helpers;
|
2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Messages.Realm;
|
|
|
|
|
using IdentityShroud.Core.Model;
|
2026-02-27 17:57:42 +00:00
|
|
|
using IdentityShroud.Core.Security.Keys;
|
|
|
|
|
using IdentityShroud.Core.Security.Keys.Rsa;
|
2026-02-15 07:15:11 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
namespace IdentityShroud.Core.Services;
|
|
|
|
|
|
2026-02-08 18:00:24 +01:00
|
|
|
public record RealmCreateResponse(Guid Id, string Slug, string Name);
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
public class RealmService(
|
|
|
|
|
Db db,
|
2026-02-27 17:57:42 +00:00
|
|
|
IKeyService keyService) : IRealmService
|
2026-02-08 11:57:57 +01:00
|
|
|
{
|
2026-02-27 17:57:42 +00:00
|
|
|
public async Task<Realm?> FindById(Guid id, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
return await db.Realms
|
|
|
|
|
.SingleOrDefaultAsync(r => r.Id == id, ct);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 07:15:11 +01:00
|
|
|
public async Task<Realm?> FindBySlug(string slug, CancellationToken ct = default)
|
2026-02-14 14:50:06 +01:00
|
|
|
{
|
2026-02-15 19:06:09 +01:00
|
|
|
return await db.Realms
|
|
|
|
|
.SingleOrDefaultAsync(r => r.Slug == slug, ct);
|
2026-02-14 14:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-08 11:57:57 +01:00
|
|
|
public async Task<Result<RealmCreateResponse>> Create(RealmCreateRequest request, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
Realm realm = new()
|
|
|
|
|
{
|
|
|
|
|
Id = request.Id ?? Guid.CreateVersion7(),
|
2026-02-08 18:00:24 +01:00
|
|
|
Slug = request.Slug ?? SlugHelper.GenerateSlug(request.Name),
|
|
|
|
|
Name = request.Name,
|
2026-02-08 11:57:57 +01:00
|
|
|
};
|
2026-02-27 17:57:42 +00:00
|
|
|
|
|
|
|
|
realm.Keys.Add(keyService.CreateKey(GetKeyPolicy(realm)));
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
db.Add(realm);
|
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
|
|
2026-02-08 18:00:24 +01:00
|
|
|
return new RealmCreateResponse(
|
|
|
|
|
realm.Id, realm.Slug, realm.Name);
|
2026-02-08 11:57:57 +01:00
|
|
|
}
|
2026-02-14 14:50:06 +01:00
|
|
|
|
2026-02-27 17:57:42 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Place holder for getting policies from the realm and falling back to sane defaults when no policies have been set.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="_"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private KeyPolicy GetKeyPolicy(Realm _) => new RsaKeyPolicy();
|
|
|
|
|
|
|
|
|
|
|
2026-02-15 19:06:09 +01:00
|
|
|
public async Task LoadActiveKeys(Realm realm)
|
|
|
|
|
{
|
|
|
|
|
await db.Entry(realm).Collection(r => r.Keys)
|
|
|
|
|
.Query()
|
2026-02-27 17:57:42 +00:00
|
|
|
.Where(k => k.RevokedAt == null)
|
2026-02-15 19:06:09 +01:00
|
|
|
.LoadAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:57:42 +00:00
|
|
|
public async Task LoadDeks(Realm realm)
|
2026-02-14 14:50:06 +01:00
|
|
|
{
|
2026-02-27 17:57:42 +00:00
|
|
|
await db.Entry(realm).Collection(r => r.Deks)
|
|
|
|
|
.Query()
|
|
|
|
|
.LoadAsync();
|
2026-02-14 14:50:06 +01:00
|
|
|
}
|
2026-02-08 11:57:57 +01:00
|
|
|
}
|