Adds Options for JwtAuthentication (30min prod - 1day dev)

This commit is contained in:
Jonathan Bourdon
2024-07-06 22:13:56 -04:00
parent eca4f60412
commit 256fa3df91
7 changed files with 41 additions and 22 deletions

View File

@@ -1,14 +1,20 @@
using System.Security.Claims;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Infrastructure.Identity;
using Hutopy.Infrastructure.Utils;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
namespace Hutopy.Web.Controllers;
public class GoogleController(IIdentityService identityService, IHttpClientFactory httpClientFactory, IConfiguration configuration) : Controller
public class GoogleController(
IIdentityService identityService,
IHttpClientFactory httpClientFactory,
IOptionsSnapshot<JwtOptions> jwtOptions)
: Controller
{
[HttpPost("/api/google/sign-in")]
public async Task<IActionResult> SignIn([FromBody] GoogleSignInRequest request)
@@ -61,12 +67,11 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity));
var jwtSection = configuration.GetRequiredSection("Authentication:Jwt");
var token = JwtTokenHelper.GenerateJwtToken(
jwtSection["Issuer"] ?? throw new ArgumentNullException("The Jwt issuer is missing."),
jwtSection["Audience"] ?? throw new ArgumentNullException("The Jwt audience is missing."),
jwtSection["Key"] ?? throw new ArgumentNullException("The Jwt key is missing."),
jwtOptions.Value.Lifetime,
jwtOptions.Value.Issuer,
jwtOptions.Value.Audience,
jwtOptions.Value.Key,
user.Id,
user.Email,
user.FirstName,

View File

@@ -3,6 +3,7 @@ using FastEndpoints;
using Hutopy.Application;
using Hutopy.Infrastructure;
using Hutopy.Infrastructure.Data;
using Hutopy.Infrastructure.Identity;
using Hutopy.Web;
using Hutopy.Web.Contents.Data;
using Hutopy.Web.Messages.Data;
@@ -89,6 +90,8 @@ builder.Services.AddDbContext<ContentDbContext>((_, options) =>
options.UseSqlServer(builder.Configuration.GetConnectionString("ContentStore"));
});
builder.Services.Configure<JwtOptions>(builder.Configuration.GetRequiredSection(JwtOptions.SectionName));
var app = builder.Build();
app.UseForwardedHeaders(

View File

@@ -14,6 +14,7 @@
},
"Authentication": {
"Jwt": {
"Lifetime": "1.00:00:00",
"Audience": "hutopy",
"Issuer": "https://auth.hutopy.com",
"Key": "b2df428b9929d3ace7c598bbf4e496b2f0b71ab3cd4f94540356cfc35b000000"

View File

@@ -6,5 +6,10 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Authentication": {
"Jwt": {
"Lifetime": "00:30:00"
}
}
}