2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Contracts;
|
2026-03-16 19:15:04 +01:00
|
|
|
using IdentityShroud.Core.EFCore;
|
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-03-16 19:15:04 +01:00
|
|
|
using IdentityShroud.Core.Security;
|
2026-02-27 17:57:42 +00:00
|
|
|
using IdentityShroud.Core.Security.Keys;
|
2026-03-16 19:15:04 +01:00
|
|
|
using IdentityShroud.Core.Security.Keys.Aes;
|
2026-02-27 17:57:42 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public class RealmService(
|
|
|
|
|
Db db,
|
2026-03-16 19:15:04 +01:00
|
|
|
IKeyService keyService,
|
|
|
|
|
IDekEncryptionService dekCryptor,
|
|
|
|
|
IClock clock) : 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-03-16 19:15:04 +01:00
|
|
|
public async Task<Result<Realm>> Create(RealmCreateRequest request, CancellationToken ct = default)
|
2026-02-08 11:57:57 +01:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2026-03-16 19:15:04 +01:00
|
|
|
realm.TokenSigningKeys.Add(CreateSigningKey(realm));
|
|
|
|
|
realm.DataEncryptionKeys.Add(CreateDataEncryptionKey(realm));
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
db.Add(realm);
|
|
|
|
|
await db.SaveChangesAsync(ct);
|
2026-03-16 19:15:04 +01:00
|
|
|
|
|
|
|
|
return realm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RealmSigningKey CreateSigningKey(Realm realm)
|
|
|
|
|
{
|
|
|
|
|
var k = keyService.CreateKey(GetSigningKeyPolicy(realm));
|
|
|
|
|
return new RealmSigningKey
|
|
|
|
|
{
|
|
|
|
|
Id = RealmSigningKeyId.NewId(),
|
|
|
|
|
KeyType = k.KeyType,
|
|
|
|
|
Key = dekCryptor.Encrypt(k.Key.PrivateKey),
|
|
|
|
|
PublicKeyParameters = k.Key.PublicKeyParameters,
|
|
|
|
|
CreatedAt = clock.UtcNow(),
|
|
|
|
|
};
|
2026-02-08 11:57:57 +01:00
|
|
|
}
|
2026-02-14 14:50:06 +01:00
|
|
|
|
2026-03-16 19:15:04 +01:00
|
|
|
private RealmDek CreateDataEncryptionKey(Realm realm)
|
|
|
|
|
{
|
|
|
|
|
var k = keyService.CreateKey(GetDataKeyPolicy(realm));
|
|
|
|
|
return new RealmDek()
|
|
|
|
|
{
|
|
|
|
|
Id = DekId.NewId(),
|
|
|
|
|
Active = true,
|
|
|
|
|
Algorithm = k.KeyType,
|
|
|
|
|
KeyData = dekCryptor.Encrypt(k.Key.PrivateKey),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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>
|
2026-03-16 19:15:04 +01:00
|
|
|
private KeyPolicy GetSigningKeyPolicy(Realm _) => new RsaKeyPolicy();
|
|
|
|
|
private KeyPolicy GetDataKeyPolicy(Realm _) => new AesKeyPolicy();
|
2026-02-27 17:57:42 +00:00
|
|
|
|
2026-02-15 19:06:09 +01:00
|
|
|
public async Task LoadActiveKeys(Realm realm)
|
|
|
|
|
{
|
2026-03-16 19:15:04 +01:00
|
|
|
await db.Entry(realm).Collection(r => r.TokenSigningKeys)
|
2026-02-15 19:06:09 +01:00
|
|
|
.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-03-16 19:15:04 +01:00
|
|
|
await db.Entry(realm).Collection(r => r.DataEncryptionKeys)
|
2026-02-27 17:57:42 +00:00
|
|
|
.Query()
|
|
|
|
|
.LoadAsync();
|
2026-02-14 14:50:06 +01:00
|
|
|
}
|
2026-02-08 11:57:57 +01:00
|
|
|
}
|