init
This commit is contained in:
commit
f6629fbb33
32 changed files with 1608 additions and 0 deletions
9
Foxchat.Identity/Authorization/AuthenticationHandler.cs
Normal file
9
Foxchat.Identity/Authorization/AuthenticationHandler.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Foxchat.Identity.Authorization;
|
||||
|
||||
public static class AuthenticationHandlerExtensions
|
||||
{
|
||||
public static void AddAuthenticationHandler(this IServiceCollection services)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
27
Foxchat.Identity/Controllers/NodeController.cs
Normal file
27
Foxchat.Identity/Controllers/NodeController.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using Foxchat.Core.Models;
|
||||
using Foxchat.Identity.Database;
|
||||
using Foxchat.Identity.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Foxchat.Identity.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("/_fox/ident/node")]
|
||||
public class NodeController(IdentityContext context, ChatInstanceResolverService chatInstanceResolverService) : ControllerBase
|
||||
{
|
||||
public const string SOFTWARE_NAME = "Foxchat.NET.Identity";
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetNode()
|
||||
{
|
||||
var instance = await context.GetInstanceAsync();
|
||||
return Ok(new NodeInfo(SOFTWARE_NAME, instance.PublicKey));
|
||||
}
|
||||
|
||||
[HttpGet("{domain}")]
|
||||
public async Task<IActionResult> GetChatNode(string domain)
|
||||
{
|
||||
var instance = await chatInstanceResolverService.ResolveChatInstanceAsync(domain);
|
||||
return Ok(instance);
|
||||
}
|
||||
}
|
63
Foxchat.Identity/Database/IdentityContext.cs
Normal file
63
Foxchat.Identity/Database/IdentityContext.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
using Foxchat.Core;
|
||||
using Foxchat.Core.Database;
|
||||
using Foxchat.Identity.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace Foxchat.Identity.Database;
|
||||
|
||||
public class IdentityContext : IDatabaseContext
|
||||
{
|
||||
private readonly string _connString;
|
||||
|
||||
public DbSet<Account> Accounts { get; set; }
|
||||
public DbSet<ChatInstance> ChatInstances { get; set; }
|
||||
public override DbSet<Instance> Instance { get; set; }
|
||||
public DbSet<Token> Tokens { get; set; }
|
||||
public DbSet<GuildAccount> GuildAccounts { get; set; }
|
||||
|
||||
public IdentityContext(InstanceConfig config)
|
||||
{
|
||||
_connString = new Npgsql.NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
Timeout = config.Database.Timeout ?? 5,
|
||||
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
|
||||
}.ConnectionString;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.UseNpgsql(_connString)
|
||||
.UseSnakeCaseNamingConvention();
|
||||
|
||||
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(a => a.Username).IsUnique();
|
||||
modelBuilder.Entity<Account>().HasIndex(a => a.Email).IsUnique();
|
||||
|
||||
modelBuilder.Entity<ChatInstance>().HasIndex(i => i.Domain).IsUnique();
|
||||
|
||||
modelBuilder.Entity<GuildAccount>().HasKey(g => new { g.ChatInstanceId, g.GuildId, g.AccountId });
|
||||
}
|
||||
}
|
||||
|
||||
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<IdentityContext>
|
||||
{
|
||||
public IdentityContext CreateDbContext(string[] args)
|
||||
{
|
||||
// Read the configuration file
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddConfiguration("identity.ini")
|
||||
.Build()
|
||||
// Get the configuration as our config class
|
||||
.Get<InstanceConfig>() ?? new();
|
||||
|
||||
return new IdentityContext(config);
|
||||
}
|
||||
}
|
21
Foxchat.Identity/Database/Models/Account.cs
Normal file
21
Foxchat.Identity/Database/Models/Account.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
namespace Foxchat.Identity.Database.Models;
|
||||
|
||||
public class Account
|
||||
{
|
||||
public Ulid Id { get; init; } = Ulid.NewUlid();
|
||||
public string Username { get; set; } = null!;
|
||||
public string Email { get; set; } = null!;
|
||||
public string Password { get; set; } = null!;
|
||||
public AccountRole Role { get; set; }
|
||||
|
||||
public string? Avatar { get; set; }
|
||||
|
||||
public List<Token> Tokens { get; } = [];
|
||||
public List<ChatInstance> ChatInstances { get; } = [];
|
||||
|
||||
public enum AccountRole
|
||||
{
|
||||
User,
|
||||
Admin,
|
||||
}
|
||||
}
|
19
Foxchat.Identity/Database/Models/ChatInstance.cs
Normal file
19
Foxchat.Identity/Database/Models/ChatInstance.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
namespace Foxchat.Identity.Database.Models;
|
||||
|
||||
public class ChatInstance
|
||||
{
|
||||
public Ulid Id { get; init; } = Ulid.NewUlid();
|
||||
public string Domain { get; init; } = null!;
|
||||
public string BaseUrl { get; set; } = null!;
|
||||
public string PublicKey { get; set; } = null!;
|
||||
public InstanceStatus Status { get; set; }
|
||||
public string? Reason { get; set; }
|
||||
|
||||
public List<Account> Accounts { get; } = [];
|
||||
|
||||
public enum InstanceStatus
|
||||
{
|
||||
Active,
|
||||
Suspended,
|
||||
}
|
||||
}
|
10
Foxchat.Identity/Database/Models/GuildAccount.cs
Normal file
10
Foxchat.Identity/Database/Models/GuildAccount.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Foxchat.Identity.Database.Models;
|
||||
|
||||
public class GuildAccount
|
||||
{
|
||||
public Ulid ChatInstanceId { get; init; }
|
||||
public ChatInstance ChatInstance { get; init; } = null!;
|
||||
public string GuildId { get; init; } = null!;
|
||||
public Ulid AccountId { get; init; }
|
||||
public Account Account { get; init; } = null!;
|
||||
}
|
8
Foxchat.Identity/Database/Models/Token.cs
Normal file
8
Foxchat.Identity/Database/Models/Token.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace Foxchat.Identity.Database.Models;
|
||||
|
||||
public class Token
|
||||
{
|
||||
public Ulid Id { get; init; } = Ulid.NewUlid();
|
||||
public Ulid AccountId { get; set; }
|
||||
public Account Account { get; set; } = null!;
|
||||
}
|
29
Foxchat.Identity/Foxchat.Identity.csproj
Normal file
29
Foxchat.Identity/Foxchat.Identity.csproj
Normal file
|
@ -0,0 +1,29 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Foxchat.Core\Foxchat.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
3
Foxchat.Identity/GlobalUsing.cs
Normal file
3
Foxchat.Identity/GlobalUsing.cs
Normal file
|
@ -0,0 +1,3 @@
|
|||
global using ILogger = Serilog.ILogger;
|
||||
global using Log = Serilog.Log;
|
||||
global using NUlid;
|
7
Foxchat.Identity/InstanceConfig.cs
Normal file
7
Foxchat.Identity/InstanceConfig.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
using Foxchat.Core;
|
||||
|
||||
namespace Foxchat.Identity;
|
||||
|
||||
public class InstanceConfig : CoreConfig
|
||||
{
|
||||
}
|
253
Foxchat.Identity/Migrations/20240512225835_Init.Designer.cs
generated
Normal file
253
Foxchat.Identity/Migrations/20240512225835_Init.Designer.cs
generated
Normal file
|
@ -0,0 +1,253 @@
|
|||
// <auto-generated />
|
||||
using System;
|
||||
using Foxchat.Identity.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Foxchat.Identity.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityContext))]
|
||||
[Migration("20240512225835_Init")]
|
||||
partial class Init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("AccountChatInstance", b =>
|
||||
{
|
||||
b.Property<Guid>("AccountsId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("accounts_id");
|
||||
|
||||
b.Property<Guid>("ChatInstancesId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("chat_instances_id");
|
||||
|
||||
b.HasKey("AccountsId", "ChatInstancesId")
|
||||
.HasName("pk_account_chat_instance");
|
||||
|
||||
b.HasIndex("ChatInstancesId")
|
||||
.HasDatabaseName("ix_account_chat_instance_chat_instances_id");
|
||||
|
||||
b.ToTable("account_chat_instance", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Core.Database.Instance", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("PrivateKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("private_key");
|
||||
|
||||
b.Property<string>("PublicKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("public_key");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_instance");
|
||||
|
||||
b.ToTable("instance", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Account", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<string>("Avatar")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("avatar");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("email");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("password");
|
||||
|
||||
b.Property<int>("Role")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("role");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("username");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_accounts");
|
||||
|
||||
b.HasIndex("Email")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_accounts_email");
|
||||
|
||||
b.HasIndex("Username")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_accounts_username");
|
||||
|
||||
b.ToTable("accounts", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.ChatInstance", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<string>("BaseUrl")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("base_url");
|
||||
|
||||
b.Property<string>("Domain")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("domain");
|
||||
|
||||
b.Property<string>("PublicKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("public_key");
|
||||
|
||||
b.Property<string>("Reason")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("reason");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("status");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_chat_instances");
|
||||
|
||||
b.HasIndex("Domain")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_chat_instances_domain");
|
||||
|
||||
b.ToTable("chat_instances", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.GuildAccount", b =>
|
||||
{
|
||||
b.Property<Guid>("ChatInstanceId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("chat_instance_id");
|
||||
|
||||
b.Property<string>("GuildId")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("guild_id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.HasKey("ChatInstanceId", "GuildId", "AccountId")
|
||||
.HasName("pk_guild_accounts");
|
||||
|
||||
b.HasIndex("AccountId")
|
||||
.HasDatabaseName("ix_guild_accounts_account_id");
|
||||
|
||||
b.ToTable("guild_accounts", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Token", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_tokens");
|
||||
|
||||
b.HasIndex("AccountId")
|
||||
.HasDatabaseName("ix_tokens_account_id");
|
||||
|
||||
b.ToTable("tokens", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AccountChatInstance", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("AccountsId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_account_chat_instance_accounts_accounts_id");
|
||||
|
||||
b.HasOne("Foxchat.Identity.Database.Models.ChatInstance", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("ChatInstancesId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_account_chat_instance_chat_instances_chat_instances_id");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.GuildAccount", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", "Account")
|
||||
.WithMany()
|
||||
.HasForeignKey("AccountId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_guild_accounts_accounts_account_id");
|
||||
|
||||
b.HasOne("Foxchat.Identity.Database.Models.ChatInstance", "ChatInstance")
|
||||
.WithMany()
|
||||
.HasForeignKey("ChatInstanceId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_guild_accounts_chat_instances_chat_instance_id");
|
||||
|
||||
b.Navigation("Account");
|
||||
|
||||
b.Navigation("ChatInstance");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Token", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", "Account")
|
||||
.WithMany("Tokens")
|
||||
.HasForeignKey("AccountId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_tokens_accounts_account_id");
|
||||
|
||||
b.Navigation("Account");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Account", b =>
|
||||
{
|
||||
b.Navigation("Tokens");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
181
Foxchat.Identity/Migrations/20240512225835_Init.cs
Normal file
181
Foxchat.Identity/Migrations/20240512225835_Init.cs
Normal file
|
@ -0,0 +1,181 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Foxchat.Identity.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "accounts",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
username = table.Column<string>(type: "text", nullable: false),
|
||||
email = table.Column<string>(type: "text", nullable: false),
|
||||
password = table.Column<string>(type: "text", nullable: false),
|
||||
role = table.Column<int>(type: "integer", nullable: false),
|
||||
avatar = table.Column<string>(type: "text", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_accounts", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "chat_instances",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
domain = table.Column<string>(type: "text", nullable: false),
|
||||
base_url = table.Column<string>(type: "text", nullable: false),
|
||||
public_key = table.Column<string>(type: "text", nullable: false),
|
||||
status = table.Column<int>(type: "integer", nullable: false),
|
||||
reason = table.Column<string>(type: "text", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_chat_instances", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "instance",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
public_key = table.Column<string>(type: "text", nullable: false),
|
||||
private_key = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_instance", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tokens",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
account_id = table.Column<Guid>(type: "uuid", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_tokens", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_tokens_accounts_account_id",
|
||||
column: x => x.account_id,
|
||||
principalTable: "accounts",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "account_chat_instance",
|
||||
columns: table => new
|
||||
{
|
||||
accounts_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
chat_instances_id = table.Column<Guid>(type: "uuid", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_account_chat_instance", x => new { x.accounts_id, x.chat_instances_id });
|
||||
table.ForeignKey(
|
||||
name: "fk_account_chat_instance_accounts_accounts_id",
|
||||
column: x => x.accounts_id,
|
||||
principalTable: "accounts",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_account_chat_instance_chat_instances_chat_instances_id",
|
||||
column: x => x.chat_instances_id,
|
||||
principalTable: "chat_instances",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "guild_accounts",
|
||||
columns: table => new
|
||||
{
|
||||
chat_instance_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
guild_id = table.Column<string>(type: "text", nullable: false),
|
||||
account_id = table.Column<Guid>(type: "uuid", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_guild_accounts", x => new { x.chat_instance_id, x.guild_id, x.account_id });
|
||||
table.ForeignKey(
|
||||
name: "fk_guild_accounts_accounts_account_id",
|
||||
column: x => x.account_id,
|
||||
principalTable: "accounts",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_guild_accounts_chat_instances_chat_instance_id",
|
||||
column: x => x.chat_instance_id,
|
||||
principalTable: "chat_instances",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_account_chat_instance_chat_instances_id",
|
||||
table: "account_chat_instance",
|
||||
column: "chat_instances_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_accounts_email",
|
||||
table: "accounts",
|
||||
column: "email",
|
||||
unique: true);
|
||||
|
||||
// EF Core doesn't support creating indexes on arbitrary expressions, so we have to create it manually.
|
||||
migrationBuilder.Sql("CREATE UNIQUE INDEX ix_accounts_username ON accounts (lower(username))");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_chat_instances_domain",
|
||||
table: "chat_instances",
|
||||
column: "domain",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_guild_accounts_account_id",
|
||||
table: "guild_accounts",
|
||||
column: "account_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tokens_account_id",
|
||||
table: "tokens",
|
||||
column: "account_id");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "account_chat_instance");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "guild_accounts");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "instance");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tokens");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "chat_instances");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "accounts");
|
||||
}
|
||||
}
|
||||
}
|
250
Foxchat.Identity/Migrations/IdentityContextModelSnapshot.cs
Normal file
250
Foxchat.Identity/Migrations/IdentityContextModelSnapshot.cs
Normal file
|
@ -0,0 +1,250 @@
|
|||
// <auto-generated />
|
||||
using System;
|
||||
using Foxchat.Identity.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Foxchat.Identity.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityContext))]
|
||||
partial class IdentityContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("AccountChatInstance", b =>
|
||||
{
|
||||
b.Property<Guid>("AccountsId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("accounts_id");
|
||||
|
||||
b.Property<Guid>("ChatInstancesId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("chat_instances_id");
|
||||
|
||||
b.HasKey("AccountsId", "ChatInstancesId")
|
||||
.HasName("pk_account_chat_instance");
|
||||
|
||||
b.HasIndex("ChatInstancesId")
|
||||
.HasDatabaseName("ix_account_chat_instance_chat_instances_id");
|
||||
|
||||
b.ToTable("account_chat_instance", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Core.Database.Instance", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("PrivateKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("private_key");
|
||||
|
||||
b.Property<string>("PublicKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("public_key");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_instance");
|
||||
|
||||
b.ToTable("instance", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Account", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<string>("Avatar")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("avatar");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("email");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("password");
|
||||
|
||||
b.Property<int>("Role")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("role");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("username");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_accounts");
|
||||
|
||||
b.HasIndex("Email")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_accounts_email");
|
||||
|
||||
b.HasIndex("Username")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_accounts_username");
|
||||
|
||||
b.ToTable("accounts", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.ChatInstance", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<string>("BaseUrl")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("base_url");
|
||||
|
||||
b.Property<string>("Domain")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("domain");
|
||||
|
||||
b.Property<string>("PublicKey")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("public_key");
|
||||
|
||||
b.Property<string>("Reason")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("reason");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("status");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_chat_instances");
|
||||
|
||||
b.HasIndex("Domain")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_chat_instances_domain");
|
||||
|
||||
b.ToTable("chat_instances", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.GuildAccount", b =>
|
||||
{
|
||||
b.Property<Guid>("ChatInstanceId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("chat_instance_id");
|
||||
|
||||
b.Property<string>("GuildId")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("guild_id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.HasKey("ChatInstanceId", "GuildId", "AccountId")
|
||||
.HasName("pk_guild_accounts");
|
||||
|
||||
b.HasIndex("AccountId")
|
||||
.HasDatabaseName("ix_guild_accounts_account_id");
|
||||
|
||||
b.ToTable("guild_accounts", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Token", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_tokens");
|
||||
|
||||
b.HasIndex("AccountId")
|
||||
.HasDatabaseName("ix_tokens_account_id");
|
||||
|
||||
b.ToTable("tokens", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AccountChatInstance", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("AccountsId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_account_chat_instance_accounts_accounts_id");
|
||||
|
||||
b.HasOne("Foxchat.Identity.Database.Models.ChatInstance", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("ChatInstancesId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_account_chat_instance_chat_instances_chat_instances_id");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.GuildAccount", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", "Account")
|
||||
.WithMany()
|
||||
.HasForeignKey("AccountId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_guild_accounts_accounts_account_id");
|
||||
|
||||
b.HasOne("Foxchat.Identity.Database.Models.ChatInstance", "ChatInstance")
|
||||
.WithMany()
|
||||
.HasForeignKey("ChatInstanceId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_guild_accounts_chat_instances_chat_instance_id");
|
||||
|
||||
b.Navigation("Account");
|
||||
|
||||
b.Navigation("ChatInstance");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Token", b =>
|
||||
{
|
||||
b.HasOne("Foxchat.Identity.Database.Models.Account", "Account")
|
||||
.WithMany("Tokens")
|
||||
.HasForeignKey("AccountId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_tokens_accounts_account_id");
|
||||
|
||||
b.Navigation("Account");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Foxchat.Identity.Database.Models.Account", b =>
|
||||
{
|
||||
b.Navigation("Tokens");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
55
Foxchat.Identity/Program.cs
Normal file
55
Foxchat.Identity/Program.cs
Normal file
|
@ -0,0 +1,55 @@
|
|||
using Newtonsoft.Json.Serialization;
|
||||
using Serilog;
|
||||
using Foxchat.Core;
|
||||
using Foxchat.Identity;
|
||||
using Foxchat.Identity.Database;
|
||||
using Foxchat.Identity.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var config = builder.AddConfiguration<InstanceConfig>("identity.ini");
|
||||
|
||||
builder.Services.AddSerilog(config.LogEventLevel);
|
||||
|
||||
await BuildInfo.ReadBuildInfo();
|
||||
Log.Information("Starting Foxchat.Identity {Version} ({Hash})", BuildInfo.Version, BuildInfo.Hash);
|
||||
|
||||
builder.Services
|
||||
.AddControllers()
|
||||
.AddNewtonsoftJson(options =>
|
||||
options.SerializerSettings.ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||
});
|
||||
|
||||
builder.Services
|
||||
.AddCoreServices<IdentityContext>()
|
||||
.AddScoped<ChatInstanceResolverService>()
|
||||
.AddEndpointsApiExplorer()
|
||||
.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseSerilogRequestLogging();
|
||||
app.UseRouting();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
using (var context = scope.ServiceProvider.GetRequiredService<IdentityContext>())
|
||||
{
|
||||
Log.Information("Initializing instance keypair...");
|
||||
if (await context.InitializeInstanceAsync())
|
||||
{
|
||||
Log.Information("Initialized instance keypair");
|
||||
}
|
||||
}
|
||||
|
||||
app.Urls.Clear();
|
||||
app.Urls.Add(config.Address);
|
||||
|
||||
app.Run();
|
39
Foxchat.Identity/Services/ChatInstanceResolverService.cs
Normal file
39
Foxchat.Identity/Services/ChatInstanceResolverService.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using Foxchat.Core.Federation;
|
||||
using Foxchat.Core.Models;
|
||||
using Foxchat.Identity.Database;
|
||||
using Foxchat.Identity.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Foxchat.Identity.Services;
|
||||
|
||||
public class ChatInstanceResolverService(ILogger logger, RequestSigningService requestSigningService, IdentityContext context, InstanceConfig config)
|
||||
{
|
||||
private readonly ILogger _logger = logger.ForContext<ChatInstanceResolverService>();
|
||||
|
||||
public async Task<ChatInstance> ResolveChatInstanceAsync(string domain)
|
||||
{
|
||||
var instance = await context.ChatInstances.Where(c => c.Domain == domain).FirstOrDefaultAsync();
|
||||
if (instance != null) return instance;
|
||||
|
||||
_logger.Information("Unknown chat instance {Domain}, fetching its data", domain);
|
||||
|
||||
var resp = await requestSigningService.RequestAsync<HelloResponse>(
|
||||
HttpMethod.Post,
|
||||
domain, "/_fox/chat/hello",
|
||||
userId: null,
|
||||
body: new HelloRequest(config.Domain)
|
||||
);
|
||||
|
||||
instance = new ChatInstance
|
||||
{
|
||||
Domain = domain,
|
||||
BaseUrl = $"https://{domain}",
|
||||
PublicKey = resp.PublicKey,
|
||||
Status = ChatInstance.InstanceStatus.Active,
|
||||
};
|
||||
await context.AddAsync(instance);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
15
Foxchat.Identity/identity.ini
Normal file
15
Foxchat.Identity/identity.ini
Normal file
|
@ -0,0 +1,15 @@
|
|||
Host = localhost
|
||||
Port = 7611
|
||||
Domain = id.fox.localhost
|
||||
|
||||
; The level to log things at. Valid settings: Verbose, Debug, Information, Warning, Error, Fatal
|
||||
LogEventLevel = Debug
|
||||
|
||||
[Database]
|
||||
; The database URL in ADO.NET format.
|
||||
Url = "Host=localhost;Database=foxchat_cs_ident;Username=foxchat;Password=password"
|
||||
|
||||
; The timeout for opening new connections. Defaults to 5.
|
||||
Timeout = 5
|
||||
; The maximum number of open connections. Defaults to 50.
|
||||
MaxPoolSize = 500
|
Loading…
Add table
Add a link
Reference in a new issue