This commit is contained in:
2023-01-17 00:29:25 +00:00
parent 8ff9b40e1a
commit 3891b7990d
2 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
namespace Sockeye.Api.Controllers
{
[Route("fetch")]//Legacy v7 license fetch route
[Produces("application/json")]
public class FetchController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<FetchController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public FetchController(AyContext dbcontext, ILogger<FetchController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
//////////////////////////////////////////////////////////////////////////
//########### AyaNova 7.x License fetch route ############
//For legacy reasons route needs to be named /fetch
//////////////////////////////////////////////////////////////////////////
// GET: fetch/somecode/bob@bob.com
[HttpGet("{code}/{email}")]
public async Task<IActionResult> Get([FromRoute] string code, [FromRoute] string email)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var rec = await ct.License.SingleOrDefaultAsync(m => m.FetchCode == code.Trim() && m.FetchEmail == email.Trim().ToLowerInvariant() && m.FetchedOn == null);
if (rec == null)
{
//delay, could be someone fishing for a key, make it painful
//Have verified this is safe, won't affect other jobs on server
//happening concurrently or other requests to server
System.Threading.Thread.Sleep(10000);
return NotFound();
}
rec.FetchedOn = System.DateTime.UtcNow;
await ct.SaveChangesAsync();
return Ok(rec.Key);
}
//------------
}//eoc
}//eons

View File

@@ -0,0 +1,90 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
using System.ComponentModel.DataAnnotations;
namespace Sockeye.Api.Controllers
{
[Route("rvf")]//v8 fetch license route
[Produces("application/json")]
public class RvfController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<RvfController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public RvfController(AyContext dbcontext, ILogger<RvfController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
public class dtoFetchRequest
{
[Required]
public string DbId { get; set; }
}
//### RAVEN FETCH LICENSE REQUEST HERE ####
//This route is used by Raven to check if there is an unfetched license key and return it
[HttpPost]
public async Task<IActionResult> PostLicenseFetchRequest([FromBody] dtoFetchRequest fetchRequest)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
string FetchRequestDbId = fetchRequest.DbId;
string LicenseKey = null;
//Get the most recent key for this dbid regardless if fetched or not, most recent is most recent and supersedes any other keys
//for the same database id
//this means if a user requests a trial key then buys a key but both are unfetched the bought key takes precedence or vice versa
var license = await ct.License.OrderByDescending(z => z.Id).Where(z => z.DbId == FetchRequestDbId).FirstOrDefaultAsync();
if (license != null)
{
//Found a recent purchased key, if not fetched then can be sent
if (license.FetchedOn == null)
{
LicenseKey = license.Key;
license.FetchedOn = System.DateTime.UtcNow;
await ct.SaveChangesAsync();
}
}
if (LicenseKey == null)
{
return NotFound();
}
return Ok(new
{
data = new
{
key = LicenseKey
}
});
}
//------------
}//eoc
}//eons