2024-05-28 07:19:40 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-05-29 03:35:52 +03:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2024-05-28 07:19:40 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Common.Attributes;
|
|
|
|
|
using Mirea.Api.Endpoint.Common.Exceptions;
|
|
|
|
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
2024-05-29 03:35:52 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Common.Services;
|
2024-05-28 07:19:40 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Configuration.General;
|
2024-05-29 03:35:52 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Configuration.General.Settings;
|
2024-05-29 03:37:04 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
2024-05-28 07:19:40 +03:00
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.Endpoint.Controllers.Configuration;
|
|
|
|
|
|
|
|
|
|
[ApiVersion("1.0")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[MaintenanceModeIgnore]
|
2024-05-29 03:35:52 +03:00
|
|
|
|
public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigureService notConfigureService, IMemoryCache cache) : BaseController
|
2024-05-28 07:19:40 +03:00
|
|
|
|
{
|
2024-05-29 03:35:52 +03:00
|
|
|
|
private const string CacheGeneralKey = "config_part";
|
|
|
|
|
private GeneralConfig GeneralConfig
|
|
|
|
|
{
|
|
|
|
|
get => cache.Get<GeneralConfig>(CacheGeneralKey) ?? new GeneralConfig();
|
|
|
|
|
set => cache.Set(CacheGeneralKey, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 07:19:40 +03:00
|
|
|
|
[HttpGet("GenerateToken")]
|
2024-05-29 03:30:26 +03:00
|
|
|
|
[Localhost]
|
2024-05-28 07:19:40 +03:00
|
|
|
|
public ActionResult<string> GenerateToken()
|
|
|
|
|
{
|
|
|
|
|
if (!notConfigureService.IsMaintenanceMode)
|
|
|
|
|
throw new ControllerArgumentException(
|
|
|
|
|
"The token cannot be generated because the server has been configured. " +
|
2024-05-28 07:20:21 +03:00
|
|
|
|
$"If you need to restart the configuration, then delete the \"{PathBuilder.Combine(GeneralConfig.FilePath)}\" file and restart the application.");
|
2024-05-28 07:19:40 +03:00
|
|
|
|
|
|
|
|
|
var token = new byte[32];
|
|
|
|
|
RandomNumberGenerator.Create().GetBytes(token);
|
|
|
|
|
setupToken.SetToken(token);
|
|
|
|
|
|
|
|
|
|
return Ok(Convert.ToBase64String(token));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("CheckToken")]
|
|
|
|
|
public ActionResult<bool> CheckToken([FromQuery] string token)
|
|
|
|
|
{
|
|
|
|
|
if (!setupToken.MatchToken(Convert.FromBase64String(token))) return Unauthorized("The token is not valid");
|
|
|
|
|
|
|
|
|
|
Response.Cookies.Append("AuthToken", token, new CookieOptions
|
|
|
|
|
{
|
|
|
|
|
HttpOnly = false,
|
|
|
|
|
Secure = false,
|
|
|
|
|
Path = "/"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 03:37:04 +03:00
|
|
|
|
private ActionResult<bool> SetDatabase<TConnection, TException>(string connectionString, DbSettings.DatabaseEnum databaseType)
|
|
|
|
|
where TConnection : class, IDbConnection, new()
|
|
|
|
|
where TException : Exception
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new TConnection();
|
|
|
|
|
connection.ConnectionString = connectionString;
|
|
|
|
|
connection.Open();
|
|
|
|
|
connection.Close();
|
|
|
|
|
|
|
|
|
|
var general = GeneralConfig;
|
|
|
|
|
general.DbSettings = new DbSettings
|
|
|
|
|
{
|
|
|
|
|
ConnectionStringSql = connectionString,
|
|
|
|
|
TypeDatabase = databaseType
|
|
|
|
|
};
|
|
|
|
|
GeneralConfig = general;
|
|
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
catch (TException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ControllerArgumentException($"Error when connecting: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 07:19:40 +03:00
|
|
|
|
|
|
|
|
|
}
|