Skip to content

Commit

Permalink
feat: Configura a autenticação usando JWT Bearer
Browse files Browse the repository at this point in the history
  • Loading branch information
renebentes committed Jan 26, 2024
1 parent ee6d23a commit d95b82d
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 34 deletions.
13 changes: 0 additions & 13 deletions JwtStore.Api/Extensions/ServiceCollectionExtensions.cs

This file was deleted.

17 changes: 0 additions & 17 deletions JwtStore.Api/Options/SecretsOptionsSetup.cs

This file was deleted.

1 change: 0 additions & 1 deletion JwtStore.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JwtStore.Api.Extensions;
using JwtStore.Infrastructure.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
Expand Down
7 changes: 6 additions & 1 deletion JwtStore.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=jwt-store;User ID=sa;Password=1q2w3e4r@#$;Trusted_Connection=False; TrustServerCertificate=True;"
},
"Jwt": {
"PrivateKey": "",
"Audience": "",
"Issuer": "",
"ExpirationInMinutes": 0
},
"Secrets": {
"ApiKey": "",
"JwtPrivateKey": "",
"PasswordSaltKey": ""
},
"Logging": {
Expand Down
14 changes: 14 additions & 0 deletions JwtStore.Infrastructure/Authentication/JwtOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace JwtStore.Infrastructure.Authentication;

public sealed class JwtOptions
{
public const string SectionName = "Jwt";

public string Audience { get; init; } = string.Empty;

public string Issuer { get; init; } = string.Empty;

public string PrivateKey { get; init; } = string.Empty;

public int TokenExpirationInMinutes { get; init; }
}
14 changes: 14 additions & 0 deletions JwtStore.Infrastructure/Authentication/JwtOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace JwtStore.Infrastructure.Authentication;

public sealed class JwtOptionsSetup(IConfiguration configuration)
: IConfigureOptions<JwtOptions>
{
private readonly IConfiguration _configuration = configuration;

public void Configure(JwtOptions options)
=> _configuration.GetSection(JwtOptions.SectionName)
.Bind(options);
}
38 changes: 38 additions & 0 deletions JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
using JwtStore.Infrastructure.Authentication;
using JwtStore.Infrastructure.Data;
using JwtStore.Infrastructure.Options;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace JwtStore.Infrastructure.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
private const string DefaultConnection = nameof(DefaultConnection);

public static IServiceCollection AddConfigurations(this IServiceCollection services)
{
services.ConfigureOptions<SecretsOptionsSetup>();
services.ConfigureOptions<JwtOptionsSetup>();

return services;
}

public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IOptions<JwtOptions> options)
{
var jwtOptions = options.Value;

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtOptions.Issuer,
ValidAudience = jwtOptions.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.PrivateKey))
};
});

services.AddAuthorization();

return services;
}

public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<AppDbContext>(options
Expand Down
1 change: 1 addition & 0 deletions JwtStore.Infrastructure/JwtStore.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions JwtStore.Infrastructure/Options/SecretsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace JwtStore.Infrastructure.Options;

public sealed class SecretsOptions
{
public string ApiKey { get; init; } = string.Empty;
public const string SectionName = "Secrets";

public string JwtPrivateKey { get; init; } = string.Empty;
public string ApiKey { get; init; } = string.Empty;

public string PasswordSaltKey { get; init; } = string.Empty;
}
14 changes: 14 additions & 0 deletions JwtStore.Infrastructure/Options/SecretsOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace JwtStore.Infrastructure.Options;

public sealed class SecretsOptionsSetup(IConfiguration configuration)
: IConfigureOptions<SecretsOptions>
{
private readonly IConfiguration _configuration = configuration;

public void Configure(SecretsOptions options)
=> _configuration.GetSection(SecretsOptions.SectionName)
.Bind(options);
}

0 comments on commit d95b82d

Please sign in to comment.