init with code from Foxnouns.NET
This commit is contained in:
commit
e658366473
30 changed files with 1387 additions and 0 deletions
6
Hydra.Backend/Database/BaseModel.cs
Normal file
6
Hydra.Backend/Database/BaseModel.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Hydra.Backend.Database;
|
||||
|
||||
public abstract class BaseModel
|
||||
{
|
||||
public Ulid Id { get; init; } = Ulid.NewUlid();
|
||||
}
|
73
Hydra.Backend/Database/HydraContext.cs
Normal file
73
Hydra.Backend/Database/HydraContext.cs
Normal file
|
@ -0,0 +1,73 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using EntityFramework.Exceptions.PostgreSQL;
|
||||
using Hydra.Backend.Database.Models;
|
||||
using Hydra.Backend.Utils;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Npgsql;
|
||||
|
||||
namespace Hydra.Backend.Database;
|
||||
|
||||
public class HydraContext : DbContext
|
||||
{
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
private readonly ILoggerFactory? _loggerFactory;
|
||||
|
||||
public DbSet<Account> Accounts { get; set; }
|
||||
public DbSet<Member> Members { get; set; }
|
||||
public DbSet<Token> Tokens { get; set; }
|
||||
public DbSet<Application> Applications { get; set; }
|
||||
|
||||
public HydraContext(Config config, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
Timeout = config.Database.Timeout ?? 5,
|
||||
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
|
||||
}.ConnectionString;
|
||||
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
|
||||
dataSourceBuilder.UseNodaTime();
|
||||
_dataSource = dataSourceBuilder.Build();
|
||||
_loggerFactory = loggerFactory;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.ConfigureWarnings(c =>
|
||||
c.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)
|
||||
.Ignore(CoreEventId.SaveChangesFailed))
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.UseLoggerFactory(_loggerFactory)
|
||||
.UseExceptionProcessor();
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
// ULIDs are stored as UUIDs in the database
|
||||
configurationBuilder.Properties<Ulid>().HaveConversion<UlidConverter>();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Account>().HasIndex(u => u.Name).IsUnique();
|
||||
modelBuilder.Entity<Member>().HasIndex(m => new { m.AccountId, m.Name }).IsUnique();
|
||||
}
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "UnusedType.Global", Justification = "Used when generating migrations")]
|
||||
public class DesignTimeDatabaseContextFactory : IDesignTimeDbContextFactory<HydraContext>
|
||||
{
|
||||
public HydraContext CreateDbContext(string[] args)
|
||||
{
|
||||
// Read the configuration file
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddConfiguration(args)
|
||||
.Build()
|
||||
// Get the configuration as our config class
|
||||
.Get<Config>() ?? new();
|
||||
|
||||
return new HydraContext(config, null);
|
||||
}
|
||||
}
|
10
Hydra.Backend/Database/Models/Account.cs
Normal file
10
Hydra.Backend/Database/Models/Account.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Hydra.Backend.Database.Models;
|
||||
|
||||
public class Account : BaseModel
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public required string EmailAddress { get; set; }
|
||||
public required string Password { get; set; }
|
||||
|
||||
public List<Member> Members { get; init; } = [];
|
||||
}
|
39
Hydra.Backend/Database/Models/Application.cs
Normal file
39
Hydra.Backend/Database/Models/Application.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using System.Security.Cryptography;
|
||||
using Hydra.Backend.Utils;
|
||||
|
||||
namespace Hydra.Backend.Database.Models;
|
||||
|
||||
public class Application : BaseModel
|
||||
{
|
||||
public required string ClientId { get; init; }
|
||||
public required string ClientSecret { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public required string[] Scopes { get; init; }
|
||||
public required string[] RedirectUris { get; set; }
|
||||
|
||||
public static Application Create(string name, string[] scopes,
|
||||
string[] redirectUrls)
|
||||
{
|
||||
var clientId = RandomNumberGenerator.GetHexString(32, true);
|
||||
var clientSecret = AuthUtils.RandomToken();
|
||||
|
||||
if (scopes.Except(AuthUtils.Scopes).Any())
|
||||
{
|
||||
throw new ArgumentException("Invalid scopes passed to Application.Create", nameof(scopes));
|
||||
}
|
||||
|
||||
if (redirectUrls.Any(s => !AuthUtils.ValidateRedirectUri(s)))
|
||||
{
|
||||
throw new ArgumentException("Invalid redirect URLs passed to Application.Create", nameof(redirectUrls));
|
||||
}
|
||||
|
||||
return new Application
|
||||
{
|
||||
ClientId = clientId,
|
||||
ClientSecret = clientSecret,
|
||||
Name = name,
|
||||
Scopes = scopes,
|
||||
RedirectUris = redirectUrls
|
||||
};
|
||||
}
|
||||
}
|
9
Hydra.Backend/Database/Models/Member.cs
Normal file
9
Hydra.Backend/Database/Models/Member.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Hydra.Backend.Database.Models;
|
||||
|
||||
public class Member : BaseModel
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
|
||||
public Ulid AccountId { get; init; }
|
||||
public Account Account { get; init; } = null!;
|
||||
}
|
17
Hydra.Backend/Database/Models/Token.cs
Normal file
17
Hydra.Backend/Database/Models/Token.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using NodaTime;
|
||||
|
||||
namespace Hydra.Backend.Database.Models;
|
||||
|
||||
public class Token : BaseModel
|
||||
{
|
||||
public required byte[] Hash { get; init; }
|
||||
public required Instant ExpiresAt { get; init; }
|
||||
public required string[] Scopes { get; init; }
|
||||
public bool ManuallyExpired { get; set; }
|
||||
|
||||
public Ulid UserId { get; init; }
|
||||
public Account Account { get; init; } = null!;
|
||||
|
||||
public Ulid ApplicationId { get; set; }
|
||||
public Application Application { get; set; } = null!;
|
||||
}
|
11
Hydra.Backend/Database/UlidConverter.cs
Normal file
11
Hydra.Backend/Database/UlidConverter.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace Hydra.Backend.Database;
|
||||
|
||||
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global",
|
||||
Justification = "Referenced in HydraContext.ConfigureConventions")]
|
||||
public class UlidConverter() : ValueConverter<Ulid, Guid>(
|
||||
convertToProviderExpression: x => x.ToGuid(),
|
||||
convertFromProviderExpression: x => new Ulid(x)
|
||||
);
|
Loading…
Add table
Add a link
Reference in a new issue