-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multitenant messaging and identification configuration extensions (#61)
* Multitenant messaging and identification configuration extensions
- Loading branch information
Showing
15 changed files
with
236 additions
and
109 deletions.
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
...MicroServices/NBB.Contracts/NBB.Contracts.Api/MultiTenancy/ServiceCollectionExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
...roServices/NBB.Contracts/NBB.Contracts.Worker/MultiTenancy/ServiceCollectionExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
src/Messaging/NBB.Messaging.MultiTenancy/TenantIdentificationExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
src/MultiTenancy/NBB.MultiTenancy.Abstractions/Hosting/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using NBB.MultiTenancy.Abstractions.Options; | ||
using NBB.MultiTenancy.Abstractions.Services; | ||
|
||
namespace NBB.MultiTenancy.Abstractions.Hosting | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddHostingConfigValidation(this IServiceCollection services) | ||
private const string MessagingSectionName = "MultiTenancy"; | ||
|
||
public static void AddMultitenancy(this IServiceCollection services, IConfiguration configuration, | ||
Action<TenancyHostingOptions> addTenantAwareServices) | ||
{ | ||
var configurationSection = configuration.GetSection(MessagingSectionName); | ||
var tenancyOptions = configurationSection.Get<TenancyHostingOptions>(); | ||
if (tenancyOptions == null || tenancyOptions.TenancyType == TenancyType.None) | ||
{ | ||
return; | ||
} | ||
|
||
services.AddSingleton<IHostedService, TenancyHostingValidator>(); | ||
services.Configure<TenancyHostingOptions>(configurationSection); | ||
|
||
addTenantAwareServices(tenancyOptions); | ||
} | ||
|
||
public static void AddTenantHostingConfigService<TTenantHostingConfigService>(this IServiceCollection services) | ||
where TTenantHostingConfigService : class, ITenantHostingConfigService | ||
{ | ||
services.AddSingleton<ITenantHostingConfigService, TTenantHostingConfigService>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...iTenancy/NBB.MultiTenancy.Identification.Http/Extensions/DependencyInjectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NBB.MultiTenancy.Identification.Identifiers; | ||
using NBB.MultiTenancy.Identification.Extensions; | ||
|
||
|
||
namespace NBB.MultiTenancy.Identification.Http.Extensions | ||
{ | ||
public static class DependencyInjectionExtensions | ||
{ | ||
public static string DefaultTenantHttpHeaderName = "TenantId"; | ||
public static string DefaultTenantQueryStringParamName = "tenantId"; | ||
|
||
public static IServiceCollection AddDefaultHttpTenantIdentification(this IServiceCollection services) | ||
{ | ||
|
||
services.AddTenantIdentification() | ||
.AddTenantIdentificationStrategy<IdTenantIdentifier>(builder => builder | ||
.AddTenantTokenResolver<TenantIdHeaderHttpTokenResolver>(DefaultTenantHttpHeaderName) | ||
.AddTenantTokenResolver<QueryStringTenantIdTokenResolver>(DefaultTenantQueryStringParamName) | ||
); | ||
|
||
return services; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/MultiTenancy/NBB.MultiTenancy.Identification.Http/QueryStringTenantIdTokenResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
using NBB.MultiTenancy.Identification.Resolvers; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBB.MultiTenancy.Identification.Http | ||
{ | ||
public class QueryStringTenantIdTokenResolver : ITenantTokenResolver | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly string _parameterName; | ||
|
||
public QueryStringTenantIdTokenResolver(IHttpContextAccessor httpContextAccessor, string parameterName) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_parameterName = parameterName; | ||
} | ||
|
||
public Task<string> GetTenantToken() | ||
{ | ||
var parameterValues = _httpContextAccessor?.HttpContext?.Request?.Query[_parameterName]; | ||
if (!parameterValues.HasValue || parameterValues.Value == StringValues.Empty || parameterValues.Value.Count != 1) | ||
{ | ||
return Task.FromResult<string>(null); | ||
} | ||
|
||
return Task.FromResult(parameterValues.Value.ToString()); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ncy/NBB.MultiTenancy.Identification.Messaging/Extensions/DependencyInjectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NBB.Messaging.MultiTenancy; | ||
using NBB.MultiTenancy.Identification.Extensions; | ||
using NBB.MultiTenancy.Identification.Identifiers; | ||
|
||
namespace NBB.MultiTenancy.Identification.Messaging.Extensions | ||
{ | ||
public static class DependencyInjectionExtensions | ||
{ | ||
public static IServiceCollection AddDefaultMessagingTenantIdentification(this IServiceCollection services) | ||
{ | ||
|
||
services.AddTenantIdentification() | ||
.AddTenantIdentificationStrategy<IdTenantIdentifier>(builder => builder | ||
.AddTenantTokenResolver<TenantIdHeaderMessagingTokenResolver>(MessagingHeaders.TenantId)); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.