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": {
"Authority": "https://localhost:44330",
"RequireHttpsMetadata": "true",
"SwaggerClientId": "AccountService_Swagger",
"SwaggerClientId": "WebGateway_Swagger",
"SwaggerClientSecret": "1q2w3e*"
},
"AllowedHosts": "*",

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

@ -24,7 +24,21 @@ namespace EShopOnAbp.WebPublicGateway
var configuration = context.Services.GetConfiguration();
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)
@ -59,12 +73,8 @@ namespace EShopOnAbp.WebPublicGateway
}
options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API");
// options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
// 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*");
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
}
});
app.MapWhen(

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

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

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

@ -24,7 +24,22 @@ namespace EShopOnAbp.WebGateway
{
var configuration = context.Services.GetConfiguration();
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 =>
{

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 Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
@ -40,7 +41,7 @@ namespace EShopOnAbp.AdministrationService
authority: configuration["AuthServer:Authority"],
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"
);
@ -85,7 +86,10 @@ namespace EShopOnAbp.AdministrationService
app.UseSwagger();
app.UseSwaggerUI(options =>
{
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Administration Service API");
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
});
app.UseAbpSerilogEnrichers();
app.UseAuditing();

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

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

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

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

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

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

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

@ -105,29 +105,16 @@ namespace EShopOnAbp.IdentityService.DbMigrations
private async Task CreateSwaggerClientsAsync()
{
await CreateSwaggerClientAsync("AccountService",
new[] { "AccountService"});
await CreateSwaggerClientAsync("IdentityService",
new[] { "IdentityService"});
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" });
await CreateWebGatewaySwaggerClientAsync("WebGateway",
new[]
{
"AccountService", "IdentityService", "AdministrationService",
"CatalogService", "BasketService",
"PaymentService", "OrderingService"
});
}
private async Task CreateSwaggerClientAsync(string name, string[] scopes = null)
private async Task CreateWebGatewaySwaggerClientAsync(string name, string[] scopes = null)
{
var commonScopes = new[]
{
@ -138,22 +125,51 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"phone",
"address"
};
scopes ??= new[] { name };
scopes ??= new[] {name};
// Swagger Client
var swaggerClientId = $"{name}_Swagger";
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(
name: swaggerClientId,
scopes: commonScopes.Union(scopes),
grantTypes: new[] { "authorization_code" },
grantTypes: new[] {"authorization_code"},
secret: "1q2w3e*".Sha256(),
requireClientSecret: false,
redirectUri: $"{swaggerRootUrl}/swagger/oauth2-redirect.html",
corsOrigins: new[] { swaggerRootUrl.RemovePostFix("/") }
redirectUris: new List<string>
{
$"{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)
{
var apiScope = await _apiScopeRepository.FindByNameAsync(name );
var apiScope = await _apiScopeRepository.FindByNameAsync(name);
if (apiScope == null)
{
apiScope = await _apiScopeRepository.InsertAsync(
@ -228,12 +244,12 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"PaymentService",
"OrderingService"
}),
grantTypes: new[] { "hybrid" },
grantTypes: new[] {"hybrid"},
secret: "1q2w3e*".Sha256(),
redirectUri: $"{publicWebClientRootUrl}signin-oidc",
redirectUris: new List<string>{ $"{publicWebClientRootUrl}signin-oidc" },
postLogoutRedirectUri: $"{publicWebClientRootUrl}signout-callback-oidc",
frontChannelLogoutUri: $"{publicWebClientRootUrl}Account/FrontChannelLogout",
corsOrigins: new[] { publicWebClientRootUrl.RemovePostFix("/") }
corsOrigins: new[] {publicWebClientRootUrl.RemovePostFix("/")}
);
//Angular Client
@ -247,13 +263,13 @@ namespace EShopOnAbp.IdentityService.DbMigrations
"IdentityService",
"AdministrationService"
}),
grantTypes: new[] { "authorization_code", "LinkLogin", "password" },
grantTypes: new[] {"authorization_code", "LinkLogin", "password"},
secret: "1q2w3e*".Sha256(),
requirePkce: true,
requireClientSecret: false,
redirectUri: $"{angularClientRootUrl}",
redirectUris: new List<string>{ $"{angularClientRootUrl}" },
postLogoutRedirectUri: $"{angularClientRootUrl}",
corsOrigins: new[] { angularClientRootUrl }
corsOrigins: new[] {angularClientRootUrl}
);
//Administration Service Client
@ -263,9 +279,9 @@ namespace EShopOnAbp.IdentityService.DbMigrations
{
"IdentityService"
}),
grantTypes: new[] { "client_credentials" },
grantTypes: new[] {"client_credentials"},
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> grantTypes,
string secret = null,
string redirectUri = null,
List<string> redirectUris = null,
string postLogoutRedirectUri = null,
string frontChannelLogoutUri = null,
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 Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
@ -28,9 +29,19 @@ namespace EShopOnAbp.IdentityService
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment();
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 =>
{
@ -50,16 +61,6 @@ namespace EShopOnAbp.IdentityService
.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)
@ -83,7 +84,10 @@ namespace EShopOnAbp.IdentityService
app.UseSwagger();
app.UseSwaggerUI(options =>
{
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Service API");
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
});
app.UseAbpSerilogEnrichers();
app.UseAuditing();

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

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

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

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

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

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

Loading…
Cancel
Save