From 3891b7990da48c8bdc341aa81eb3a2e734033fee Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 17 Jan 2023 00:29:25 +0000 Subject: [PATCH] --- server/Controllers/FetchController.cs | 73 ++++++++++++++++++++++ server/Controllers/RvfController.cs | 90 +++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 server/Controllers/FetchController.cs create mode 100644 server/Controllers/RvfController.cs diff --git a/server/Controllers/FetchController.cs b/server/Controllers/FetchController.cs new file mode 100644 index 0000000..fb7683d --- /dev/null +++ b/server/Controllers/FetchController.cs @@ -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 log; + private readonly ApiServerState serverState; + + /// + /// ctor + /// + /// + /// + /// + public FetchController(AyContext dbcontext, ILogger 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 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 \ No newline at end of file diff --git a/server/Controllers/RvfController.cs b/server/Controllers/RvfController.cs new file mode 100644 index 0000000..93dfab8 --- /dev/null +++ b/server/Controllers/RvfController.cs @@ -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 log; + private readonly ApiServerState serverState; + + /// + /// ctor + /// + /// + /// + /// + public RvfController(AyContext dbcontext, ILogger 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 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 \ No newline at end of file