From 4e5095d46c1b6b30c4ad542714847407a0575ab4 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Sun, 28 Aug 2022 22:45:12 +0000 Subject: [PATCH] Add webhook for semi-automatic shareit order notification processing --- Controllers/OrderController.cs | 77 +++++++++++++++++++ util/AutoOrderProcessingUtils.cs | 31 ++++++++ wwwroot/js/app.rfsettings.js | 15 +++- .../js/templates/app.rfsettings.handlebars | 4 + 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Controllers/OrderController.cs create mode 100644 util/AutoOrderProcessingUtils.cs diff --git a/Controllers/OrderController.cs b/Controllers/OrderController.cs new file mode 100644 index 0000000..98e06f6 --- /dev/null +++ b/Controllers/OrderController.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.EntityFrameworkCore; +using rockfishCore.Models; +using rockfishCore.Util; +using System.ComponentModel.DataAnnotations; +using Newtonsoft.Json.Linq; + +namespace rockfishCore.Controllers +{ + //### OUR ROUTE CALLED FROM ROCKFISH CLIENT #### + [Produces("application/json")] + [Route("api/order")] + [Authorize] + public class OrderController : Controller + { + private readonly rockfishContext ct; + + + public OrderController(rockfishContext context) + { + ct = context; + } + + + + + //Receive an order notification from ShareIt + [HttpPost("shareit")] + public async Task Post([FromHeader] string Authorization, [FromBody] JObject j) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + try + { + //do stuff with the notification + (string username, string password) = rockfishCore.Util.AutoOrderProcessingUtil.GetUsernameAndPasswordFromAuthorizeHeader(Authorization); + // Now use username and password with whatever authentication process you want + if (username == "Y24PYYDQSA1L12905N5MKU" && password == "GI2F7CP17C2JS872MHASAF") + { + //put the notification into the db as freeform notification information + //to be processed by other code later. i.e. just capture it as is cleanly and don't bother trying to do anything fancy here this should be tight and focused and side effect free + + + } + + + } + catch (Exception ex) + { + //todo Log this here + System.Diagnostics.Debug.WriteLine($"order/shareit - Exception processing request: {ex.Message}"); + } + + return NoContent(); + + } + + + [HttpGet("list/{siteId}")] + public async Task GetList([FromRoute] long siteId) + { + return Ok(await ct.License.AsNoTracking().Where(z => z.SiteId == siteId).OrderByDescending(z => z.Id).ToListAsync()); + } + + + + + }//eoc +}//eons \ No newline at end of file diff --git a/util/AutoOrderProcessingUtils.cs b/util/AutoOrderProcessingUtils.cs new file mode 100644 index 0000000..3181b58 --- /dev/null +++ b/util/AutoOrderProcessingUtils.cs @@ -0,0 +1,31 @@ +using System; +using System.Text; + + + +namespace rockfishCore.Util +{ + //Import fogbugz stuff into our own db + //called by schema update 7 + //does not modify FB at all + //can be called any time, won't re-import + public static class AutoOrderProcessingUtil + { + // static System.Text.Encoding ISO_8859_1_ENCODING = System.Text.Encoding.GetEncoding("ISO-8859-1"); + static Encoding ISO_8859_1_ENCODING = System.Text.Encoding.GetEncoding("ISO-8859-1"); + public static (string, string) GetUsernameAndPasswordFromAuthorizeHeader(string authorizeHeader) + { + if (authorizeHeader == null || !authorizeHeader.Contains("Basic ")) + return (null, null); + + string encodedUsernamePassword = authorizeHeader.Substring("Basic ".Length).Trim(); + string usernamePassword = ISO_8859_1_ENCODING.GetString(Convert.FromBase64String(encodedUsernamePassword)); + + string username = usernamePassword.Split(':')[0]; + string password = usernamePassword.Split(':')[1]; + + return (username, password); + } + + }//eoc +}//eons \ No newline at end of file diff --git a/wwwroot/js/app.rfsettings.js b/wwwroot/js/app.rfsettings.js index c2ca588..8d0f48f 100644 --- a/wwwroot/js/app.rfsettings.js +++ b/wwwroot/js/app.rfsettings.js @@ -13,7 +13,8 @@ app.rfsettings = (function() { var stateMap = {}, configModule, onChangePassword, - initModule; + initModule, + onTest; //----------------- END MODULE SCOPE VARIABLES --------------- //------------------- BEGIN UTILITY METHODS ------------------ @@ -48,6 +49,17 @@ app.rfsettings = (function() { return false; //prevent default? }; + + + /////////////////////////////// + //ONTEST + // + onTest = function(event) { + event.preventDefault(); + + + return false; //prevent default? + }; //-------------------- END EVENT HANDLERS -------------------- //------------------- BEGIN PUBLIC METHODS ------------------- @@ -70,6 +82,7 @@ app.rfsettings = (function() { // bind actions $("#btn-change-password").bind("click", onChangePassword); + $("#btn-test").bind("click", onTest); //Context menu app.nav.contextClear(); diff --git a/wwwroot/js/templates/app.rfsettings.handlebars b/wwwroot/js/templates/app.rfsettings.handlebars index e753cb0..7fa640d 100644 --- a/wwwroot/js/templates/app.rfsettings.handlebars +++ b/wwwroot/js/templates/app.rfsettings.handlebars @@ -10,6 +10,10 @@
+ +
+ +
\ No newline at end of file