Pull JWTm connection string, and hosting port in settings

dashboard
Pat Hartl 2023-01-26 00:27:52 -06:00
parent db9c569ebc
commit b7df636fc7
6 changed files with 41 additions and 31 deletions

View File

@ -1,4 +1,6 @@
using LANCommander.Data.Models;
using LANCommander.Models;
using LANCommander.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
@ -28,13 +30,13 @@ namespace LANCommander.Controllers.Api
{
private readonly UserManager<User> UserManager;
private readonly RoleManager<Role> RoleManager;
private readonly IConfiguration Configuration;
private readonly LANCommanderSettings Settings;
public AuthController(UserManager<User> userManager, RoleManager<Role> roleManager, IConfiguration configuration)
public AuthController(UserManager<User> userManager, RoleManager<Role> roleManager)
{
UserManager = userManager;
RoleManager = roleManager;
Configuration = configuration;
Settings = SettingService.GetSettings();
}
[HttpPost]
@ -60,10 +62,8 @@ namespace LANCommander.Controllers.Api
var token = GetToken(authClaims);
var refreshToken = GenerateRefreshToken();
_ = int.TryParse(Configuration["JWT:RefreshTokenValidityInDays"], out int refreshTokenValidityInDays);
user.RefreshToken = refreshToken;
user.RefreshTokenExpiration = DateTime.Now.AddDays(refreshTokenValidityInDays);
user.RefreshTokenExpiration = DateTime.Now.AddDays(Settings.TokenLifetime);
await UserManager.UpdateAsync(user);
@ -127,10 +127,10 @@ namespace LANCommander.Controllers.Api
private JwtSecurityToken GetToken(List<Claim> authClaims)
{
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]));
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.TokenSecret));
var token = new JwtSecurityToken(
expires: DateTime.Now.AddDays(30),
expires: DateTime.Now.AddDays(Settings.TokenLifetime),
claims: authClaims,
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
);
@ -157,7 +157,7 @@ namespace LANCommander.Controllers.Api
ValidateAudience = false,
ValidateIssuer = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"])),
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.TokenSecret)),
ValidateLifetime = false
};

View File

@ -2,6 +2,10 @@
{
public class LANCommanderSettings
{
public int Port { get; set; }
public string TokenSecret { get; set; }
public int TokenLifetime { get; set; }
public string DatabaseConnectionString { get; set; }
public string IGDBClientId { get; set; }
public string IGDBClientSecret { get; set; }
}

View File

@ -14,6 +14,13 @@ ConfigurationManager configuration = builder.Configuration;
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
var settings = SettingService.GetSettings();
builder.WebHost.ConfigureKestrel(options =>
{
// Configure as HTTP only
options.ListenAnyIP(settings.Port);
});
builder.Services.AddDbContext<LANCommander.Data.DatabaseContext>(b =>
{

View File

@ -8,7 +8,7 @@ namespace LANCommander.Services
{
private const string SettingsFilename = "Settings.yml";
public LANCommanderSettings GetSettings()
public static LANCommanderSettings GetSettings()
{
if (File.Exists(SettingsFilename))
{
@ -25,7 +25,7 @@ namespace LANCommander.Services
return new LANCommanderSettings();
}
public void SaveSettings(LANCommanderSettings settings)
public static void SaveSettings(LANCommanderSettings settings)
{
var serializer = new SerializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)

View File

@ -0,0 +1,18 @@
# The port LANCommander should listen on (HTTP)
Port: 1337
# Broadcast the server's address on the local network to be picked up by clients
Beacon: True
# Change this!
TokenSecret: abcdefghijklmnopqrstuvwxyz123456790
# How long the token is valid for (in days)
TokenLifetime: 30
# Probably want to leave this alone
DatabaseConnectionString: Data Source=LANCommander.db;Cache=Shared
# IGDB Credentials: https://api-docs.igdb.com/#getting-started
IGDBClientId: abcdefghijklmnopqrstuvwxyz123456790
IGDBClientSecret: abcdefghijklmnopqrstuvwxyz123456790

View File

@ -1,28 +1,9 @@
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=LANCommander.db;Cache=Shared"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://0.0.0.0:5000"
},
"Https": {
"Url": "https://0.0.0.0:5001"
}
}
},
"AllowedHosts": "*",
"JWT": {
"ValidAudience": "",
"ValidIssuer": "",
"Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr",
"RefreshTokenValidityInDays": 30
}
"AllowedHosts": "*"
}