feat: modularize external providers

This commit is contained in:
2026-07-27 17:47:21 +08:00
parent db4c7b4496
commit 70b99e6063
47 changed files with 1219 additions and 667 deletions

View File

@@ -1,6 +1,7 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Tiku.Application.Auth;
using Tiku.Application.Tenancy;
using Tiku.Domain.Identity;
using Tiku.Domain.Tenancy;
using Tiku.Infrastructure.Persistence;
@@ -12,12 +13,13 @@ public sealed class AuthService(
IPasswordHasher passwordHasher,
ISmsVerificationService smsVerificationService,
ISessionService sessionService,
IWechatOAuthClient wechatOAuthClient) : IAuthService
IWechatOAuthClient wechatOAuthClient,
ITenantExternalProviderConfigService providerConfigService) : IAuthService
{
private const string PasswordProvider = "password";
private const string SmsProvider = "sms";
private const string WechatWebProvider = "wechat_web";
private const string WechatMiniAppProvider = "wechat-miniapp";
private const string WechatMiniAppProvider = "wechat_miniapp";
private static readonly string[] WechatWebProviderAliases = ["wechat_web", "wechat-web", "wechat"];
private static readonly string[] WechatMiniAppProviderAliases = ["wechat-miniapp", "wechat_miniapp", "wechat-mini", "wechatMiniapp"];
private static readonly string[] WechatIdentityProviders = ["wechat_web", "wechat-web", "wechat", "wechat-miniapp", "wechat_miniapp", "wechat-mini", "wechatMiniapp"];
@@ -356,24 +358,30 @@ public sealed class AuthService(
IReadOnlyList<string> aliases,
CancellationToken cancellationToken)
{
var rows = await dbContext.TenantAuthProviders
.Where(entity =>
entity.TenantId == tenantId &&
aliases.Contains(entity.Provider) &&
(entity.Status == TenantAuthProviderStatus.Active ||
entity.Status == TenantAuthProviderStatus.Testing))
.ToListAsync(cancellationToken);
var row = aliases
.Select(alias => rows.FirstOrDefault(entity => entity.Provider == alias))
.FirstOrDefault(entity => entity is not null);
TenantExternalProviderAccount? account = null;
foreach (var alias in aliases)
{
try
{
account = await providerConfigService.GetActiveProviderAsync(
tenantId,
TenantExternalProviderCapability.Identity,
alias,
cancellationToken);
break;
}
catch (TenantExternalProviderException)
{
}
}
if (row is null)
if (account is null)
{
throw new AuthProviderNotConfiguredException(provider);
}
var appId = GetJsonString(row.ConfigPublic, "appId", "clientId");
var appSecret = GetJsonString(row.ConfigPublic, "appSecret", "clientSecret", "secret");
var appId = GetJsonString(account.ConfigPublic, "appId", "clientId");
var appSecret = GetJsonString(account.SecretPayload, "appSecret", "clientSecret", "secret");
if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(appSecret))
{
throw new AuthProviderNotConfiguredException(provider);