新增 ISimApiTokenResolver 自定义Token解析扩展
GetLogin 优先按注册顺序执行 TokenResolver 链, 非null即短路, 全部为null才回退内置缓存; resolver 每次调用在独立作用域内解析并即时释放
This commit is contained in:
+12
-1
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Interfaces;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace SimApi.Helpers;
|
||||
@@ -18,14 +20,16 @@ public class SimApiAuth
|
||||
|
||||
private readonly IDistributedCache _cache;
|
||||
private readonly IDatabase? _redisDb;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
// InMemory 模式:用户 → Token集合
|
||||
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, byte>> _userTokens = new();
|
||||
|
||||
public SimApiAuth(IDistributedCache cache, IServiceProvider sp)
|
||||
public SimApiAuth(IDistributedCache cache, IServiceProvider sp, IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_cache = cache;
|
||||
_redisDb = sp.GetService<IConnectionMultiplexer>()?.GetDatabase();
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -88,6 +92,13 @@ public class SimApiAuth
|
||||
/// <returns></returns>
|
||||
public SimApiLoginItem? GetLogin(string token)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope(); // 用完即释放
|
||||
foreach (var resolver in scope.ServiceProvider.GetServices<ISimApiTokenResolver>())
|
||||
{
|
||||
var item = resolver.Run(token);
|
||||
if (item != null) return item;
|
||||
}
|
||||
|
||||
var cacheKey = TokenCacheKey.Replace("{token}", token);
|
||||
var login = _cache.GetString(cacheKey);
|
||||
var resp = login != null ? SimApiUtil.FromJson<SimApiLoginItem>(login) : null;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using SimApi.Communications;
|
||||
|
||||
namespace SimApi.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 自定义 Token 解析扩展点,用于让 <c>SimApiAuth.GetLogin</c> 识别本库之外签发的 Token(JWT、第三方登录态等)。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <b>注册</b>:本库不会自动发现实现,必须由使用者自行注册。<c>AddScoped</c> / <c>AddSingleton</c> / <c>AddTransient</c>
|
||||
/// 均可,因为每次调用都在独立作用域内解析。仅在 <c>EnableSimApiAuth = true</c> 时才会被执行。<br/>
|
||||
/// <b>调用时机</b>:每次 <c>GetLogin</c>(即每个携带 Token 的请求)都会先执行全部 resolver,再回退到内置缓存查询;
|
||||
/// 因此本机 <c>Login</c> 签发的 Token 同样会走一遍 resolver 链。<c>Logout</c> 与 <c>GetAllLogins</c> 内部也调用
|
||||
/// <c>GetLogin</c>,同样会触发 resolver 链,其中 <c>GetAllLogins</c> 会按该用户的 Token 数量执行多次。<br/>
|
||||
/// <b>顺序</b>:按注册顺序依次执行,返回非 <c>null</c> 即作为认证结果并立即短路,后续 resolver 与内置缓存不再参与。<br/>
|
||||
/// <b>生命周期</b>:每次调用在根容器新建的子作用域内解析,因此可以安全注入 Scoped 服务(如 DbContext)。
|
||||
/// 但该作用域不是当前请求的作用域,请求级事务 / UnitOfWork 的未提交改动不可见,实现应当只读。<br/>
|
||||
/// <b>返回值</b>:必须是完全物化的对象。作用域在返回后立即释放,不可返回依赖延迟加载的 EF 追踪实体。<br/>
|
||||
/// <b>Token 管理</b>:返回的登录态不受本库管理,<c>Login</c> / <c>Update</c> / <c>GetAllLogins</c> 不会包含它,
|
||||
/// <c>Logout</c> 对它无效,吊销需由实现方自行处理。<br/>
|
||||
/// <b>异常</b>:抛出的异常会直接冒泡并中断整条链(该请求认证失败)。表示“不是我的 Token”请返回 <c>null</c>,不要抛异常。<br/>
|
||||
/// <b>性能</b>:这是认证热路径上的同步方法,查库等 I/O 会阻塞调用线程,建议在实现内部自行做短 TTL 缓存。
|
||||
/// </remarks>
|
||||
public interface ISimApiTokenResolver
|
||||
{
|
||||
/// <summary>
|
||||
/// 尝试解析 Token。
|
||||
/// </summary>
|
||||
/// <param name="token">请求携带的 Token 原文。</param>
|
||||
/// <returns>解析成功返回登录信息;不属于本实现的 Token 返回 <c>null</c> 由后续 resolver 继续处理。</returns>
|
||||
public SimApiLoginItem? Run(string token);
|
||||
}
|
||||
@@ -78,7 +78,7 @@ SimApi/
|
||||
├── Configurations/ # SimApiOptions + 各模块 Option 类
|
||||
├── Controllers/ # SimApiBaseController, SimApiCommonController, SimApiAuthController
|
||||
├── Helpers/ # SimApiError, SimApiAuth, SimApiCache, SimApiHttpClient, SimApiStorage, SimApiUtil, SimApiAesUtil
|
||||
├── Interfaces/ # ISimApiAuthChecker
|
||||
├── Interfaces/ # ISimApiAuthChecker, ISimApiTokenResolver
|
||||
├── Middlewares/ # SimApiExceptionMiddleware, SimApiAuthMiddleware
|
||||
├── Synapse/ # MQTT Pub/Sub + RPC + Config Store
|
||||
├── Exceptions/ # SimApiException
|
||||
@@ -221,6 +221,33 @@ public interface ISimApiAuthChecker {
|
||||
// 实现后自动注册为 Scoped,每次认证后调用
|
||||
```
|
||||
|
||||
### 自定义 Token 解析 — ISimApiTokenResolver
|
||||
|
||||
让认证识别**本库之外签发的 Token**(JWT、第三方登录态等),是 `GetLogin` 的前置扩展点。
|
||||
|
||||
```csharp
|
||||
public interface ISimApiTokenResolver {
|
||||
SimApiLoginItem? Run(string token); // 不是自己负责的 Token 返回 null
|
||||
}
|
||||
```
|
||||
|
||||
**必须由使用者手动注册**,本库不会自动发现实现(`AddScoped` / `AddSingleton` / `AddTransient` 均可):
|
||||
|
||||
```csharp
|
||||
builder.Services.AddScoped<ISimApiTokenResolver, MyJwtTokenResolver>();
|
||||
```
|
||||
|
||||
调用行为:
|
||||
|
||||
- 每次 `GetLogin`(即每个携带 Token 的请求)都会**先**按注册顺序执行全部 resolver,返回非 `null` 即短路生效;全部返回 `null` 才回退到内置缓存查询
|
||||
- 因此本机 `Login()` 签发的 Token 同样会走一遍 resolver 链,实现应保持廉价,或自行做短 TTL 缓存
|
||||
- `Logout` 与 `GetAllLogins` 内部也会调用 `GetLogin`,同样会触发 resolver 链,其中 `GetAllLogins` 会按该用户的 Token 数量执行多次
|
||||
- 每次调用在新建的子作用域内解析,可以注入 Scoped 服务(如 `DbContext`);但它**不是当前请求的作用域**,请求级事务 / UnitOfWork 的未提交改动不可见,实现应当只读
|
||||
- 返回值必须是完全物化的对象,作用域在返回后立即释放,不可返回依赖延迟加载的 EF 追踪实体
|
||||
- 返回的登录态**不受本库管理**:`Login` / `Update` / `GetAllLogins` 不会包含它,`Logout` 对它无效,吊销需由实现方自行处理
|
||||
- 抛出的异常会中断整条链并导致该请求认证失败;表示"不是我的 Token"请返回 `null`,不要抛异常
|
||||
- 仅在 `EnableSimApiAuth = true` 时才会被执行
|
||||
|
||||
---
|
||||
|
||||
## 4. AuthSDK — Auth Center 网关 & IAM
|
||||
|
||||
Reference in New Issue
Block a user