feat(S6): add QQ identity resolution, DB-backed API keys and one-time bind codes

This commit is contained in:
2026-11-09 22:47:20 +08:00
parent 43ea1a9971
commit 052fe47db7
16 changed files with 875 additions and 58 deletions
+35 -1
View File
@@ -1,3 +1,4 @@
using System.Security.Claims;
using System.Text.Json;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
@@ -7,6 +8,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using OshimaBot.Data;
using OshimaBot.Messaging;
using OshimaBot.Models;
using OshimaBot.Security;
using OshimaBot.Services;
using Xunit;
@@ -30,7 +32,7 @@ public sealed class EventPipelineTests
private static QQEventDispatcher CreateDispatcher(
FakeDedup dedup, FakeAck ack, FakeSink sink) => new(
dedup, ack, sink, NullLogger<QQEventDispatcher>.Instance);
dedup, ack, sink, new FakeUserDirectory(), NullLogger<QQEventDispatcher>.Instance);
[Fact]
public async Task DedupStore_SecondMarkIsRejected()
@@ -77,6 +79,24 @@ public sealed class EventPipelineTests
Assert.Equal(1, dedup.MarkedCount);
}
[Fact]
public async Task Dispatcher_AttachesResolvedIdentityBeforeQueue()
{
FakeDedup dedup = new();
FakeSink sink = new();
FakeUserDirectory users = new();
QQEventDispatcher dispatcher = new(dedup, new FakeAck(), sink, users, NullLogger<QQEventDispatcher>.Instance);
EventDispatchResult result = await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.WebSocket);
Assert.Equal(EventDispatchStatus.Accepted, result.Status);
IBotMessage message = Assert.Single(sink.Messages);
Assert.Equal(42, message.FunGameUID);
Assert.NotNull(message.Principal);
Assert.Equal("42", message.Principal!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
Assert.Equal(1, users.Calls);
}
[Fact]
public async Task Dispatcher_RejectsDuplicateAcrossBothSources()
{
@@ -264,6 +284,20 @@ public sealed class EventPipelineTests
}
}
private sealed class FakeUserDirectory : IUserDirectory
{
public int Calls { get; private set; }
public Task<BotUserPrincipal> ResolveAsync(ExternalIdentity identity, CancellationToken cancellationToken = default)
{
Calls++;
return Task.FromResult(new BotUserPrincipal(42, "U000042", "", [BotRole.Player], false));
}
public Task<IdentityLinkStatus> LinkAsync(long userId, ExternalIdentity identity, CancellationToken cancellationToken = default)
=> Task.FromResult(IdentityLinkStatus.Linked);
}
private sealed class FakeSink : IBotMessageSink
{
public List<IBotMessage> Messages { get; } = [];