This commit is contained in:
2020-06-13 18:12:13 +00:00
parent 0f1a529ec4
commit dd4a4cb68c
2 changed files with 26 additions and 21 deletions

View File

@@ -98,7 +98,7 @@ namespace AyaNova.Api.Controllers
[HttpPost]
public async Task<IActionResult> FetchLicense()
{
if (serverState.IsClosed)
if (serverState.IsClosed)
{
//Exception for SuperUser account to handle licensing issues
if (UserIdFromContext.Id(HttpContext.Items) != 1)
@@ -116,7 +116,11 @@ namespace AyaNova.Api.Controllers
try
{
await AyaNova.Core.License.FetchKeyAsync(serverState, ct, log);
var ret = await AyaNova.Core.License.FetchKeyAsync(serverState, ct, log, false);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.License, AyaEvent.LicenseFetch), ct);
return Ok(ret);
}
catch (Exception ex)
{
@@ -126,7 +130,6 @@ namespace AyaNova.Api.Controllers
rootex = rootex.InnerException;
}
if (rootex.Message.Contains("E1020"))
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "LICENSE_KEY", rootex.Message));
@@ -137,11 +140,6 @@ namespace AyaNova.Api.Controllers
}
}
var ret = AyaNova.Core.License.LicenseInfoAsJson;
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.License, AyaEvent.LicenseFetch), ct);
return Ok(ApiOkResponse.Response(ret));
}
@@ -210,7 +208,7 @@ namespace AyaNova.Api.Controllers
[HttpPost("permanently-erase-all-data")]
public async Task<IActionResult> RemoveAllData([FromBody] string acceptCode)
{
if (serverState.IsClosed)
if (serverState.IsClosed)
{
//Exception for SuperUser account to handle licensing issues
if (UserIdFromContext.Id(HttpContext.Items) != 1)
@@ -243,7 +241,7 @@ namespace AyaNova.Api.Controllers
//------------------------------------------------------

View File

@@ -479,29 +479,36 @@ namespace AyaNova.Core
/// Fetch a key, validate it and install it in the db then initialize with it
/// </summary>
/// <returns>Result string</returns>
internal static async Task FetchKeyAsync(AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ct, ILogger log)
internal static async Task<string> FetchKeyAsync(AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ct, ILogger log, bool calledFromInternalJob = true)
{
log.LogDebug($"Fetching license for DBID {LicenseDbId.ToString()}");
string sUrl = $"{LICENSE_SERVER_URL}rvf/{LicenseDbId.ToString()}";
try
{
string RawTextKeyFromRockfish = await ServiceProviderProvider.HttpClientFactory.CreateClient().GetStringAsync(sUrl);
//FUTURE: if there is any kind of error response or REASON or LicenseFetchStatus then here is
//where to deal with it
//todo: key should hang off json data object as text version of key
//that way we can put other objects on the data return object
AyaNovaLicenseKey ParsedKey = Parse(RawTextKeyFromRockfish, log);
if (ParsedKey != null)
// string ResponseText = await ServiceProviderProvider.HttpClientFactory.CreateClient().GetStringAsync(sUrl);
var client = ServiceProviderProvider.HttpClientFactory.CreateClient();
var res = await client.GetAsync(sUrl);
if (res.IsSuccessStatusCode)
{
await InstallAsync(RawTextKeyFromRockfish, ParsedKey, apiServerState, ct, log);
var responseText = await res.Content.ReadAsStringAsync();
AyaNovaLicenseKey ParsedKey = Parse(responseText, log);
if (ParsedKey != null)
{
await InstallAsync(responseText, ParsedKey, apiServerState, ct, log);
return "ok";
}
return $"E1020 - Error fetching license key: No key was returned";
}
else
return $"E1020 - Error fetching license key: {res.ReasonPhrase}";
}
catch (Exception ex)
{
if (calledFromInternalJob) throw ex;
var msg = "E1020 - Error fetching license key";
log.LogError(ex, msg);
throw new ApplicationException(msg, ex);
return msg;
}
}