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] [HttpPost]
public async Task<IActionResult> FetchLicense() public async Task<IActionResult> FetchLicense()
{ {
if (serverState.IsClosed) if (serverState.IsClosed)
{ {
//Exception for SuperUser account to handle licensing issues //Exception for SuperUser account to handle licensing issues
if (UserIdFromContext.Id(HttpContext.Items) != 1) if (UserIdFromContext.Id(HttpContext.Items) != 1)
@@ -116,7 +116,11 @@ namespace AyaNova.Api.Controllers
try 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) catch (Exception ex)
{ {
@@ -126,7 +130,6 @@ namespace AyaNova.Api.Controllers
rootex = rootex.InnerException; rootex = rootex.InnerException;
} }
if (rootex.Message.Contains("E1020")) if (rootex.Message.Contains("E1020"))
{ {
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "LICENSE_KEY", rootex.Message)); 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")] [HttpPost("permanently-erase-all-data")]
public async Task<IActionResult> RemoveAllData([FromBody] string acceptCode) public async Task<IActionResult> RemoveAllData([FromBody] string acceptCode)
{ {
if (serverState.IsClosed) if (serverState.IsClosed)
{ {
//Exception for SuperUser account to handle licensing issues //Exception for SuperUser account to handle licensing issues
if (UserIdFromContext.Id(HttpContext.Items) != 1) if (UserIdFromContext.Id(HttpContext.Items) != 1)

View File

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