网站首页 网站源码

using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Threading;
using Dpz.Core.Public.Entity.Auth;
using OpenIddict.Abstractions;
namespace Dpz.Core.Service;
public class TokenStoreService<T>(IRepository<T> repository, ILogger<TokenStoreService<T>> logger)
: IOpenIddictTokenStore<T>
where T : AuthToken, new()
{
public async ValueTask<long> CountAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Counting tokens");
return await repository.MongodbQueryable.LongCountAsync(
cancellationToken: cancellationToken
);
}
public async ValueTask<long> CountAsync<TResult>(
Func<IQueryable<T>, IQueryable<TResult>> query,
CancellationToken cancellationToken
)
{
logger.LogInformation("Counting tokens with custom query");
return await query(repository.MongodbQueryable)
.LongCountAsync(cancellationToken: cancellationToken);
}
public async ValueTask CreateAsync(T token, CancellationToken cancellationToken)
{
logger.LogInformation("Creating token {Id}", token.Id);
await repository.InsertAsync(token, cancellationToken);
}
public async ValueTask DeleteAsync(T token, CancellationToken cancellationToken)
{
logger.LogInformation("Deleting token {Id}", token.Id);
await repository.DeleteAsync(token.Id, cancellationToken);
}
public IAsyncEnumerable<T> FindAsync(
string? subject,
string? client,
string? status,
string? type,
CancellationToken cancellationToken
)
{
logger.LogInformation(
"Find tokens subject={Subject}, client={Client}, status={Status}, type={Type}",
subject,
client,
status,
type
);
var filter = Builders<T>.Filter.Empty;
if (!string.IsNullOrWhiteSpace(subject))
{
filter &= Builders<T>.Filter.Eq(x => x.Subject, subject);
}
if (!string.IsNullOrWhiteSpace(client) && ObjectId.TryParse(client, out var appId))
{
filter &= Builders<T>.Filter.Eq(x => x.ApplicationId, appId);
}
if (!string.IsNullOrWhiteSpace(status))
{
filter &= Builders<T>.Filter.Eq(x => x.Status, status);
}
if (!string.IsNullOrWhiteSpace(type))
{
filter &= Builders<T>.Filter.Eq(x => x.Type, type);
}
return repository.SearchForAsync(filter, cancellationToken);
}
public IAsyncEnumerable<T> FindByApplicationIdAsync(
string identifier,
CancellationToken cancellationToken
)
{
logger.LogInformation("Find tokens by application id {Identifier}", identifier);
if (!ObjectId.TryParse(identifier, out var appId))
{
return AsyncEnumerable.Empty<T>();
}
var filter = Builders<T>.Filter.Eq(x => x.ApplicationId, appId);
return repository.SearchForAsync(filter, cancellationToken);
}
public IAsyncEnumerable<T> FindByAuthorizationIdAsync(
string identifier,
CancellationToken cancellationToken
)
{
logger.LogInformation("Find tokens by authorization id {Identifier}", identifier);
if (!ObjectId.TryParse(identifier, out var authId))
{
return AsyncEnumerable.Empty<T>();
}
var filter = Builders<T>.Filter.Eq(x => x.AuthorizationId, authId);
return repository.SearchForAsync(filter, cancellationToken);
}
public async ValueTask<T?> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
logger.LogInformation("Find token by id {Identifier}", identifier);
if (ObjectId.TryParse(identifier, out var id))
{
return await repository.FindAsync(id, cancellationToken);
}
return null;
}
public async ValueTask<T?> FindByReferenceIdAsync(
string identifier,
CancellationToken cancellationToken
)
{
logger.LogInformation("Find token by reference id {Identifier}", identifier);
return await repository
.SearchFor(x => x.ReferenceId == identifier)
.FirstOrDefaultAsync(cancellationToken);
}
public IAsyncEnumerable<T> FindBySubjectAsync(
string subject,
CancellationToken cancellationToken
)
{
logger.LogInformation("Find tokens by subject {Subject}", subject);
var filter = Builders<T>.Filter.Eq(x => x.Subject, subject);
return repository.SearchForAsync(filter, cancellationToken);
}
public async ValueTask<string?> GetApplicationIdAsync(
T token,
CancellationToken cancellationToken
)
{
return await ValueTask.FromResult(token.ApplicationId.ToString());
}
public async ValueTask<TResult?> GetAsync<TState, TResult>(
Func<IQueryable<T>, TState, IQueryable<TResult>> query,
TState state,
CancellationToken cancellationToken
)
{
return await Task.FromResult(query(repository.MongodbQueryable, state).FirstOrDefault());
}
public async ValueTask<string?> GetAuthorizationIdAsync(
T token,
CancellationToken cancellationToken
)
{
return await ValueTask.FromResult(token.AuthorizationId.ToString());
}
public async ValueTask<DateTimeOffset?> GetCreationDateAsync(
T token,
CancellationToken cancellationToken
)
{
var result = token.CreationDate is { } dt
? new DateTimeOffset(DateTime.SpecifyKind(dt, DateTimeKind.Utc))
: (DateTimeOffset?)null;
return await ValueTask.FromResult(result);
}
public async ValueTask<DateTimeOffset?> GetExpirationDateAsync(
T token,
CancellationToken cancellationToken
)
{
var result = token.ExpirationDate is { } dt
? new DateTimeOffset(DateTime.SpecifyKind(dt, DateTimeKind.Utc))
: (DateTimeOffset?)null;
return await ValueTask.FromResult(result);
}
public async ValueTask<string?> GetIdAsync(T token, CancellationToken cancellationToken)
{
return await ValueTask.FromResult(token.Id.ToString());
}
public async ValueTask<string?> GetPayloadAsync(T token, CancellationToken cancellationToken)
{
return await ValueTask.FromResult(token.Payload);
}
public async ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(
T token,
CancellationToken cancellationToken
)
{
if (token.Properties is null or { ElementCount: 0 })
{
return await ValueTask.FromResult(ImmutableDictionary<string, JsonElement>.Empty);
}
var json = token.Properties.ToJson();
using var doc = JsonDocument.Parse(json);
var builder = ImmutableDictionary.CreateBuilder<string, JsonElement>();
foreach (var prop in doc.RootElement.EnumerateObject())
{
builder[prop.Name] = prop.Value.Clone();
}
return await ValueTask.FromResult(builder.ToImmutable());
}
public async ValueTask<DateTimeOffset?> GetRedemptionDateAsync(
T token,
CancellationToken cancellationToken
)
{
var result = token.RedemptionDate is { } dt
? new DateTimeOffset(DateTime.SpecifyKind(dt, DateTimeKind.Utc))
: (DateTimeOffset?)null;
return await ValueTask.FromResult(result);
}
public async ValueTask<string?> GetReferenceIdAsync(
T token,
CancellationToken cancellationToken
)
{
return await ValueTask.FromResult(token.ReferenceId);
}
public async ValueTask<string?> GetStatusAsync(T token, CancellationToken cancellationToken)
{
return await ValueTask.FromResult(token.Status);
}
public async ValueTask<string?> GetSubjectAsync(T token, CancellationToken cancellationToken)
{
return await ValueTask.FromResult(token.Subject);
}
public async ValueTask<string?> GetTypeAsync(T token, CancellationToken cancellationToken)
{
return await ValueTask.FromResult(token.Type);
}
public async ValueTask<T> InstantiateAsync(CancellationToken cancellationToken)
{
return await ValueTask.FromResult(new T { Id = ObjectId.GenerateNewId() });
}
public IAsyncEnumerable<T> ListAsync(
int? count,
int? offset,
CancellationToken cancellationToken
)
{
logger.LogInformation(
"List tokens with pagination: offset={Offset}, count={Count}",
offset,
count
);
var options = new FindOptions<T>();
if (offset.HasValue)
{
options.Skip = offset.Value;
}
if (count.HasValue)
{
options.Limit = count.Value;
}
return repository.SearchForAsync(Builders<T>.Filter.Empty, options, cancellationToken);
}
public async IAsyncEnumerable<TResult> ListAsync<TState, TResult>(
Func<IQueryable<T>, TState, IQueryable<TResult>> query,
TState state,
[EnumeratorCancellation] CancellationToken cancellationToken
)
{
logger.LogInformation("List tokens with custom query state={State}", state);
var projected = query(repository.MongodbQueryable, state);
await foreach (
var element in projected.ToAsyncEnumerable().WithCancellation(cancellationToken)
)
{
yield return element;
}
}
public async ValueTask<long> PruneAsync(
DateTimeOffset threshold,
CancellationToken cancellationToken
)
{
logger.LogInformation("Prune tokens threshold={Threshold}", threshold);
var builder = Builders<T>.Filter;
var filter = builder.Or(
builder.Lt(x => x.ExpirationDate, threshold.UtcDateTime),
builder.Lt(x => x.RedemptionDate, threshold.UtcDateTime)
);
var result = await repository.Collection.DeleteManyAsync(filter, cancellationToken);
return result.DeletedCount;
}
public async ValueTask<long> RevokeAsync(
string? subject,
string? client,
string? status,
string? type,
CancellationToken cancellationToken
)
{
logger.LogInformation(
"Revoke tokens subject={Subject}, client={Client}, status={Status}, type={Type}",
subject,
client,
status,
type
);
var builder = Builders<T>.Filter;
var filter = builder.Empty;
if (!string.IsNullOrWhiteSpace(subject))
{
filter &= builder.Eq(x => x.Subject, subject);
}
if (!string.IsNullOrWhiteSpace(client) && ObjectId.TryParse(client, out var appId))
{
filter &= builder.Eq(x => x.ApplicationId, appId);
}
if (!string.IsNullOrWhiteSpace(status))
{
filter &= builder.Eq(x => x.Status, status);
}
if (!string.IsNullOrWhiteSpace(type))
{
filter &= builder.Eq(x => x.Type, type);
}
var update = Builders<T>.Update.Set(x => x.Status, OpenIddictConstants.Statuses.Revoked);
var result = await repository.Collection.UpdateManyAsync(
filter,
update,
cancellationToken: cancellationToken
);
return result.ModifiedCount;
}
public async ValueTask<long> RevokeByApplicationIdAsync(
string identifier,
CancellationToken cancellationToken = new CancellationToken()
)
{
logger.LogInformation("Revoke tokens by application id {Identifier}", identifier);
if (!ObjectId.TryParse(identifier, out var appId))
{
return 0L;
}
var filter = Builders<T>.Filter.Eq(x => x.ApplicationId, appId);
var update = Builders<T>.Update.Set(x => x.Status, OpenIddictConstants.Statuses.Revoked);
var result = await repository.Collection.UpdateManyAsync(
filter,
update,
cancellationToken: cancellationToken
);
return result.ModifiedCount;
}
public async ValueTask<long> RevokeByAuthorizationIdAsync(
string identifier,
CancellationToken cancellationToken
)
{
logger.LogInformation("Revoke tokens by authorization id {Identifier}", identifier);
if (!ObjectId.TryParse(identifier, out var authId))
{
return 0L;
}
var filter = Builders<T>.Filter.Eq(x => x.AuthorizationId, authId);
var update = Builders<T>.Update.Set(x => x.Status, OpenIddictConstants.Statuses.Revoked);
var result = await repository.Collection.UpdateManyAsync(
filter,
update,
cancellationToken: cancellationToken
);
return result.ModifiedCount;
}
public async ValueTask<long> RevokeBySubjectAsync(
string subject,
CancellationToken cancellationToken = new CancellationToken()
)
{
logger.LogInformation("Revoke tokens by subject {Subject}", subject);
var filter = Builders<T>.Filter.Eq(x => x.Subject, subject);
var update = Builders<T>.Update.Set(x => x.Status, OpenIddictConstants.Statuses.Revoked);
var result = await repository.Collection.UpdateManyAsync(
filter,
update,
cancellationToken: cancellationToken
);
return result.ModifiedCount;
}
public async ValueTask SetApplicationIdAsync(
T token,
string? identifier,
CancellationToken cancellationToken
)
{
if (ObjectId.TryParse(identifier, out var appId))
{
token.ApplicationId = appId;
}
await ValueTask.CompletedTask;
}
public async ValueTask SetAuthorizationIdAsync(
T token,
string? identifier,
CancellationToken cancellationToken
)
{
if (ObjectId.TryParse(identifier, out var authId))
{
token.AuthorizationId = authId;
}
await ValueTask.CompletedTask;
}
public async ValueTask SetCreationDateAsync(
T token,
DateTimeOffset? date,
CancellationToken cancellationToken
)
{
token.CreationDate = date?.UtcDateTime;
await ValueTask.CompletedTask;
}
public async ValueTask SetExpirationDateAsync(
T token,
DateTimeOffset? date,
CancellationToken cancellationToken
)
{
token.ExpirationDate = date?.UtcDateTime;
await ValueTask.CompletedTask;
}
public async ValueTask SetPayloadAsync(
T token,
string? payload,
CancellationToken cancellationToken
)
{
token.Payload = payload;
await ValueTask.CompletedTask;
}
public async ValueTask SetPropertiesAsync(
T token,
ImmutableDictionary<string, JsonElement> properties,
CancellationToken cancellationToken
)
{
if (properties.Count == 0)
{
token.Properties = null;
await ValueTask.CompletedTask;
return;
}
var bson = new BsonDocument();
foreach (var kv in properties)
{
bson[kv.Key] = BsonDocument.Parse(kv.Value.GetRawText());
}
token.Properties = bson;
await ValueTask.CompletedTask;
}
public async ValueTask SetRedemptionDateAsync(
T token,
DateTimeOffset? date,
CancellationToken cancellationToken
)
{
token.RedemptionDate = date?.UtcDateTime;
await ValueTask.CompletedTask;
}
public async ValueTask SetReferenceIdAsync(
T token,
string? identifier,
CancellationToken cancellationToken
)
{
token.ReferenceId = identifier;
await ValueTask.CompletedTask;
}
public async ValueTask SetStatusAsync(
T token,
string? status,
CancellationToken cancellationToken
)
{
token.Status = status;
await ValueTask.CompletedTask;
}
public async ValueTask SetSubjectAsync(
T token,
string? subject,
CancellationToken cancellationToken
)
{
token.Subject = subject;
await ValueTask.CompletedTask;
}
public async ValueTask SetTypeAsync(T token, string? type, CancellationToken cancellationToken)
{
token.Type = type;
await ValueTask.CompletedTask;
}
public async ValueTask UpdateAsync(T token, CancellationToken cancellationToken)
{
logger.LogInformation("Update token {Id}", token.Id);
await repository.UpdateAsync(token, cancellationToken);
}
}
上述代码定义了一个名为 TokenStoreService<T> 的类,它实现了 IOpenIddictTokenStore<T> 接口。这个类的主要功能是管理和操作与身份验证令牌相关的数据,通常用于 OAuth2 或 OpenID Connect 的实现。以下是代码的主要功能和结构的详细解释:
TokenStoreService<T> 是一个泛型类,T 必须是 AuthToken 的子类,并且具有无参数构造函数。IRepository<T> 接口的实例来进行数据存取操作,并使用 ILogger<TokenStoreService<T>> 进行日志记录。该类提供了一系列方法来管理令牌,包括:
CountAsync:计算存储中的令牌数量。CountAsync<TResult>:根据自定义查询计算令牌数量。CreateAsync:创建一个新的令牌并将其存储。DeleteAsync:根据令牌的 ID 删除令牌。FindAsync:根据多个条件(如主题、客户端、状态和类型)查找令牌。FindByApplicationIdAsync、FindByAuthorizationIdAsync、FindBySubjectAsync:根据应用程序 ID、授权 ID 或主题查找令牌。FindByIdAsync:根据令牌 ID 查找特定令牌。FindByReferenceIdAsync:根据引用 ID 查找令牌。GetApplicationIdAsync、GetAuthorizationIdAsync、GetCreationDateAsync、GetExpirationDateAsync 等方法用于获取令牌的各种属性。ListAsync:以分页的方式列出令牌。ListAsync<TState, TResult>:根据自定义查询列出令牌。PruneAsync:删除过期或已撤销的令牌。RevokeAsync、RevokeByApplicationIdAsync、RevokeByAuthorizationIdAsync、RevokeBySubjectAsync:根据不同条件撤销令牌。SetApplicationIdAsync、SetAuthorizationIdAsync、SetCreationDateAsync、SetExpirationDateAsync 等方法用于设置令牌的各种属性。UpdateAsync:更新现有的令牌。在每个方法中,使用 ILogger 记录相关操作的信息,例如创建、删除、查找和更新令牌的操作。这有助于在生产环境中进行调试和监控。
该类的方法大多数都是异步的,使用 ValueTask 和 IAsyncEnumerable,这使得它能够在高并发的环境中有效地处理请求。
TokenStoreService<T> 类是一个用于管理身份验证令牌的服务,提供了创建、查找、更新、删除和撤销令牌的功能,并且通过日志记录提供了操作的可追溯性。它的设计使得它能够与 MongoDB 等数据库进行交互,适用于需要处理大量身份验证令牌的应用程序。
