|
|
|
@ -40,69 +40,74 @@ namespace OpenIddict.MongoDb |
|
|
|
/// A <see cref="ValueTask{TResult}"/> that can be used to monitor the
|
|
|
|
/// asynchronous operation, whose result returns the MongoDB database.
|
|
|
|
/// </returns>
|
|
|
|
public async ValueTask<IMongoDatabase> GetDatabaseAsync(CancellationToken cancellationToken) |
|
|
|
public ValueTask<IMongoDatabase> GetDatabaseAsync(CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
if (_database != null) |
|
|
|
{ |
|
|
|
return _database; |
|
|
|
return new ValueTask<IMongoDatabase>(_database); |
|
|
|
} |
|
|
|
|
|
|
|
var options = _options.CurrentValue; |
|
|
|
if (options == null) |
|
|
|
async Task<IMongoDatabase> ExecuteAsync() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("The OpenIddict MongoDB options cannot be retrieved."); |
|
|
|
} |
|
|
|
|
|
|
|
var database = options.Database; |
|
|
|
if (database == null) |
|
|
|
{ |
|
|
|
database = _provider.GetService<IMongoDatabase>(); |
|
|
|
} |
|
|
|
var options = _options.CurrentValue; |
|
|
|
if (options == null) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("The OpenIddict MongoDB options cannot be retrieved."); |
|
|
|
} |
|
|
|
|
|
|
|
if (database == null) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException(new StringBuilder() |
|
|
|
.AppendLine("No suitable MongoDB database service can be found.") |
|
|
|
.Append("To configure the OpenIddict MongoDB stores to use a specific database, use ") |
|
|
|
.Append("'services.AddOpenIddict().AddCore().UseMongoDb().UseDatabase()' or register an ") |
|
|
|
.Append("'IMongoDatabase' in the dependency injection container in 'ConfigureServices()'.") |
|
|
|
.ToString()); |
|
|
|
} |
|
|
|
var database = options.Database; |
|
|
|
if (database == null) |
|
|
|
{ |
|
|
|
database = _provider.GetService<IMongoDatabase>(); |
|
|
|
} |
|
|
|
|
|
|
|
// Note: the cancellation token passed as a parameter is deliberately not used here to ensure
|
|
|
|
// the cancellation of a single store operation doesn't prevent the indexes from being created.
|
|
|
|
var applications = database.GetCollection<OpenIddictApplication>(options.ApplicationsCollectionName); |
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.ClientId), |
|
|
|
new CreateIndexOptions |
|
|
|
if (database == null) |
|
|
|
{ |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
throw new InvalidOperationException(new StringBuilder() |
|
|
|
.AppendLine("No suitable MongoDB database service can be found.") |
|
|
|
.Append("To configure the OpenIddict MongoDB stores to use a specific database, use ") |
|
|
|
.Append("'services.AddOpenIddict().AddCore().UseMongoDb().UseDatabase()' or register an ") |
|
|
|
.Append("'IMongoDatabase' in the dependency injection container in 'ConfigureServices()'.") |
|
|
|
.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.PostLogoutRedirectUris)); |
|
|
|
// Note: the cancellation token passed as a parameter is deliberately not used here to ensure
|
|
|
|
// the cancellation of a single store operation doesn't prevent the indexes from being created.
|
|
|
|
var applications = database.GetCollection<OpenIddictApplication>(options.ApplicationsCollectionName); |
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.ClientId), |
|
|
|
new CreateIndexOptions |
|
|
|
{ |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
|
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.RedirectUris)); |
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.PostLogoutRedirectUris)); |
|
|
|
|
|
|
|
var scopes = database.GetCollection<OpenIddictScope>(options.ScopesCollectionName); |
|
|
|
await scopes.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictScope>.IndexKeys.Ascending(scope => scope.Name), |
|
|
|
new CreateIndexOptions |
|
|
|
{ |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
await applications.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.RedirectUris)); |
|
|
|
|
|
|
|
var tokens = database.GetCollection<OpenIddictToken>(options.TokensCollectionName); |
|
|
|
await tokens.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictToken>.IndexKeys.Ascending(token => token.ReferenceId), |
|
|
|
new CreateIndexOptions<OpenIddictToken> |
|
|
|
{ |
|
|
|
PartialFilterExpression = Builders<OpenIddictToken>.Filter.Exists(token => token.ReferenceId), |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
var scopes = database.GetCollection<OpenIddictScope>(options.ScopesCollectionName); |
|
|
|
await scopes.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictScope>.IndexKeys.Ascending(scope => scope.Name), |
|
|
|
new CreateIndexOptions |
|
|
|
{ |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
|
|
|
|
var tokens = database.GetCollection<OpenIddictToken>(options.TokensCollectionName); |
|
|
|
await tokens.Indexes.CreateOneAsync( |
|
|
|
Builders<OpenIddictToken>.IndexKeys.Ascending(token => token.ReferenceId), |
|
|
|
new CreateIndexOptions<OpenIddictToken> |
|
|
|
{ |
|
|
|
PartialFilterExpression = Builders<OpenIddictToken>.Filter.Exists(token => token.ReferenceId), |
|
|
|
Unique = true |
|
|
|
}); |
|
|
|
|
|
|
|
return _database = database; |
|
|
|
} |
|
|
|
|
|
|
|
return _database = database; |
|
|
|
return new ValueTask<IMongoDatabase>(ExecuteAsync()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|