fix basket api

This commit is contained in:
ericuss 2019-07-31 09:12:46 +02:00
parent 17dd9cebb1
commit 445c43221b
2 changed files with 79 additions and 38 deletions

View File

@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
namespace Basket.API.Infrastructure.Filters
{
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// Check for authorize attribute
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any();
if (!hasAuthorize) return;
operation.Responses.TryAdd("401", new OpenApiResponse { Description = "Unauthorized" });
operation.Responses.TryAdd("403", new OpenApiResponse { Description = "Forbidden" });
var oAuthScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
};
operation.Security = new List<OpenApiSecurityRequirement>
{
new OpenApiSecurityRequirement
{
[ oAuthScheme ] = new [] { "basketapi" }
}
};
}
}
}

View File

@ -55,6 +55,37 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
}).AddNewtonsoftJson(); }).AddNewtonsoftJson();
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Basket HTTP API",
Version = "v1",
Description = "The Basket Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "basket", "Basket API" }
}
}
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
ConfigureAuthService(services); ConfigureAuthService(services);
services.AddCustomHealthCheck(Configuration); services.AddCustomHealthCheck(Configuration);
@ -124,33 +155,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
RegisterEventBus(services); RegisterEventBus(services);
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Basket HTTP API",
Version = "v1",
Description = "The Basket Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "basket", "Basket API" }
}
}
}
});
});
services.AddCors(options => services.AddCors(options =>
{ {
@ -185,20 +189,12 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
app.UseRouting();
ConfigureAuth(app); ConfigureAuth(app);
app.UseStaticFiles(); app.UseStaticFiles();
app.UseSwagger()
.UseSwaggerUI(setup =>
{
setup.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1");
setup.OAuthClientId("basketswaggerui");
setup.OAuthAppName("Basket Swagger UI");
});
app.UseCors("CorsPolicy"); app.UseCors("CorsPolicy");
app.UseRouting();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapDefaultControllerRoute(); endpoints.MapDefaultControllerRoute();
@ -213,6 +209,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
Predicate = r => r.Name.Contains("self") Predicate = r => r.Name.Contains("self")
}); });
}); });
app.UseSwagger()
.UseSwaggerUI(setup =>
{
setup.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1");
setup.OAuthClientId("basketswaggerui");
setup.OAuthAppName("Basket Swagger UI");
});
ConfigureEventBus(app); ConfigureEventBus(app);
} }
@ -226,7 +231,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
private void ConfigureAuthService(IServiceCollection services) private void ConfigureAuthService(IServiceCollection services)
{ {
// prevent from mapping "sub" claim to nameidentifier. // prevent from mapping "sub" claim to nameidentifier.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
var identityUrl = Configuration.GetValue<string>("IdentityUrl"); var identityUrl = Configuration.GetValue<string>("IdentityUrl");
@ -250,8 +255,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
app.UseMiddleware<ByPassAuthMiddleware>(); app.UseMiddleware<ByPassAuthMiddleware>();
} }
app.UseAuthorization();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization();
} }
private void RegisterEventBus(IServiceCollection services) private void RegisterEventBus(IServiceCollection services)