From bb7a92fde645bcfeb79c2603649af851321c3d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sat, 16 Jul 2022 16:01:55 +0200 Subject: [PATCH] Bump MongoDB, remove CultureInfo from the Mongo entities and use [BsonElement] everywhere --- Packages.props | 10 ++++- .../OpenIddictMongoDbApplication.cs | 21 +++++---- .../OpenIddictMongoDbAuthorization.cs | 5 ++- .../OpenIddictMongoDbScope.cs | 13 +++--- .../OpenIddictMongoDbToken.cs | 2 +- .../OpenIddictMongoDbApplicationStore.cs | 41 ++++++++++------- .../OpenIddictMongoDbAuthorizationStore.cs | 10 ++--- .../Stores/OpenIddictMongoDbScopeStore.cs | 44 ++++++++++++++----- .../Stores/OpenIddictMongoDbTokenStore.cs | 4 +- 9 files changed, 94 insertions(+), 56 deletions(-) diff --git a/Packages.props b/Packages.props index 72432fe5..02568e53 100644 --- a/Packages.props +++ b/Packages.props @@ -31,8 +31,6 @@ - - @@ -65,6 +63,8 @@ + + @@ -92,6 +92,8 @@ + + @@ -118,6 +120,8 @@ + + @@ -144,6 +148,8 @@ + + diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs index c0874330..bb50840b 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs @@ -6,7 +6,6 @@ using System.Collections.Immutable; using System.Diagnostics; -using System.Globalization; namespace OpenIddict.MongoDb.Models; @@ -52,8 +51,8 @@ public class OpenIddictMongoDbApplication /// Gets or sets the localized display names associated with the current application. /// [BsonElement("display_names"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary DisplayNames { get; set; } - = ImmutableDictionary.Create(); + public virtual IReadOnlyDictionary? DisplayNames { get; set; } + = ImmutableDictionary.Create(); /// /// Gets or sets the unique identifier associated with the current application. @@ -64,14 +63,14 @@ public class OpenIddictMongoDbApplication /// /// Gets or sets the permissions associated with the current application. /// - [BsonElement("permissions"), BsonIgnoreIfDefault] - public virtual IReadOnlyList Permissions { get; set; } = ImmutableList.Create(); + [BsonElement("permissions"), BsonIgnoreIfNull] + public virtual IReadOnlyList? Permissions { get; set; } = ImmutableList.Create(); /// /// Gets or sets the logout callback URLs associated with the current application. /// - [BsonElement("post_logout_redirect_uris"), BsonIgnoreIfDefault] - public virtual IReadOnlyList PostLogoutRedirectUris { get; set; } = ImmutableList.Create(); + [BsonElement("post_logout_redirect_uris"), BsonIgnoreIfNull] + public virtual IReadOnlyList? PostLogoutRedirectUris { get; set; } = ImmutableList.Create(); /// /// Gets or sets the additional properties associated with the current application. @@ -82,14 +81,14 @@ public class OpenIddictMongoDbApplication /// /// Gets or sets the callback URLs associated with the current application. /// - [BsonElement("redirect_uris"), BsonIgnoreIfDefault] - public virtual IReadOnlyList RedirectUris { get; set; } = ImmutableList.Create(); + [BsonElement("redirect_uris"), BsonIgnoreIfNull] + public virtual IReadOnlyList? RedirectUris { get; set; } = ImmutableList.Create(); /// /// Gets or sets the requirements associated with the current application. /// - [BsonElement("requirements"), BsonIgnoreIfDefault] - public virtual IReadOnlyList Requirements { get; set; } = ImmutableList.Create(); + [BsonElement("requirements"), BsonIgnoreIfNull] + public virtual IReadOnlyList? Requirements { get; set; } = ImmutableList.Create(); /// /// Gets or sets the application type diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs index c47550b8..d512b616 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs @@ -30,6 +30,7 @@ public class OpenIddictMongoDbAuthorization /// /// Gets or sets the UTC creation date of the current authorization. /// + [BsonElement("creation_date"), BsonIgnoreIfNull] public virtual DateTime? CreationDate { get; set; } /// @@ -47,8 +48,8 @@ public class OpenIddictMongoDbAuthorization /// /// Gets or sets the scopes associated with the current authorization. /// - [BsonElement("scopes"), BsonIgnoreIfDefault] - public virtual IReadOnlyList Scopes { get; set; } = ImmutableList.Create(); + [BsonElement("scopes"), BsonIgnoreIfNull] + public virtual IReadOnlyList? Scopes { get; set; } = ImmutableList.Create(); /// /// Gets or sets the status of the current authorization. diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs index ac15b5b6..641796c9 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs @@ -6,7 +6,6 @@ using System.Collections.Immutable; using System.Diagnostics; -using System.Globalization; namespace OpenIddict.MongoDb.Models; @@ -32,8 +31,8 @@ public class OpenIddictMongoDbScope /// Gets or sets the localized public descriptions associated with the current scope. /// [BsonElement("descriptions"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary Descriptions { get; set; } - = ImmutableDictionary.Create(); + public virtual IReadOnlyDictionary? Descriptions { get; set; } + = ImmutableDictionary.Create(); /// /// Gets or sets the display name associated with the current scope. @@ -45,8 +44,8 @@ public class OpenIddictMongoDbScope /// Gets or sets the localized display names associated with the current scope. /// [BsonElement("display_names"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary DisplayNames { get; set; } - = ImmutableDictionary.Create(); + public virtual IReadOnlyDictionary? DisplayNames { get; set; } + = ImmutableDictionary.Create(); /// /// Gets or sets the unique identifier associated with the current scope. @@ -69,6 +68,6 @@ public class OpenIddictMongoDbScope /// /// Gets or sets the resources associated with the current scope. /// - [BsonElement("resources"), BsonIgnoreIfDefault] - public virtual IReadOnlyList Resources { get; set; } = ImmutableList.Create(); + [BsonElement("resources"), BsonIgnoreIfNull] + public virtual IReadOnlyList? Resources { get; set; } = ImmutableList.Create(); } diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs index 68e4c4bd..f566ef68 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs @@ -88,7 +88,7 @@ public class OpenIddictMongoDbToken /// /// Gets or sets the subject associated with the current token. /// - [BsonElement("subject"), BsonIgnoreIfDefault] + [BsonElement("subject"), BsonIgnoreIfNull] public virtual string? Subject { get; set; } /// diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs index 7e040a4b..7d60beee 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs @@ -92,7 +92,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if ((await collection.DeleteOneAsync(entity => entity.Id == application.Id && - entity.ConcurrencyToken == application.ConcurrencyToken, cancellationToken)).DeletedCount == 0) + entity.ConcurrencyToken == application.ConcurrencyToken, cancellationToken)).DeletedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0239)); } @@ -152,7 +152,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); await foreach (var application in collection.Find(application => - application.PostLogoutRedirectUris.Contains(address)).ToAsyncEnumerable(cancellationToken)) + application.PostLogoutRedirectUris!.Contains(address)).ToAsyncEnumerable(cancellationToken)) { yield return application; } @@ -176,7 +176,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); await foreach (var application in collection.Find(application => - application.RedirectUris.Contains(address)).ToAsyncEnumerable(cancellationToken)) + application.RedirectUris!.Contains(address)).ToAsyncEnumerable(cancellationToken)) { yield return application; } @@ -262,12 +262,14 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - if (application.DisplayNames is null || application.DisplayNames.Count == 0) + if (application.DisplayNames is not { Count: > 0 }) { return new(ImmutableDictionary.Create()); } - return new(application.DisplayNames.ToImmutableDictionary()); + return new(application.DisplayNames.ToImmutableDictionary( + pair => CultureInfo.GetCultureInfo(pair.Key), + pair => pair.Value)); } /// @@ -290,7 +292,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - if (application.Permissions is null || application.Permissions.Count == 0) + if (application.Permissions is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -307,7 +309,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - if (application.PostLogoutRedirectUris is null || application.PostLogoutRedirectUris.Count == 0) + if (application.PostLogoutRedirectUris is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -348,7 +350,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - if (application.RedirectUris is null || application.RedirectUris.Count == 0) + if (application.RedirectUris is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -364,7 +366,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - if (application.Requirements is null || application.Requirements.Count == 0) + if (application.Requirements is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -515,7 +517,16 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic throw new ArgumentNullException(nameof(application)); } - application.DisplayNames = names; + if (names is not { Count: > 0 }) + { + application.DisplayNames = null; + + return default; + } + + application.DisplayNames = names.ToImmutableDictionary( + pair => pair.Key.Name, + pair => pair.Value); return default; } @@ -530,7 +541,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if (permissions.IsDefaultOrEmpty) { - application.Permissions = ImmutableList.Create(); + application.Permissions = null; return default; } @@ -551,7 +562,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if (addresses.IsDefaultOrEmpty) { - application.PostLogoutRedirectUris = ImmutableList.Create(); + application.PostLogoutRedirectUris = null; return default; } @@ -611,7 +622,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if (addresses.IsDefaultOrEmpty) { - application.RedirectUris = ImmutableList.Create(); + application.RedirectUris = null; return default; } @@ -632,7 +643,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if (requirements.IsDefaultOrEmpty) { - application.Requirements = ImmutableList.Create(); + application.Requirements = null; return default; } @@ -660,7 +671,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic if ((await collection.ReplaceOneAsync(entity => entity.Id == application.Id && - entity.ConcurrencyToken == timestamp, application, null as ReplaceOptions, cancellationToken)).MatchedCount == 0) + entity.ConcurrencyToken == timestamp, application, null as ReplaceOptions, cancellationToken)).MatchedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0239)); } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs index cb1c14eb..863c3fb6 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs @@ -91,7 +91,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu if ((await collection.DeleteOneAsync(entity => entity.Id == authorization.Id && - entity.ConcurrencyToken == authorization.ConcurrencyToken, cancellationToken)).DeletedCount == 0) + entity.ConcurrencyToken == authorization.ConcurrencyToken, cancellationToken)).DeletedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0241)); } @@ -251,7 +251,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu authorization.ApplicationId == ObjectId.Parse(client) && authorization.Status == status && authorization.Type == type && - Enumerable.All(scopes, scope => authorization.Scopes.Contains(scope))).ToAsyncEnumerable(cancellationToken)) + Enumerable.All(scopes, scope => authorization.Scopes!.Contains(scope))).ToAsyncEnumerable(cancellationToken)) { yield return authorization; } @@ -412,7 +412,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu throw new ArgumentNullException(nameof(authorization)); } - if (authorization.Scopes is null || authorization.Scopes.Count == 0) + if (authorization.Scopes is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -653,7 +653,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu if (scopes.IsDefaultOrEmpty) { - authorization.Scopes = ImmutableList.Create(); + authorization.Scopes = null; return default; } @@ -720,7 +720,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu if ((await collection.ReplaceOneAsync(entity => entity.Id == authorization.Id && - entity.ConcurrencyToken == timestamp, authorization, null as ReplaceOptions, cancellationToken)).MatchedCount == 0) + entity.ConcurrencyToken == timestamp, authorization, null as ReplaceOptions, cancellationToken)).MatchedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0241)); } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs index 498fe29a..486aac5e 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs @@ -92,7 +92,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore if ((await collection.DeleteOneAsync(entity => entity.Id == scope.Id && - entity.ConcurrencyToken == scope.ConcurrencyToken, cancellationToken)).DeletedCount == 0) + entity.ConcurrencyToken == scope.ConcurrencyToken, cancellationToken)).DeletedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0245)); } @@ -165,7 +165,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); - await foreach (var scope in collection.Find(scope => scope.Resources.Contains(resource)).ToAsyncEnumerable(cancellationToken)) + await foreach (var scope in collection.Find(scope => scope.Resources!.Contains(resource)).ToAsyncEnumerable(cancellationToken)) { yield return scope; } @@ -207,12 +207,14 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore throw new ArgumentNullException(nameof(scope)); } - if (scope.Descriptions is null || scope.Descriptions.Count == 0) + if (scope.Descriptions is not { Count: > 0 }) { return new(ImmutableDictionary.Create()); } - return new(scope.Descriptions.ToImmutableDictionary()); + return new(scope.Descriptions.ToImmutableDictionary( + pair => CultureInfo.GetCultureInfo(pair.Key), + pair => pair.Value)); } /// @@ -234,12 +236,14 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore throw new ArgumentNullException(nameof(scope)); } - if (scope.DisplayNames is null || scope.DisplayNames.Count == 0) + if (scope.DisplayNames is not { Count: > 0 }) { return new(ImmutableDictionary.Create()); } - return new(scope.DisplayNames.ToImmutableDictionary()); + return new(scope.DisplayNames.ToImmutableDictionary( + pair => CultureInfo.GetCultureInfo(pair.Key), + pair => pair.Value)); } /// @@ -296,7 +300,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore throw new ArgumentNullException(nameof(scope)); } - if (scope.Resources is null || scope.Resources.Count == 0) + if (scope.Resources is not { Count: > 0 }) { return new(ImmutableArray.Create()); } @@ -390,7 +394,16 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore throw new ArgumentNullException(nameof(scope)); } - scope.Descriptions = descriptions; + if (descriptions is not { Count: > 0 }) + { + scope.Descriptions = null; + + return default; + } + + scope.Descriptions = descriptions.ToImmutableDictionary( + pair => pair.Key.Name, + pair => pair.Value); return default; } @@ -404,7 +417,16 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore throw new ArgumentNullException(nameof(scope)); } - scope.DisplayNames = names; + if (names is not { Count: > 0 }) + { + scope.DisplayNames = null; + + return default; + } + + scope.DisplayNames = names.ToImmutableDictionary( + pair => pair.Key.Name, + pair => pair.Value); return default; } @@ -484,7 +506,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore if (resources.IsDefaultOrEmpty) { - scope.Resources = ImmutableList.Create(); + scope.Resources = null; return default; } @@ -512,7 +534,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore if ((await collection.ReplaceOneAsync(entity => entity.Id == scope.Id && - entity.ConcurrencyToken == timestamp, scope, null as ReplaceOptions, cancellationToken)).MatchedCount == 0) + entity.ConcurrencyToken == timestamp, scope, null as ReplaceOptions, cancellationToken)).MatchedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0245)); } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs index 68881a20..6244d143 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs @@ -91,7 +91,7 @@ public class OpenIddictMongoDbTokenStore : IOpenIddictTokenStore if ((await collection.DeleteOneAsync(entity => entity.Id == token.Id && - entity.ConcurrencyToken == token.ConcurrencyToken, cancellationToken)).DeletedCount == 0) + entity.ConcurrencyToken == token.ConcurrencyToken, cancellationToken)).DeletedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0247)); } @@ -807,7 +807,7 @@ public class OpenIddictMongoDbTokenStore : IOpenIddictTokenStore if ((await collection.ReplaceOneAsync(entity => entity.Id == token.Id && - entity.ConcurrencyToken == timestamp, token, null as ReplaceOptions, cancellationToken)).MatchedCount == 0) + entity.ConcurrencyToken == timestamp, token, null as ReplaceOptions, cancellationToken)).MatchedCount is 0) { throw new ConcurrencyException(SR.GetResourceString(SR.ID0247)); }