// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 using Microsoft.EntityFrameworkCore; using OpenIddict.EntityFrameworkCore.Models; namespace Progmasoft.OAuth.Storage; /// The tables of Progmasoft OAuth: OpenIddict's, and who owns each application. /// /// The tables are named oauth_* so that they sit next to the account tables of the same database without a /// clash. creates them; Entity Framework only reads and writes rows. /// /// Options of the context. internal sealed class OAuthDbContext(DbContextOptions options) : DbContext(options) { /// Which account registered which application. public DbSet ApplicationOwners => Set(); /// protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.UseOpenIddict(); modelBuilder.Entity().ToTable("oauth_applications"); modelBuilder.Entity().ToTable("oauth_authorizations"); modelBuilder.Entity().ToTable("oauth_scopes"); modelBuilder.Entity().ToTable("oauth_tokens"); modelBuilder.Entity(owner => { owner.ToTable("oauth_application_owners"); owner.HasKey(entry => entry.ApplicationId); owner.HasIndex(entry => entry.AccountId); owner.HasOne() .WithMany() .HasForeignKey(entry => entry.ApplicationId) .OnDelete(DeleteBehavior.Cascade); }); } } /// The account that registered an application and may change or delete it. internal sealed class ApplicationOwner { /// Identifier of the OpenIddict application. public string ApplicationId { get; set; } = string.Empty; /// Identifier of the owning account. public Guid AccountId { get; set; } /// When the application was registered. public DateTimeOffset CreatedAt { get; set; } }