From 07e08aa5ce8654bac1dcf0939607626fd1687969 Mon Sep 17 00:00:00 2001 From: femsci Date: Fri, 3 Mar 2023 23:54:09 +0100 Subject: [PATCH] Initial MVP. --- .gitignore | 2 + .nyoki | 3 ++ AutoMFA.sln | 36 ++++++++++++++++ src/AutoMFA.Server/AutoMFA.Server.csproj | 15 +++++++ .../Controllers/DataController.cs | 39 ++++++++++++++++++ .../Controllers/ServiceController.cs | 18 ++++++++ src/AutoMFA.Server/Models/MessageModel.cs | 12 ++++++ src/AutoMFA.Server/Program.cs | 16 ++++++++ .../Properties/launchSettings.json | 41 +++++++++++++++++++ .../appsettings.Development.json | 8 ++++ src/AutoMFA.Server/appsettings.json | 9 ++++ test/AutoMFA.Test/AutoMFA.Test.csproj | 24 +++++++++++ test/AutoMFA.Test/RegexTest.cs | 19 +++++++++ test/AutoMFA.Test/Usings.cs | 1 + 14 files changed, 243 insertions(+) create mode 100644 .gitignore create mode 100644 .nyoki create mode 100644 AutoMFA.sln create mode 100644 src/AutoMFA.Server/AutoMFA.Server.csproj create mode 100644 src/AutoMFA.Server/Controllers/DataController.cs create mode 100644 src/AutoMFA.Server/Controllers/ServiceController.cs create mode 100644 src/AutoMFA.Server/Models/MessageModel.cs create mode 100644 src/AutoMFA.Server/Program.cs create mode 100644 src/AutoMFA.Server/Properties/launchSettings.json create mode 100644 src/AutoMFA.Server/appsettings.Development.json create mode 100644 src/AutoMFA.Server/appsettings.json create mode 100644 test/AutoMFA.Test/AutoMFA.Test.csproj create mode 100644 test/AutoMFA.Test/RegexTest.cs create mode 100644 test/AutoMFA.Test/Usings.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f181d71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/bin +**/obj diff --git a/.nyoki b/.nyoki new file mode 100644 index 0000000..3159982 --- /dev/null +++ b/.nyoki @@ -0,0 +1,3 @@ +{ + "instanceId": "de389c65-2419-4451-b350-a029cded3175" +} \ No newline at end of file diff --git a/AutoMFA.sln b/AutoMFA.sln new file mode 100644 index 0000000..e8e3d8d --- /dev/null +++ b/AutoMFA.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{BFFC3FE0-F692-4AE3-B895-C459E949089E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMFA.Server", "src\AutoMFA.Server\AutoMFA.Server.csproj", "{60293B32-7FBE-49DB-AE7F-C8B0933B8822}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F1A4AF86-865D-4B0F-A47C-A84F73565A41}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMFA.Test", "test\AutoMFA.Test\AutoMFA.Test.csproj", "{AEEDBEB2-14F9-4D50-83FD-774035CA00B0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {60293B32-7FBE-49DB-AE7F-C8B0933B8822}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60293B32-7FBE-49DB-AE7F-C8B0933B8822}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60293B32-7FBE-49DB-AE7F-C8B0933B8822}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60293B32-7FBE-49DB-AE7F-C8B0933B8822}.Release|Any CPU.Build.0 = Release|Any CPU + {AEEDBEB2-14F9-4D50-83FD-774035CA00B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AEEDBEB2-14F9-4D50-83FD-774035CA00B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AEEDBEB2-14F9-4D50-83FD-774035CA00B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AEEDBEB2-14F9-4D50-83FD-774035CA00B0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {60293B32-7FBE-49DB-AE7F-C8B0933B8822} = {BFFC3FE0-F692-4AE3-B895-C459E949089E} + {AEEDBEB2-14F9-4D50-83FD-774035CA00B0} = {F1A4AF86-865D-4B0F-A47C-A84F73565A41} + EndGlobalSection +EndGlobal diff --git a/src/AutoMFA.Server/AutoMFA.Server.csproj b/src/AutoMFA.Server/AutoMFA.Server.csproj new file mode 100644 index 0000000..e08a89b --- /dev/null +++ b/src/AutoMFA.Server/AutoMFA.Server.csproj @@ -0,0 +1,15 @@ + + + + net7.0 + enable + enable + + + + + + + + + diff --git a/src/AutoMFA.Server/Controllers/DataController.cs b/src/AutoMFA.Server/Controllers/DataController.cs new file mode 100644 index 0000000..c886265 --- /dev/null +++ b/src/AutoMFA.Server/Controllers/DataController.cs @@ -0,0 +1,39 @@ +using System.Text.RegularExpressions; +using AutoMFA.Server.Models; +using Microsoft.AspNetCore.Mvc; +using TextCopy; + +namespace AutoMFA.Server.Controllers; + +[ApiController] +[Route("/data")] +public partial class DataController : ControllerBase +{ + private readonly ILogger _logger; + private readonly IClipboard _clipboard; + + public DataController(ILogger logger, IClipboard clipboard) + { + _logger = logger; + _clipboard = clipboard; + } + + [HttpPost("publish")] + public async Task Publish([FromBody] MessageModel model) + { + _logger.LogInformation("SMS at {}", model.Timestamp); + + string msg = model.MessageRaw; + var match = _mBankRegex().Match(msg); + if (match.Success) + { + long token = long.Parse(match.Groups[1].Value); + await _clipboard.SetTextAsync(token.ToString()); + } + + return Ok(); + } + + [GeneratedRegex("haslo: (\\d{8})")] + private static partial Regex _mBankRegex(); +} diff --git a/src/AutoMFA.Server/Controllers/ServiceController.cs b/src/AutoMFA.Server/Controllers/ServiceController.cs new file mode 100644 index 0000000..4acc93b --- /dev/null +++ b/src/AutoMFA.Server/Controllers/ServiceController.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace AutoMFA.Server.Controllers; + +[ApiController] +[Route("/")] +public class ServiceController : ControllerBase +{ + [HttpGet("ping")] + public IActionResult Ping() + { + return Ok(); + } +} diff --git a/src/AutoMFA.Server/Models/MessageModel.cs b/src/AutoMFA.Server/Models/MessageModel.cs new file mode 100644 index 0000000..c2bc393 --- /dev/null +++ b/src/AutoMFA.Server/Models/MessageModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AutoMFA.Server.Models; + +public class MessageModel +{ + public long Timestamp { get; set; } + public string MessageRaw { get; set; } = null!; +} diff --git a/src/AutoMFA.Server/Program.cs b/src/AutoMFA.Server/Program.cs new file mode 100644 index 0000000..886db9b --- /dev/null +++ b/src/AutoMFA.Server/Program.cs @@ -0,0 +1,16 @@ +using TextCopy; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); +builder.Services.InjectClipboard(); + +var app = builder.Build(); + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/src/AutoMFA.Server/Properties/launchSettings.json b/src/AutoMFA.Server/Properties/launchSettings.json new file mode 100644 index 0000000..c7c803a --- /dev/null +++ b/src/AutoMFA.Server/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49705", + "sslPort": 44307 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://0.0.0.0:8080", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7241;http://localhost:5107", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/AutoMFA.Server/appsettings.Development.json b/src/AutoMFA.Server/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/src/AutoMFA.Server/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/AutoMFA.Server/appsettings.json b/src/AutoMFA.Server/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/src/AutoMFA.Server/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/test/AutoMFA.Test/AutoMFA.Test.csproj b/test/AutoMFA.Test/AutoMFA.Test.csproj new file mode 100644 index 0000000..86a36ef --- /dev/null +++ b/test/AutoMFA.Test/AutoMFA.Test.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/test/AutoMFA.Test/RegexTest.cs b/test/AutoMFA.Test/RegexTest.cs new file mode 100644 index 0000000..9a57319 --- /dev/null +++ b/test/AutoMFA.Test/RegexTest.cs @@ -0,0 +1,19 @@ +using System.Text.RegularExpressions; + +namespace AutoMFA.Test; + +public partial class RegexTests +{ + [Fact] + public void MBankRegex() + { + var msg = "58 PLN haslo: 21372137 mBank"; + var match = mBank().Match(msg); + + Assert.True(match.Success); + Assert.Equal("21372137", match.Groups[1].Value); + } + + [GeneratedRegex("haslo: (\\d{8})")] + private static partial Regex mBank(); +} diff --git a/test/AutoMFA.Test/Usings.cs b/test/AutoMFA.Test/Usings.cs new file mode 100644 index 0000000..c802f44 --- /dev/null +++ b/test/AutoMFA.Test/Usings.cs @@ -0,0 +1 @@ +global using Xunit;