| | 1 | | using Despesas.Application.Authentication; |
| | 2 | | using Despesas.Business.Authentication.Abstractions; |
| | 3 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 4 | | using Microsoft.AspNetCore.Authorization; |
| | 5 | | using Microsoft.Extensions.Options; |
| | 6 | | using Microsoft.IdentityModel.Tokens; |
| | 7 | | using System.Security.Cryptography.X509Certificates; |
| | 8 | |
|
| | 9 | | namespace Despesas.Backend.CommonDependenceInject; |
| | 10 | |
|
| | 11 | | public static class AutorizationDependenceInject |
| | 12 | | { |
| | 13 | | public static void AddSigningConfigurations(this WebApplicationBuilder builder) |
| 13 | 14 | | { |
| 13 | 15 | | builder.Services.Configure<TokenOptions>(builder.Configuration.GetSection("TokenConfigurations")); |
| 13 | 16 | | var options = builder.Services.BuildServiceProvider().GetService<IOptions<TokenOptions>>(); |
| 13 | 17 | | string certificatePath = Path.Combine(AppContext.BaseDirectory, options.Value.Certificate); |
| 13 | 18 | | X509Certificate2 certificate = new X509Certificate2(certificatePath, options.Value.Password, X509KeyStorageFlags |
| 13 | 19 | | var signingConfigurations = new SigningConfigurations(certificate, options); |
| 13 | 20 | | builder.Services.AddSingleton<SigningConfigurations>(signingConfigurations); |
| | 21 | |
|
| 13 | 22 | | if (builder.Environment.IsProduction()) |
| 1 | 23 | | { |
| 1 | 24 | | builder.WebHost.ConfigureKestrel(serverOptions => |
| 0 | 25 | | { |
| 0 | 26 | | serverOptions.ConfigureHttpsDefaults(httpsOptions => |
| 0 | 27 | | { |
| 0 | 28 | | httpsOptions.ServerCertificate = certificate; |
| 0 | 29 | | }); |
| 1 | 30 | | }); |
| 1 | 31 | | } |
| 13 | 32 | | } |
| | 33 | |
|
| | 34 | | public static void AddAuthenticationConfigurations(this WebApplicationBuilder builder) |
| 1 | 35 | | { |
| 1 | 36 | | builder.Services.AddAuthentication(authOptions => |
| 1 | 37 | | { |
| 1 | 38 | | authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
| 1 | 39 | | authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |
| 1 | 40 | | }) |
| 1 | 41 | | .AddJwtBearer(bearerOptions => |
| 1 | 42 | | { |
| 1 | 43 | | var options = builder.Services.BuildServiceProvider().GetService<IOptions<TokenOptions>>(); |
| 1 | 44 | |
|
| 1 | 45 | | bearerOptions.TokenValidationParameters = new TokenValidationParameters |
| 1 | 46 | | { |
| 1 | 47 | | IssuerSigningKey = builder.Services.BuildServiceProvider().GetService<SigningConfigurations>().Key, |
| 1 | 48 | | ValidAudience = options.Value.Audience, |
| 1 | 49 | | ValidIssuer = options.Value.Issuer, |
| 1 | 50 | | ValidateIssuerSigningKey = true, |
| 1 | 51 | | ValidateLifetime = true, |
| 1 | 52 | | ClockSkew = TimeSpan.Zero |
| 1 | 53 | | }; |
| 2 | 54 | | }); |
| | 55 | |
|
| 1 | 56 | | builder.Services.AddAuthorization(auth => |
| 1 | 57 | | { |
| 1 | 58 | | auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder().AddAuthenticationSchemes(JwtBearerDefaults.Authent |
| 2 | 59 | | }); |
| 1 | 60 | | } |
| | 61 | | } |