Browse Source

Merge pull request #68 from abpframework/gterdem/gateway_auth_enh

eShopOnAbp: Gateway multiple api definition authorization
pull/70/head
Galip Tolga Erdem 5 years ago
committed by GitHub
parent
commit
04e4d11dcf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      apps/auth-server/src/EShopOnAbp.AuthServer/appsettings.json
  2. 24
      gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs
  3. 4
      gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json
  4. 17
      gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs
  5. 6
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs
  6. 2
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/appsettings.json
  7. 2
      services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json
  8. 2
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json
  9. 100
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServerDataSeeder.cs
  10. 26
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs
  11. 2
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/appsettings.json
  12. 2
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/appsettings.json
  13. 2
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/appsettings.json

2
apps/auth-server/src/EShopOnAbp.AuthServer/appsettings.json

@ -14,7 +14,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "AccountService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"AllowedHosts": "*", "AllowedHosts": "*",

24
gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs

@ -24,7 +24,21 @@ namespace EShopOnAbp.WebPublicGateway
var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment(); var hostingEnvironment = context.Services.GetHostingEnvironment();
SwaggerConfigurationHelper.Configure(context, "WebPublic Gateway"); SwaggerWithAuthConfigurationHelper.Configure(
context: context,
authority: configuration["AuthServer:Authority"],
scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
{
{ "AccountService", "Account Service API" },
{ "IdentityService", "Identity Service API" },
{ "AdministrationService", "Administration Service API" },
{ "CatalogService", "Catalog Service API" },
{ "BasketService", "Basket Service API" },
{ "PaymentService", "Payment Service API" },
{ "OrderingService", "Ordering Service API" },
},
apiTitle: "WebPublic Gateway"
);
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override void OnApplicationInitialization(ApplicationInitializationContext context)
@ -59,12 +73,8 @@ namespace EShopOnAbp.WebPublicGateway
} }
options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API");
// options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
// options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
// TODO: Find a way to get these configurations from related running applications settings.
// options.OAuthClientId($"{config.ServiceKey.Replace(" ","")}_Swagger");
// options.OAuthClientSecret("1q2w3e*");
} }
}); });
app.MapWhen( app.MapWhen(

4
gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json

@ -5,7 +5,9 @@
}, },
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true" "RequireHttpsMetadata": "true",
"SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*"
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {

17
gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs

@ -24,7 +24,22 @@ namespace EShopOnAbp.WebGateway
{ {
var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment(); var hostingEnvironment = context.Services.GetHostingEnvironment();
SwaggerConfigurationHelper.Configure(context, "Web Gateway");
SwaggerWithAuthConfigurationHelper.Configure(
context: context,
authority: configuration["AuthServer:Authority"],
scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
{
{ "AccountService", "Account Service API" },
{ "IdentityService", "Identity Service API" },
{ "AdministrationService", "Administration Service API" },
{ "CatalogService", "Catalog Service API" },
{ "BasketService", "Basket Service API" },
{ "PaymentService", "Payment Service API" },
{ "OrderingService", "Ordering Service API" },
},
apiTitle: "Web Gateway"
);
context.Services.AddCors(options => context.Services.AddCors(options =>
{ {

6
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs

@ -7,6 +7,7 @@ using EShopOnAbp.Shared.Hosting.AspNetCore;
using EShopOnAbp.Shared.Hosting.Microservices; using EShopOnAbp.Shared.Hosting.Microservices;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Volo.Abp; using Volo.Abp;
@ -40,7 +41,7 @@ namespace EShopOnAbp.AdministrationService
authority: configuration["AuthServer:Authority"], authority: configuration["AuthServer:Authority"],
scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */ scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
{ {
{"Administration", "Administration Service API"}, {"AdministrationService", "Administration Service API"},
}, },
apiTitle: "Administration Service API" apiTitle: "Administration Service API"
); );
@ -85,7 +86,10 @@ namespace EShopOnAbp.AdministrationService
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(options => app.UseSwaggerUI(options =>
{ {
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Administration Service API"); options.SwaggerEndpoint("/swagger/v1/swagger.json", "Administration Service API");
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
}); });
app.UseAbpSerilogEnrichers(); app.UseAbpSerilogEnrichers();
app.UseAuditing(); app.UseAuditing();

2
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "AdministrationService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"RemoteServices": { "RemoteServices": {

2
services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "BasketService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"RemoteServices": { "RemoteServices": {

2
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "CatalogService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"Logging": { "Logging": {

100
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServerDataSeeder.cs

@ -105,29 +105,16 @@ namespace EShopOnAbp.IdentityService.DbMigrations
private async Task CreateSwaggerClientsAsync() private async Task CreateSwaggerClientsAsync()
{ {
await CreateSwaggerClientAsync("AccountService", await CreateWebGatewaySwaggerClientAsync("WebGateway",
new[] { "AccountService"}); new[]
{
await CreateSwaggerClientAsync("IdentityService", "AccountService", "IdentityService", "AdministrationService",
new[] { "IdentityService"}); "CatalogService", "BasketService",
"PaymentService", "OrderingService"
await CreateSwaggerClientAsync("AdministrationService", });
new[] { "AdministrationService" });
await CreateSwaggerClientAsync("CatalogService",
new[] { "CatalogService" });
await CreateSwaggerClientAsync("BasketService",
new[] { "BasketService" });
await CreateSwaggerClientAsync("OrderingService",
new[] { "OrderingService" });
await CreateSwaggerClientAsync("PaymentService",
new[] { "PaymentService" });
} }
private async Task CreateSwaggerClientAsync(string name, string[] scopes = null) private async Task CreateWebGatewaySwaggerClientAsync(string name, string[] scopes = null)
{ {
var commonScopes = new[] var commonScopes = new[]
{ {
@ -138,22 +125,51 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"phone", "phone",
"address" "address"
}; };
scopes ??= new[] { name }; scopes ??= new[] {name};
// Swagger Client // Swagger Client
var swaggerClientId = $"{name}_Swagger"; var swaggerClientId = $"{name}_Swagger";
if (!swaggerClientId.IsNullOrWhiteSpace()) if (!swaggerClientId.IsNullOrWhiteSpace())
{ {
var swaggerRootUrl = _configuration[$"IdentityServerClients:{name}:RootUrl"].TrimEnd('/'); var webGatewaySwaggerRootUrl = _configuration[$"IdentityServerClients:{name}:RootUrl"].TrimEnd('/');
var publicWebGatewayRootUrl = _configuration[$"IdentityServerClients:PublicWebGateway:RootUrl"].TrimEnd('/');
var accountServiceRootUrl = _configuration[$"IdentityServerClients:AccountService:RootUrl"].TrimEnd('/');
var identityServiceRootUrl = _configuration[$"IdentityServerClients:IdentityService:RootUrl"].TrimEnd('/');
var administrationServiceRootUrl = _configuration[$"IdentityServerClients:AdministrationService:RootUrl"].TrimEnd('/');
var catalogServiceRootUrl = _configuration[$"IdentityServerClients:CatalogService:RootUrl"].TrimEnd('/');
var basketServiceRootUrl = _configuration[$"IdentityServerClients:BasketService:RootUrl"].TrimEnd('/');
var orderingServiceRootUrl = _configuration[$"IdentityServerClients:OrderingService:RootUrl"].TrimEnd('/');
var paymentServiceRootUrl = _configuration[$"IdentityServerClients:PaymentService:RootUrl"].TrimEnd('/');
await CreateClientAsync( await CreateClientAsync(
name: swaggerClientId, name: swaggerClientId,
scopes: commonScopes.Union(scopes), scopes: commonScopes.Union(scopes),
grantTypes: new[] { "authorization_code" }, grantTypes: new[] {"authorization_code"},
secret: "1q2w3e*".Sha256(), secret: "1q2w3e*".Sha256(),
requireClientSecret: false, requireClientSecret: false,
redirectUri: $"{swaggerRootUrl}/swagger/oauth2-redirect.html", redirectUris: new List<string>
corsOrigins: new[] { swaggerRootUrl.RemovePostFix("/") } {
$"{webGatewaySwaggerRootUrl}/swagger/oauth2-redirect.html", // WebGateway redirect uri
$"{publicWebGatewayRootUrl}/swagger/oauth2-redirect.html", // PublicWebGateway redirect uri
$"{accountServiceRootUrl}/swagger/oauth2-redirect.html", // AccountService redirect uri
$"{identityServiceRootUrl}/swagger/oauth2-redirect.html", // IdentityService redirect uri
$"{administrationServiceRootUrl}/swagger/oauth2-redirect.html", // AdministrationService redirect uri
$"{catalogServiceRootUrl}/swagger/oauth2-redirect.html", // CatalogService redirect uri
$"{basketServiceRootUrl}/swagger/oauth2-redirect.html", // BasketService redirect uri
$"{orderingServiceRootUrl}/swagger/oauth2-redirect.html", // OrderingService redirect uri
$"{paymentServiceRootUrl}/swagger/oauth2-redirect.html" // PaymentService redirect uri
},
corsOrigins: new[]
{
webGatewaySwaggerRootUrl.RemovePostFix("/"),
publicWebGatewayRootUrl.RemovePostFix("/"),
accountServiceRootUrl.RemovePostFix("/"),
administrationServiceRootUrl.RemovePostFix("/"),
catalogServiceRootUrl.RemovePostFix("/"),
basketServiceRootUrl.RemovePostFix("/"),
orderingServiceRootUrl.RemovePostFix("/"),
paymentServiceRootUrl.RemovePostFix("/")
}
); );
} }
} }
@ -186,7 +202,7 @@ namespace EShopOnAbp.IdentityService.DbMigrations
private async Task<ApiScope> CreateApiScopeAsync(string name) private async Task<ApiScope> CreateApiScopeAsync(string name)
{ {
var apiScope = await _apiScopeRepository.FindByNameAsync(name ); var apiScope = await _apiScopeRepository.FindByNameAsync(name);
if (apiScope == null) if (apiScope == null)
{ {
apiScope = await _apiScopeRepository.InsertAsync( apiScope = await _apiScopeRepository.InsertAsync(
@ -228,12 +244,12 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"PaymentService", "PaymentService",
"OrderingService" "OrderingService"
}), }),
grantTypes: new[] { "hybrid" }, grantTypes: new[] {"hybrid"},
secret: "1q2w3e*".Sha256(), secret: "1q2w3e*".Sha256(),
redirectUri: $"{publicWebClientRootUrl}signin-oidc", redirectUris: new List<string>{ $"{publicWebClientRootUrl}signin-oidc" },
postLogoutRedirectUri: $"{publicWebClientRootUrl}signout-callback-oidc", postLogoutRedirectUri: $"{publicWebClientRootUrl}signout-callback-oidc",
frontChannelLogoutUri: $"{publicWebClientRootUrl}Account/FrontChannelLogout", frontChannelLogoutUri: $"{publicWebClientRootUrl}Account/FrontChannelLogout",
corsOrigins: new[] { publicWebClientRootUrl.RemovePostFix("/") } corsOrigins: new[] {publicWebClientRootUrl.RemovePostFix("/")}
); );
//Angular Client //Angular Client
@ -247,13 +263,13 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"IdentityService", "IdentityService",
"AdministrationService" "AdministrationService"
}), }),
grantTypes: new[] { "authorization_code", "LinkLogin", "password" }, grantTypes: new[] {"authorization_code", "LinkLogin", "password"},
secret: "1q2w3e*".Sha256(), secret: "1q2w3e*".Sha256(),
requirePkce: true, requirePkce: true,
requireClientSecret: false, requireClientSecret: false,
redirectUri: $"{angularClientRootUrl}", redirectUris: new List<string>{ $"{angularClientRootUrl}" },
postLogoutRedirectUri: $"{angularClientRootUrl}", postLogoutRedirectUri: $"{angularClientRootUrl}",
corsOrigins: new[] { angularClientRootUrl } corsOrigins: new[] {angularClientRootUrl}
); );
//Administration Service Client //Administration Service Client
@ -263,9 +279,9 @@ namespace EShopOnAbp.IdentityService.DbMigrations
{ {
"IdentityService" "IdentityService"
}), }),
grantTypes: new[] { "client_credentials" }, grantTypes: new[] {"client_credentials"},
secret: "1q2w3e*".Sha256(), secret: "1q2w3e*".Sha256(),
permissions: new[] { IdentityPermissions.Users.Default } permissions: new[] {IdentityPermissions.Users.Default}
); );
} }
@ -274,7 +290,7 @@ namespace EShopOnAbp.IdentityService.DbMigrations
IEnumerable<string> scopes, IEnumerable<string> scopes,
IEnumerable<string> grantTypes, IEnumerable<string> grantTypes,
string secret = null, string secret = null,
string redirectUri = null, List<string> redirectUris = null,
string postLogoutRedirectUri = null, string postLogoutRedirectUri = null,
string frontChannelLogoutUri = null, string frontChannelLogoutUri = null,
bool requireClientSecret = true, bool requireClientSecret = true,
@ -333,11 +349,17 @@ namespace EShopOnAbp.IdentityService.DbMigrations
} }
} }
if (redirectUri != null) if (redirectUris != null)
{ {
if (client.FindRedirectUri(redirectUri) == null) foreach (var redirectUri in redirectUris)
{ {
client.AddRedirectUri(redirectUri); if (redirectUri != null)
{
if (client.FindRedirectUri(redirectUri) == null)
{
client.AddRedirectUri(redirectUri);
}
}
} }
} }

26
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs

@ -8,6 +8,7 @@ using EShopOnAbp.Shared.Hosting.Gateways;
using EShopOnAbp.Shared.Hosting.Microservices; using EShopOnAbp.Shared.Hosting.Microservices;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Volo.Abp; using Volo.Abp;
@ -28,9 +29,19 @@ namespace EShopOnAbp.IdentityService
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment();
JwtBearerConfigurationHelper.Configure(context, "IdentityService"); JwtBearerConfigurationHelper.Configure(context, "IdentityService");
SwaggerWithAuthConfigurationHelper.Configure(
context: context,
authority: configuration["AuthServer:Authority"],
scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
{
{"IdentityService", "Identity Service API"}
},
apiTitle: "IdentityService Gateway API"
);
context.Services.AddCors(options => context.Services.AddCors(options =>
{ {
@ -50,16 +61,6 @@ namespace EShopOnAbp.IdentityService
.AllowCredentials(); .AllowCredentials();
}); });
}); });
SwaggerWithAuthConfigurationHelper.Configure(
context: context,
authority: configuration["AuthServer:Authority"],
scopes: new Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
{
{"IdentityService", "Identity Service API"}
},
apiTitle: "IdentityService Gateway API"
);
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override void OnApplicationInitialization(ApplicationInitializationContext context)
@ -83,7 +84,10 @@ namespace EShopOnAbp.IdentityService
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(options => app.UseSwaggerUI(options =>
{ {
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Service API"); options.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Service API");
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
}); });
app.UseAbpSerilogEnrichers(); app.UseAbpSerilogEnrichers();
app.UseAuditing(); app.UseAuditing();

2
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "IdentityService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"Logging": { "Logging": {

2
services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "OrderingService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"Logging": { "Logging": {

2
services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/appsettings.json

@ -6,7 +6,7 @@
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44330", "Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true", "RequireHttpsMetadata": "true",
"SwaggerClientId": "PaymentService_Swagger", "SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*" "SwaggerClientSecret": "1q2w3e*"
}, },
"Logging": { "Logging": {

Loading…
Cancel
Save