From 530a3658166bd0068caa66021b7a087a2bc393cb Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 29 Mar 2022 11:20:56 +0800 Subject: [PATCH] Add MongoDB Repositories --- .../MongoDB/IOpenIddictMongoDbContext.cs | 12 -- .../MongoDB/OpenIddictMongoDbContext.cs | 19 --- .../OpenIddictMongoDbContextExtensions.cs | 13 -- .../MongoDB/OpenIddictMongoDbModule.cs | 22 ---- .../Volo.Abp.OpenIddict.MongoDB.csproj | 2 +- .../AbpOpenIddictQueryableExtensions.cs | 27 +++++ .../MongoOpenIddictApplicationRepository.cs | 62 ++++++++++ .../MongoOpenIddictAuthorizationRepository.cs | 90 ++++++++++++++ .../MongoDB/IOpenIddictMongoDbContext.cs | 21 ++++ .../MongoDB/OpenIddictMongoDbContext.cs | 28 +++++ .../OpenIddictMongoDbContextExtensions.cs | 36 ++++++ .../MongoDB/OpenIddictMongoDbModule.cs | 29 +++++ .../Scopes/MongoOpenIddictScopeRepository.cs | 67 +++++++++++ .../Tokens/MongoOpenIddictTokenRepository.cs | 113 ++++++++++++++++++ 14 files changed, 474 insertions(+), 67 deletions(-) delete mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/IOpenIddictMongoDbContext.cs delete mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContext.cs delete mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContextExtensions.cs delete mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbModule.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/AbpOpenIddictQueryableExtensions.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Applications/MongoOpenIddictApplicationRepository.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/IOpenIddictMongoDbContext.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContext.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContextExtensions.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbModule.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Scopes/MongoOpenIddictScopeRepository.cs create mode 100644 modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Tokens/MongoOpenIddictTokenRepository.cs diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/IOpenIddictMongoDbContext.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/IOpenIddictMongoDbContext.cs deleted file mode 100644 index 8c43ae6416..0000000000 --- a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/IOpenIddictMongoDbContext.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Volo.Abp.Data; -using Volo.Abp.MongoDB; - -namespace Volo.Abp.OpenIddict.MongoDB; - -[ConnectionStringName(OpenIddictDbProperties.ConnectionStringName)] -public interface IOpenIddictMongoDbContext : IAbpMongoDbContext -{ - /* Define mongo collections here. Example: - * IMongoCollection Questions { get; } - */ -} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContext.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContext.cs deleted file mode 100644 index 1338ed6f7f..0000000000 --- a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContext.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Volo.Abp.Data; -using Volo.Abp.MongoDB; - -namespace Volo.Abp.OpenIddict.MongoDB; - -[ConnectionStringName(OpenIddictDbProperties.ConnectionStringName)] -public class OpenIddictMongoDbContext : AbpMongoDbContext, IOpenIddictMongoDbContext -{ - /* Add mongo collections here. Example: - * public IMongoCollection Questions => Collection(); - */ - - protected override void CreateModel(IMongoModelBuilder modelBuilder) - { - base.CreateModel(modelBuilder); - - modelBuilder.ConfigureOpenIddict(); - } -} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContextExtensions.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContextExtensions.cs deleted file mode 100644 index ca9d7ca238..0000000000 --- a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbContextExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Volo.Abp; -using Volo.Abp.MongoDB; - -namespace Volo.Abp.OpenIddict.MongoDB; - -public static class OpenIddictMongoDbContextExtensions -{ - public static void ConfigureOpenIddict( - this IMongoModelBuilder builder) - { - Check.NotNull(builder, nameof(builder)); - } -} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbModule.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbModule.cs deleted file mode 100644 index 72884db133..0000000000 --- a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/MongoDB/OpenIddictMongoDbModule.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Modularity; -using Volo.Abp.MongoDB; - -namespace Volo.Abp.OpenIddict.MongoDB; - -[DependsOn( - typeof(OpenIddictDomainModule), - typeof(AbpMongoDbModule) - )] -public class OpenIddictMongoDbModule : AbpModule -{ - public override void ConfigureServices(ServiceConfigurationContext context) - { - context.Services.AddMongoDbContext(options => - { - /* Add custom repositories here. Example: - * options.AddRepository(); - */ - }); - } -} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo.Abp.OpenIddict.MongoDB.csproj b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo.Abp.OpenIddict.MongoDB.csproj index 06e2838061..ff155124b1 100644 --- a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo.Abp.OpenIddict.MongoDB.csproj +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo.Abp.OpenIddict.MongoDB.csproj @@ -4,7 +4,7 @@ - netstandard2.0 + net6.0 diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/AbpOpenIddictQueryableExtensions.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/AbpOpenIddictQueryableExtensions.cs new file mode 100644 index 0000000000..2bea2427e1 --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/AbpOpenIddictQueryableExtensions.cs @@ -0,0 +1,27 @@ +using System.Linq; +using JetBrains.Annotations; + +namespace Volo.Abp.OpenIddict; + +internal static class AbpOpenIddictQueryableExtensions +{ + public static TQueryable SkipIf([NotNull] this TQueryable query, bool condition, int count) + where TQueryable : IQueryable + { + Check.NotNull(query, nameof(query)); + + return condition + ? (TQueryable)query.Skip(count) + : query; + } + + public static TQueryable TakeIf([NotNull] this TQueryable query, bool condition, int count) + where TQueryable : IQueryable + { + Check.NotNull(query, nameof(query)); + + return condition + ? (TQueryable)query.Take(count) + : query; + } +} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Applications/MongoOpenIddictApplicationRepository.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Applications/MongoOpenIddictApplicationRepository.cs new file mode 100644 index 0000000000..c36a3b50df --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Applications/MongoOpenIddictApplicationRepository.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.MongoDB; + +namespace Volo.Abp.OpenIddict.Applications; + +public class MongoOpenIddictApplicationRepository : MongoDbRepository, IOpenIddictApplicationRepository +{ + public MongoOpenIddictApplicationRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken)) + .As>() + .LongCountAsync(GetCancellationToken(cancellationToken)); + } + + public async Task FindByClientIdAsync(string clientId, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .FirstOrDefaultAsync(x => x.ClientId == clientId, cancellationToken: GetCancellationToken(cancellationToken)); + } + + public async Task> FindByPostLogoutRedirectUriAsync(string address, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).Where(x => x.PostLogoutRedirectUris.Contains(address)).ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task> FindByRedirectUriAsync(string address, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).Where(x => x.RedirectUris.Contains(address)).ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task GetAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + } + + public async Task> ListAsync(int? count, int? offset, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .OrderBy(x => x.Id) + .SkipIf>(offset.HasValue, offset.Value) + .TakeIf>(count.HasValue, count.Value) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task> ListAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().ToListAsync(GetCancellationToken(cancellationToken)); + } +} \ No newline at end of file diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs new file mode 100644 index 0000000000..de174f345f --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using OpenIddict.Abstractions; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.MongoDB; + +namespace Volo.Abp.OpenIddict.Authorizations; + +public class MongoOpenIddictAuthorizationRepository : MongoDbRepository, IOpenIddictAuthorizationRepository +{ + public MongoOpenIddictAuthorizationRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public virtual async Task CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken)).As>().LongCountAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .Where(x => x.Subject == subject && x.ApplicationId == client) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, string status, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .Where(x => x.Subject == subject && x.Status == status && x.ApplicationId == client) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, string status, string type, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .Where(x => x.Subject == subject && x.Status == status && x.Type == type && x.ApplicationId == client) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindByApplicationIdAsync(Guid applicationId, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).Where(x => x.ApplicationId == applicationId).ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task FindByIdAsync(Guid id, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.Id == id, GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindBySubjectAsync(string subject, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).Where(x => x.Subject == subject).ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(int? count, int? offset, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .OrderBy(authorization => authorization.Id!) + .SkipIf>(offset.HasValue, offset.Value) + .TakeIf>(count.HasValue, count.Value) + .As>().ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().ToListAsync(cancellationToken); + } + + public virtual async Task> GetPruneListAsync(DateTime date, int count, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)) + .Where(x => x.CreationDate < date) + .Where(x => x.Status != OpenIddictConstants.Statuses.Valid || (x.Type == OpenIddictConstants.AuthorizationTypes.AdHoc && !x.Tokens.Any())) + .OrderBy(x => x.Id) + .Take(count) + .ToListAsync(cancellationToken); + } +} \ No newline at end of file diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/IOpenIddictMongoDbContext.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/IOpenIddictMongoDbContext.cs new file mode 100644 index 0000000000..a5501cbf39 --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/IOpenIddictMongoDbContext.cs @@ -0,0 +1,21 @@ +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.Applications; +using Volo.Abp.OpenIddict.Authorizations; +using Volo.Abp.OpenIddict.Scopes; +using Volo.Abp.OpenIddict.Tokens; + +namespace Volo.Abp.OpenIddict.MongoDB; + +[ConnectionStringName(OpenIddictDbProperties.ConnectionStringName)] +public interface IOpenIddictMongoDbContext : IAbpMongoDbContext +{ + IMongoCollection Applications { get; } + + IMongoCollection Authorizations { get; } + + IMongoCollection Scopes { get; } + + IMongoCollection Tokens { get; } +} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContext.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContext.cs new file mode 100644 index 0000000000..895b951e4b --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContext.cs @@ -0,0 +1,28 @@ +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.Applications; +using Volo.Abp.OpenIddict.Authorizations; +using Volo.Abp.OpenIddict.Scopes; +using Volo.Abp.OpenIddict.Tokens; + +namespace Volo.Abp.OpenIddict.MongoDB; + +[ConnectionStringName(OpenIddictDbProperties.ConnectionStringName)] +public class OpenIddictMongoDbContext : AbpMongoDbContext, IOpenIddictMongoDbContext +{ + public IMongoCollection Applications => Collection(); + + public IMongoCollection Authorizations => Collection(); + + public IMongoCollection Scopes => Collection(); + + public IMongoCollection Tokens => Collection(); + + protected override void CreateModel(IMongoModelBuilder modelBuilder) + { + base.CreateModel(modelBuilder); + + modelBuilder.ConfigureOpenIddict(); + } +} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContextExtensions.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContextExtensions.cs new file mode 100644 index 0000000000..4da581e6ab --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbContextExtensions.cs @@ -0,0 +1,36 @@ +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.Applications; +using Volo.Abp.OpenIddict.Authorizations; +using Volo.Abp.OpenIddict.Scopes; +using Volo.Abp.OpenIddict.Tokens; + +namespace Volo.Abp.OpenIddict.MongoDB; + +public static class OpenIddictMongoDbContextExtensions +{ + public static void ConfigureOpenIddict( + this IMongoModelBuilder builder) + { + Check.NotNull(builder, nameof(builder)); + + builder.Entity(b => + { + b.CollectionName = OpenIddictDbProperties.DbTablePrefix + "Applications"; + }); + + builder.Entity(b => + { + b.CollectionName = OpenIddictDbProperties.DbTablePrefix + "Authorizations"; + }); + + builder.Entity(b => + { + b.CollectionName = OpenIddictDbProperties.DbTablePrefix + "Scopes"; + }); + + builder.Entity(b => + { + b.CollectionName = OpenIddictDbProperties.DbTablePrefix + "Tokens"; + }); + } +} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbModule.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbModule.cs new file mode 100644 index 0000000000..5d31d1f843 --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/MongoDB/OpenIddictMongoDbModule.cs @@ -0,0 +1,29 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.Applications; +using Volo.Abp.OpenIddict.Authorizations; +using Volo.Abp.OpenIddict.Scopes; +using Volo.Abp.OpenIddict.Tokens; + +namespace Volo.Abp.OpenIddict.MongoDB; + +[DependsOn( + typeof(OpenIddictDomainModule), + typeof(AbpMongoDbModule) + )] +public class OpenIddictMongoDbModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddMongoDbContext(options => + { + options.AddDefaultRepositories(); + + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + }); + } +} diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Scopes/MongoOpenIddictScopeRepository.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Scopes/MongoOpenIddictScopeRepository.cs new file mode 100644 index 0000000000..861762f93a --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Scopes/MongoOpenIddictScopeRepository.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.MongoDB; + +namespace Volo.Abp.OpenIddict.Scopes; + +public class MongoOpenIddictScopeRepository : MongoDbRepository, IOpenIddictScopeRepository +{ + public MongoOpenIddictScopeRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public virtual async Task CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken)).As>().LongCountAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task FindByIdAsync(Guid id, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.Id == id, cancellationToken: GetCancellationToken(cancellationToken)); + } + + public virtual async Task FindByNameAsync(string name, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.Name == name, cancellationToken: GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindByNamesAsync(string[] names, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => names.Contains(x.Name)) + .As>() + .ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindByResourceAsync(string resource, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.Resources.Contains(resource)) + .As>() + .ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(int? count, int? offset, CancellationToken cancellationToken) + { + return await Queryable.OrderBy((await GetMongoQueryableAsync(cancellationToken)), x => x.Id) + .SkipIf>(offset.HasValue, offset.Value) + .TakeIf>(count.HasValue, count.Value) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().ToListAsync(GetCancellationToken(cancellationToken)); + } +} \ No newline at end of file diff --git a/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Tokens/MongoOpenIddictTokenRepository.cs b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Tokens/MongoOpenIddictTokenRepository.cs new file mode 100644 index 0000000000..f5d81f92dd --- /dev/null +++ b/modules/OpenIddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Tokens/MongoOpenIddictTokenRepository.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using OpenIddict.Abstractions; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Abp.OpenIddict.Authorizations; +using Volo.Abp.OpenIddict.MongoDB; + +namespace Volo.Abp.OpenIddict.Tokens; + +public class MongoOpenIddictTokenRepository : MongoDbRepository, IOpenIddictTokenRepository +{ + public MongoOpenIddictTokenRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public virtual async Task CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).LongCountAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.Subject == subject && x.ApplicationId == client) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, string status, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.Subject == subject && x.ApplicationId == client && x.Status == status) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindAsync(string subject, Guid client, string status, string type, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.Subject == subject && x.ApplicationId == client && x.Status == status && x.Type == type) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindByApplicationIdAsync(Guid applicationId, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.ApplicationId == applicationId) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindByAuthorizationIdAsync(Guid authorizationId, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.AuthorizationId == authorizationId) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task FindByIdAsync(Guid id, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.Id == id, GetCancellationToken(cancellationToken)); + } + + public virtual async Task FindByReferenceIdAsync(string referenceId, CancellationToken cancellationToken) + { + return await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.ReferenceId == referenceId, GetCancellationToken(cancellationToken)); + } + + public virtual async Task> FindBySubjectAsync(string subject, CancellationToken cancellationToken) + { + return await Queryable.Where((await GetMongoQueryableAsync(cancellationToken)), x => x.Subject == subject) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(int? count, int? offset, CancellationToken cancellationToken) + { + return await Queryable.OrderBy((await GetMongoQueryableAsync(cancellationToken)), x => x.Id) + .SkipIf>(offset.HasValue, offset.Value) + .TakeIf>(count.HasValue, count.Value) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task> ListAsync(Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) + { + return await query(await GetMongoQueryableAsync(cancellationToken), state).As>().ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task> GetPruneListAsync(DateTime date, int count, CancellationToken cancellationToken) + { + //TODO: Test & Improve? + return await (from token in await GetMongoQueryableAsync(cancellationToken) + join authorization in (await GetMongoQueryableAsync(cancellationToken)) + on token.AuthorizationId equals authorization.Id into ta + from a in ta + where token.CreationDate < date + where (token.Status != OpenIddictConstants.Statuses.Inactive && + token.Status != OpenIddictConstants.Statuses.Valid) || + (a != null && a.Status != OpenIddictConstants.Statuses.Valid) || + token.ExpirationDate < DateTime.UtcNow + orderby token.Id + select token).Take(count).ToListAsync(GetCancellationToken(cancellationToken)); + } +} \ No newline at end of file