Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 32ae1e1

Browse files
committed
Api -> Net core 3 - Checkpoint
1 parent f7fdb80 commit 32ae1e1

8 files changed

Lines changed: 76 additions & 77 deletions

File tree

src/Backend/Jp.Application/AutoMapper/AutoMapperConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Linq;
2-
using AutoMapper;
1+
using AutoMapper;
2+
using System.Linq;
33

44
namespace Jp.Application.AutoMapper
55
{

src/Backend/Jp.Application/Interfaces/IIdentityResourceAppService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
using IdentityServer4.Models;
2+
using Jp.Application.ViewModels.IdentityResourceViewModels;
13
using System;
24
using System.Collections.Generic;
3-
using System.Text;
45
using System.Threading.Tasks;
5-
using IdentityServer4.Models;
6-
using Jp.Application.ViewModels;
7-
using Jp.Application.ViewModels.IdentityResourceViewModels;
86

97
namespace Jp.Application.Interfaces
108
{
11-
public interface IIdentityResourceAppService: IDisposable
9+
public interface IIdentityResourceAppService : IDisposable
1210
{
1311
Task<IEnumerable<IdentityResourceListView>> GetIdentityResources();
1412
Task<IdentityResource> GetDetails(string name);

src/Backend/Jp.UserManagement/Configuration/AuthorizeCheckOperationFilter.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using Microsoft.AspNetCore.Authorization;
4-
using Swashbuckle.AspNetCore.Swagger;
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.OpenApi.Models;
53
using Swashbuckle.AspNetCore.SwaggerGen;
4+
using System.Collections.Generic;
5+
using System.Linq;
66

77
namespace Jp.Management.Configuration
88
{
99
public class AuthorizeCheckOperationFilter : IOperationFilter
1010
{
11-
public void Apply(Operation operation, OperationFilterContext context)
11+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
1212
{
1313
// Check for authorize attribute
14-
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
15-
.Union(context.MethodInfo.GetCustomAttributes(true))
16-
.OfType<AuthorizeAttribute>().Any();
17-
14+
var requiredScopes = context.MethodInfo
15+
.GetCustomAttributes(true)
16+
.OfType<AuthorizeAttribute>()
17+
.Select(attr => attr.Policy)
18+
.Distinct()
19+
.ToList();
1820

19-
20-
if (hasAuthorize)
21+
if (requiredScopes.Any())
2122
{
22-
operation.Responses.Add("401", new Response { Description = "Unauthorized" });
23-
operation.Responses.Add("403", new Response { Description = "Forbidden" });
23+
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
24+
operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });
25+
26+
var oAuthScheme = new OpenApiSecurityScheme
27+
{
28+
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
29+
};
2430

25-
operation.Security = new List<IDictionary<string, IEnumerable<string>>> {
26-
new Dictionary<string, IEnumerable<string>> {{"oauth2", new[] { "UserManagementApi.owner-content" } }}
31+
operation.Security = new List<OpenApiSecurityRequirement>
32+
{
33+
new OpenApiSecurityRequirement
34+
{
35+
[ oAuthScheme ] = requiredScopes
36+
}
2737
};
2838
}
2939
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
2-
using AutoMapper;
3-
using Jp.Application.AutoMapper;
1+
using Jp.Application.AutoMapper;
42
using Microsoft.Extensions.DependencyInjection;
3+
using System;
54

65
namespace Jp.Management.Configuration
76
{
@@ -11,14 +10,7 @@ public static void AddAutoMapperSetup(this IServiceCollection services)
1110
{
1211
if (services == null) throw new ArgumentNullException(nameof(services));
1312

14-
services.AddAutoMapper();
15-
16-
// Registering Mappings automatically only works if the
17-
// Automapper Profile classes are in ASP.NET project
18-
AutoMapperConfig.RegisterMappings(new CustomMappingProfile());
19-
13+
services.AddSingleton(AutoMapperConfig.RegisterMappings(new CustomMappingProfile()).CreateMapper());
2014
}
21-
22-
2315
}
2416
}

src/Backend/Jp.UserManagement/Configuration/SwaggerConfig.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.DependencyInjection;
3-
using Swashbuckle.AspNetCore.Swagger;
3+
using Microsoft.OpenApi.Models;
4+
using System;
45
using System.Collections.Generic;
56

67
namespace Jp.Management.Configuration
@@ -11,23 +12,38 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IC
1112
{
1213
services.AddSwaggerGen(options =>
1314
{
14-
options.SwaggerDoc("v1", new Info
15+
options.SwaggerDoc("v1", new OpenApiInfo()
1516
{
1617
Version = "v1",
1718
Title = "Identity Server 4 User Management API ",
1819
Description = "Swagger surface",
19-
Contact = new Contact { Name = "Bruno Brito", Email = "bhdebrito@gmail.com", Url = "http://www.brunobrito.net.br" },
20-
License = new License { Name = "MIT", Url = "https://github.com/brunohbrito/JP-Project/blob/master/LICENSE" },
20+
Contact = new OpenApiContact()
21+
{
22+
Name = "Bruno Brito",
23+
Email = "bhdebrito@gmail.com",
24+
Url = new Uri("http://www.brunobrito.net.br")
25+
},
26+
License = new OpenApiLicense()
27+
{
28+
Name = "MIT",
29+
Url = new Uri("https://github.com/brunohbrito/JP-Project/blob/master/LICENSE")
30+
},
2131

2232
});
2333

24-
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
34+
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
2535
{
26-
Flow = "implicit",
27-
AuthorizationUrl = $"{configuration.GetValue<string>("ApplicationSettings:Authority")}/connect/authorize",
28-
Scopes = new Dictionary<string, string> {
29-
{ "jp_api.user", "User Management API - full access" },
30-
{ "jp_api.is4", "IS4 Management API - full access" },
36+
Flows = new OpenApiOAuthFlows()
37+
{
38+
Implicit = new OpenApiOAuthFlow()
39+
{
40+
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("ApplicationSettings:Authority")}/connect/authorize"),
41+
Scopes = new Dictionary<string, string>
42+
{
43+
{"jp_api.user", "User Management API - full access"},
44+
{"jp_api.is4", "IS4 Management API - full access"},
45+
}
46+
}
3147
}
3248
});
3349
options.OperationFilter<AuthorizeCheckOperationFilter>();

src/Backend/Jp.UserManagement/Controllers/IdentityResourceController.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using IdentityServer4.Models;
1+
using IdentityServer4.Models;
62
using Jp.Application.Interfaces;
7-
using Jp.Application.ViewModels.ClientsViewModels;
83
using Jp.Application.ViewModels.IdentityResourceViewModels;
94
using Jp.Domain.Core.Bus;
105
using Jp.Domain.Core.Notifications;
116
using Jp.Infra.CrossCutting.Tools.Model;
127
using MediatR;
138
using Microsoft.AspNetCore.Authorization;
149
using Microsoft.AspNetCore.Mvc;
10+
using System.Collections.Generic;
11+
using System.Threading.Tasks;
1512

1613
namespace Jp.Management.Controllers
1714
{

src/Backend/Jp.UserManagement/Jp.Management.csproj

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<UserSecretsId>9c91d295-54c5-4d09-9bd6-fa56fb74011b</UserSecretsId>
66
<DockerTargetOS>Linux</DockerTargetOS>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.1.1" />
11-
<PackageReference Include="IdentityServer4" Version="2.5.0" />
12-
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.7.0" />
10+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
11+
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
1312
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
14-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.7.1" />
15-
<PackageReference Include="Microsoft.AspNetCore.App" />
16-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.10" />
17-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
18-
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
19-
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.0.3" />
13+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0" />
14+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" />
15+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
16+
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
17+
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.0.4" />
2018
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
2119
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
22-
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
20+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
2321
</ItemGroup>
2422

2523
<ItemGroup>

src/Backend/Jp.UserManagement/Startup.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
910
using Microsoft.Extensions.Logging;
1011

1112
namespace Jp.Management
@@ -14,30 +15,17 @@ public class Startup
1415
{
1516
private readonly ILogger<Startup> _logger;
1617
public IConfiguration Configuration { get; }
17-
public IHostingEnvironment HostEnvironment { get; }
1818

19-
public Startup(IHostingEnvironment hostEnvironment, ILogger<Startup> logger)
19+
public Startup(ILogger<Startup> logger, IConfiguration configuration)
2020
{
21+
Configuration = configuration;
2122
_logger = logger;
22-
var builder = new ConfigurationBuilder()
23-
.SetBasePath(hostEnvironment.ContentRootPath)
24-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
25-
.AddJsonFile($"appsettings.{hostEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
26-
.AddEnvironmentVariables();
27-
28-
if (hostEnvironment.IsDevelopment())
29-
{
30-
builder.AddUserSecrets<Startup>();
31-
}
32-
33-
Configuration = builder.Build();
34-
HostEnvironment = hostEnvironment;
3523
}
3624
// This method gets called by the runtime. Use this method to add services to the container.
3725
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
3826
public void ConfigureServices(IServiceCollection services)
3927
{
40-
services.AddMvc();
28+
services.AddMvcCore().AddApiExplorer();
4129

4230
// Identity Database
4331
services.ConfigureDatabase(Configuration);
@@ -62,7 +50,7 @@ public void ConfigureServices(IServiceCollection services)
6250
}
6351

6452
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
65-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
53+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6654
{
6755
if (env.IsDevelopment())
6856
{

0 commit comments

Comments
 (0)