From 040d276d5bdac502c0740654a552f3fb35fcebc4 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Sat, 15 Jun 2024 00:01:39 +0400 Subject: [PATCH] fix config jwt provider --- BusinessLogic/Tools/JwtOptions.cs | 14 ++++++++++++++ BusinessLogic/Tools/JwtProvider.cs | 11 ++++++++--- RestAPI/Program.cs | 19 +++++++++++++++++++ RestAPI/appsettings.json | 6 +++++- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 BusinessLogic/Tools/JwtOptions.cs diff --git a/BusinessLogic/Tools/JwtOptions.cs b/BusinessLogic/Tools/JwtOptions.cs new file mode 100644 index 0000000..7327dd0 --- /dev/null +++ b/BusinessLogic/Tools/JwtOptions.cs @@ -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; } + } +} \ No newline at end of file diff --git a/BusinessLogic/Tools/JwtProvider.cs b/BusinessLogic/Tools/JwtProvider.cs index e2a8758..525d74d 100644 --- a/BusinessLogic/Tools/JwtProvider.cs +++ b/BusinessLogic/Tools/JwtProvider.cs @@ -14,10 +14,9 @@ namespace BusinessLogic.Tools { public class JwtProvider { - // TODO: Переместить ключ и время в надежное место - private const string _key = "secretkey_secretkey_secretkey_secretkey"; + private static string _key; - private const int _expiresHours = 24; + private static int _expiresHours; public static string Generate(UserBindingModel user) { @@ -37,5 +36,11 @@ namespace BusinessLogic.Tools var stringToken = new JwtSecurityTokenHandler().WriteToken(token); return stringToken; } + + public void SetupJwtOptions(JwtOptions options) + { + _key = options.SecretKey; + _expiresHours = options.ExpiresHours; + } } } \ No newline at end of file diff --git a/RestAPI/Program.cs b/RestAPI/Program.cs index 6fbbd7c..208766e 100644 --- a/RestAPI/Program.cs +++ b/RestAPI/Program.cs @@ -1,9 +1,12 @@ using BusinessLogic.BusinessLogic; +using BusinessLogic.Tools; +using BusinessLogic.Tools.Mail; using Contracts.BusinessLogicContracts; using Contracts.StorageContracts; using DatabaseImplement.Implements; using Microsoft.OpenApi.Models; using System; +using System.Net.Mail; const string VERSION = "v1"; const string TITLE = "21GunsRestAPI"; @@ -21,6 +24,9 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + #endregion DI builder.Services.AddControllers(); @@ -32,6 +38,19 @@ builder.Services.AddSwaggerGen(c => }); var app = builder.Build(); +var jwtProvider = app.Services.GetService(); +var mailSender = app.Services.GetService(); + +#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. if (app.Environment.IsDevelopment()) diff --git a/RestAPI/appsettings.json b/RestAPI/appsettings.json index 10f68b8..995bec1 100644 --- a/RestAPI/appsettings.json +++ b/RestAPI/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, + "JwtOptions": { + "SecretKey": "secretkey_secretkey_secretkey_secretkey", + "ExpiresHours": 24 + }, "AllowedHosts": "*" -} +} \ No newline at end of file