fix config jwt provider

This commit is contained in:
mfnefd 2024-06-15 00:01:39 +04:00
parent d9f3faf92d
commit 040d276d5b
4 changed files with 46 additions and 4 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools
{
public class JwtOptions
{
public string SecretKey { get; set; } = null!;
public short ExpiresHours { get; set; }
}
}

View File

@ -14,10 +14,9 @@ namespace BusinessLogic.Tools
{ {
public class JwtProvider public class JwtProvider
{ {
// TODO: Переместить ключ и время в надежное место private static string _key;
private const string _key = "secretkey_secretkey_secretkey_secretkey";
private const int _expiresHours = 24; private static int _expiresHours;
public static string Generate(UserBindingModel user) public static string Generate(UserBindingModel user)
{ {
@ -37,5 +36,11 @@ namespace BusinessLogic.Tools
var stringToken = new JwtSecurityTokenHandler().WriteToken(token); var stringToken = new JwtSecurityTokenHandler().WriteToken(token);
return stringToken; return stringToken;
} }
public void SetupJwtOptions(JwtOptions options)
{
_key = options.SecretKey;
_expiresHours = options.ExpiresHours;
}
} }
} }

View File

@ -1,9 +1,12 @@
using BusinessLogic.BusinessLogic; using BusinessLogic.BusinessLogic;
using BusinessLogic.Tools;
using BusinessLogic.Tools.Mail;
using Contracts.BusinessLogicContracts; using Contracts.BusinessLogicContracts;
using Contracts.StorageContracts; using Contracts.StorageContracts;
using DatabaseImplement.Implements; using DatabaseImplement.Implements;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using System; using System;
using System.Net.Mail;
const string VERSION = "v1"; const string VERSION = "v1";
const string TITLE = "21GunsRestAPI"; const string TITLE = "21GunsRestAPI";
@ -21,6 +24,9 @@ builder.Services.AddTransient<IUserLogic, UserLogic>();
builder.Services.AddTransient<IRoleStorage, RoleStorage>(); builder.Services.AddTransient<IRoleStorage, RoleStorage>();
builder.Services.AddTransient<IUserStorage, UserStorage>(); builder.Services.AddTransient<IUserStorage, UserStorage>();
builder.Services.AddSingleton<JwtProvider>();
builder.Services.AddSingleton<MailSender>();
#endregion DI #endregion DI
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -32,6 +38,19 @@ builder.Services.AddSwaggerGen(c =>
}); });
var app = builder.Build(); var app = builder.Build();
var jwtProvider = app.Services.GetService<JwtProvider>();
var mailSender = app.Services.GetService<MailSender>();
#region Setup config
string? getSection(string section) => builder.Configuration?.GetSection(section)?.Value?.ToString();
jwtProvider?.SetupJwtOptions(new()
{
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
});
#endregion Setup config
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())

View File

@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"JwtOptions": {
"SecretKey": "secretkey_secretkey_secretkey_secretkey",
"ExpiresHours": 24
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }