Browse Source

Bump MongoDB, remove CultureInfo from the Mongo entities and use [BsonElement] everywhere

pull/1488/head
Kévin Chalet 4 years ago
parent
commit
bb7a92fde6
  1. 10
      Packages.props
  2. 21
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs
  3. 5
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs
  4. 13
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs
  5. 2
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs
  6. 41
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs
  7. 10
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs
  8. 44
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs
  9. 4
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs

10
Packages.props

@ -31,8 +31,6 @@
<PackageReference Update="Microsoft.Owin.Security.OAuth" Version="4.2.2" />
<PackageReference Update="Microsoft.Owin.Testing" Version="4.2.2" />
<PackageReference Update="Microsoft.Web.Infrastructure" Version="1.0.0" />
<PackageReference Update="MongoDB.Bson" Version="2.10.4" />
<PackageReference Update="MongoDB.Driver" Version="2.10.4" />
<PackageReference Update="Moq" Version="4.18.1" />
<PackageReference Update="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Update="Polly.Extensions.Http" Version="3.0.0" />
@ -65,6 +63,8 @@
<PackageReference Update="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageReference Update="Microsoft.Extensions.Primitives" Version="2.1.6" />
<PackageReference Update="Microsoft.Extensions.WebEncoders" Version="2.1.1" />
<PackageReference Update="MongoDB.Bson" Version="2.11.6" />
<PackageReference Update="MongoDB.Driver" Version="2.11.6" />
<PackageReference Update="System.Collections.Immutable" Version="1.7.1" />
<PackageReference Update="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Update="System.Interactive" Version="4.1.1" />
@ -92,6 +92,8 @@
<PackageReference Update="Microsoft.Extensions.Options" Version="3.1.26" />
<PackageReference Update="Microsoft.Extensions.Primitives" Version="3.1.26" />
<PackageReference Update="Microsoft.Extensions.WebEncoders" Version="3.1.26" />
<PackageReference Update="MongoDB.Bson" Version="2.11.6" />
<PackageReference Update="MongoDB.Driver" Version="2.11.6" />
<PackageReference Update="System.Collections.Immutable" Version="1.7.1" />
<PackageReference Update="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Update="System.Interactive" Version="4.1.1" />
@ -118,6 +120,8 @@
<PackageReference Update="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Update="Microsoft.Extensions.Primitives" Version="5.0.1" />
<PackageReference Update="Microsoft.Extensions.WebEncoders" Version="5.0.17" />
<PackageReference Update="MongoDB.Bson" Version="2.16.1" />
<PackageReference Update="MongoDB.Driver" Version="2.16.1" />
<PackageReference Update="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Update="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Update="System.Interactive" Version="4.1.1" />
@ -144,6 +148,8 @@
<PackageReference Update="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Update="Microsoft.Extensions.Primitives" Version="6.0.0" />
<PackageReference Update="Microsoft.Extensions.WebEncoders" Version="6.0.6" />
<PackageReference Update="MongoDB.Bson" Version="2.16.1" />
<PackageReference Update="MongoDB.Driver" Version="2.16.1" />
<PackageReference Update="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Update="System.ComponentModel.Annotations" Version="6.0.0" />
<PackageReference Update="System.Interactive" Version="4.1.1" />

21
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.
/// </summary>
[BsonElement("display_names"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<CultureInfo, string> DisplayNames { get; set; }
= ImmutableDictionary.Create<CultureInfo, string>();
public virtual IReadOnlyDictionary<string, string>? DisplayNames { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary>
/// Gets or sets the unique identifier associated with the current application.
@ -64,14 +63,14 @@ public class OpenIddictMongoDbApplication
/// <summary>
/// Gets or sets the permissions associated with the current application.
/// </summary>
[BsonElement("permissions"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> Permissions { get; set; } = ImmutableList.Create<string>();
[BsonElement("permissions"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Permissions { get; set; } = ImmutableList.Create<string>();
/// <summary>
/// Gets or sets the logout callback URLs associated with the current application.
/// </summary>
[BsonElement("post_logout_redirect_uris"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> PostLogoutRedirectUris { get; set; } = ImmutableList.Create<string>();
[BsonElement("post_logout_redirect_uris"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? PostLogoutRedirectUris { get; set; } = ImmutableList.Create<string>();
/// <summary>
/// Gets or sets the additional properties associated with the current application.
@ -82,14 +81,14 @@ public class OpenIddictMongoDbApplication
/// <summary>
/// Gets or sets the callback URLs associated with the current application.
/// </summary>
[BsonElement("redirect_uris"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> RedirectUris { get; set; } = ImmutableList.Create<string>();
[BsonElement("redirect_uris"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? RedirectUris { get; set; } = ImmutableList.Create<string>();
/// <summary>
/// Gets or sets the requirements associated with the current application.
/// </summary>
[BsonElement("requirements"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> Requirements { get; set; } = ImmutableList.Create<string>();
[BsonElement("requirements"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Requirements { get; set; } = ImmutableList.Create<string>();
/// <summary>
/// Gets or sets the application type

5
src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs

@ -30,6 +30,7 @@ public class OpenIddictMongoDbAuthorization
/// <summary>
/// Gets or sets the UTC creation date of the current authorization.
/// </summary>
[BsonElement("creation_date"), BsonIgnoreIfNull]
public virtual DateTime? CreationDate { get; set; }
/// <summary>
@ -47,8 +48,8 @@ public class OpenIddictMongoDbAuthorization
/// <summary>
/// Gets or sets the scopes associated with the current authorization.
/// </summary>
[BsonElement("scopes"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> Scopes { get; set; } = ImmutableList.Create<string>();
[BsonElement("scopes"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Scopes { get; set; } = ImmutableList.Create<string>();
/// <summary>
/// Gets or sets the status of the current authorization.

13
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.
/// </summary>
[BsonElement("descriptions"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<CultureInfo, string> Descriptions { get; set; }
= ImmutableDictionary.Create<CultureInfo, string>();
public virtual IReadOnlyDictionary<string, string>? Descriptions { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary>
/// 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.
/// </summary>
[BsonElement("display_names"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<CultureInfo, string> DisplayNames { get; set; }
= ImmutableDictionary.Create<CultureInfo, string>();
public virtual IReadOnlyDictionary<string, string>? DisplayNames { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary>
/// Gets or sets the unique identifier associated with the current scope.
@ -69,6 +68,6 @@ public class OpenIddictMongoDbScope
/// <summary>
/// Gets or sets the resources associated with the current scope.
/// </summary>
[BsonElement("resources"), BsonIgnoreIfDefault]
public virtual IReadOnlyList<string> Resources { get; set; } = ImmutableList.Create<string>();
[BsonElement("resources"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Resources { get; set; } = ImmutableList.Create<string>();
}

2
src/OpenIddict.MongoDb.Models/OpenIddictMongoDbToken.cs

@ -88,7 +88,7 @@ public class OpenIddictMongoDbToken
/// <summary>
/// Gets or sets the subject associated with the current token.
/// </summary>
[BsonElement("subject"), BsonIgnoreIfDefault]
[BsonElement("subject"), BsonIgnoreIfNull]
public virtual string? Subject { get; set; }
/// <summary>

41
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs

@ -92,7 +92,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<TApplication> : IOpenIddictApplic
var collection = database.GetCollection<TApplication>(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<TApplication> : IOpenIddictApplic
var collection = database.GetCollection<TApplication>(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<TApplication> : 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<CultureInfo, string>());
}
return new(application.DisplayNames.ToImmutableDictionary());
return new(application.DisplayNames.ToImmutableDictionary(
pair => CultureInfo.GetCultureInfo(pair.Key),
pair => pair.Value));
}
/// <inheritdoc/>
@ -290,7 +292,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<string>());
}
@ -307,7 +309,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<string>());
}
@ -348,7 +350,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<string>());
}
@ -364,7 +366,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<string>());
}
@ -515,7 +517,16 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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<TApplication> : IOpenIddictApplic
if (permissions.IsDefaultOrEmpty)
{
application.Permissions = ImmutableList.Create<string>();
application.Permissions = null;
return default;
}
@ -551,7 +562,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : IOpenIddictApplic
if (addresses.IsDefaultOrEmpty)
{
application.PostLogoutRedirectUris = ImmutableList.Create<string>();
application.PostLogoutRedirectUris = null;
return default;
}
@ -611,7 +622,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : IOpenIddictApplic
if (addresses.IsDefaultOrEmpty)
{
application.RedirectUris = ImmutableList.Create<string>();
application.RedirectUris = null;
return default;
}
@ -632,7 +643,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : IOpenIddictApplic
if (requirements.IsDefaultOrEmpty)
{
application.Requirements = ImmutableList.Create<string>();
application.Requirements = null;
return default;
}
@ -660,7 +671,7 @@ public class OpenIddictMongoDbApplicationStore<TApplication> : 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));
}

10
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs

@ -91,7 +91,7 @@ public class OpenIddictMongoDbAuthorizationStore<TAuthorization> : 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<TAuthorization> : 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<TAuthorization> : 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<string>());
}
@ -653,7 +653,7 @@ public class OpenIddictMongoDbAuthorizationStore<TAuthorization> : IOpenIddictAu
if (scopes.IsDefaultOrEmpty)
{
authorization.Scopes = ImmutableList.Create<string>();
authorization.Scopes = null;
return default;
}
@ -720,7 +720,7 @@ public class OpenIddictMongoDbAuthorizationStore<TAuthorization> : 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));
}

44
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs

@ -92,7 +92,7 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
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<TScope> : IOpenIddictScopeStore<TScope>
var database = await Context.GetDatabaseAsync(cancellationToken);
var collection = database.GetCollection<TScope>(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<TScope> : IOpenIddictScopeStore<TScope>
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<CultureInfo, string>());
}
return new(scope.Descriptions.ToImmutableDictionary());
return new(scope.Descriptions.ToImmutableDictionary(
pair => CultureInfo.GetCultureInfo(pair.Key),
pair => pair.Value));
}
/// <inheritdoc/>
@ -234,12 +236,14 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
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<CultureInfo, string>());
}
return new(scope.DisplayNames.ToImmutableDictionary());
return new(scope.DisplayNames.ToImmutableDictionary(
pair => CultureInfo.GetCultureInfo(pair.Key),
pair => pair.Value));
}
/// <inheritdoc/>
@ -296,7 +300,7 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
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<string>());
}
@ -390,7 +394,16 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
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<TScope> : IOpenIddictScopeStore<TScope>
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<TScope> : IOpenIddictScopeStore<TScope>
if (resources.IsDefaultOrEmpty)
{
scope.Resources = ImmutableList.Create<string>();
scope.Resources = null;
return default;
}
@ -512,7 +534,7 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
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));
}

4
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs

@ -91,7 +91,7 @@ public class OpenIddictMongoDbTokenStore<TToken> : IOpenIddictTokenStore<TToken>
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<TToken> : IOpenIddictTokenStore<TToken>
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));
}

Loading…
Cancel
Save