From f5fcb3a9b1af49b197926d7ff9f57d4991d35506 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Mar 2023 21:40:06 +0100 Subject: [PATCH] Poprawki --- enovaApi.Proxy/ApiKey.cs | 14 ++++++++- enovaApi.Proxy/Controllers/ApiController.cs | 4 +-- enovaApi.Proxy/Controllers/KeysController.cs | 30 ++++++++++++++------ enovaApi.Proxy/Cryptography.cs | 30 ++++++++++++++++++-- enovaApi.Proxy/Program.cs | 15 ++++++++-- enovaApi.Proxy/appsettings.json | 4 ++- enovaApi.Proxy/enovaApi.Proxy.csproj | 7 +++-- enovaApi.Proxy/keys.json | 4 +-- 8 files changed, 87 insertions(+), 21 deletions(-) diff --git a/enovaApi.Proxy/ApiKey.cs b/enovaApi.Proxy/ApiKey.cs index 220465f..a6feff0 100644 --- a/enovaApi.Proxy/ApiKey.cs +++ b/enovaApi.Proxy/ApiKey.cs @@ -1,11 +1,23 @@ -namespace enovaApi.Proxy +using System.Security.Cryptography; + +namespace enovaApi.Proxy { public class ApiKey { + public ApiKey() + { + GenerateKey(); + } + public string Key { get; set; } = string.Empty; public string Operator { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; + public void GenerateKey() + { + Key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)); + } + public override string ToString() { return $"{Operator} has key {Key}"; diff --git a/enovaApi.Proxy/Controllers/ApiController.cs b/enovaApi.Proxy/Controllers/ApiController.cs index 87aa133..7aa1749 100644 --- a/enovaApi.Proxy/Controllers/ApiController.cs +++ b/enovaApi.Proxy/Controllers/ApiController.cs @@ -28,9 +28,9 @@ public async Task Post() { var body = await Request.Body.GetString(); var jObject = JObject.Parse(body); - if (jObject["ApiKey"] != null) + var key = jObject["ApiKey"]; + if (key != null) { - var key = jObject["ApiKey"]; jObject.Remove("ApiKey"); jObject.Remove("Operator"); jObject.Remove("Password"); diff --git a/enovaApi.Proxy/Controllers/KeysController.cs b/enovaApi.Proxy/Controllers/KeysController.cs index c73575f..11c788b 100644 --- a/enovaApi.Proxy/Controllers/KeysController.cs +++ b/enovaApi.Proxy/Controllers/KeysController.cs @@ -16,19 +16,33 @@ public KeysController(IConfiguration configuration) private readonly Keys keys; [HttpPost] - public string CreateKey(string oper, string password) + public string CreateKey(string oper, string password, bool regenerate) { - var key = new ApiKey() + ApiKey apiKey = keys.ApiKeys.FirstOrDefault(x => x.Operator == oper); + if (apiKey != null) { - Key = Guid.NewGuid().ToString("N"), - Operator = oper, - Password = Cryptography.Encrypt(password) - }; - keys.ApiKeys.Add(key); + if (regenerate) + { + apiKey.GenerateKey(); + } + else + { + return new JObject(new JProperty("Message", $"Api key for {oper} was generated. Set regenerate parameter to true to regenerate key.")).ToString(); + } + } + else + { + apiKey = new ApiKey() + { + Operator = oper, + Password = Cryptography.Encrypt(password) + }; + keys.ApiKeys.Add(apiKey); + } System.IO.File.WriteAllText("keys.json", JObject.FromObject(keys).ToString(), Encoding.UTF8); - return key.Key; + return new JObject(new JProperty("Key", apiKey.Key)).ToString(); } } } diff --git a/enovaApi.Proxy/Cryptography.cs b/enovaApi.Proxy/Cryptography.cs index ae6e092..04783f1 100644 --- a/enovaApi.Proxy/Cryptography.cs +++ b/enovaApi.Proxy/Cryptography.cs @@ -7,14 +7,40 @@ public static class Cryptography { private static Aes Aes { get; } = Aes.Create(); + internal static void Configure() + { + Aes.BlockSize = 128; + Aes.KeySize = 256; + } + public static void SetIV(string iv) { - Aes.IV = Convert.FromBase64String(iv); + byte[] encodedIv; + try + { + encodedIv = Convert.FromBase64String(iv); + } + catch + { + // add some log + throw; + } + Aes.IV = encodedIv; } public static void SetKey(string key) { - Aes.Key = Convert.FromBase64String(key); + byte[] encodedKey; + try + { + encodedKey = Convert.FromBase64String(key); + } + catch + { + // add some log + throw; + } + Aes.Key = encodedKey; } public static string Decrypt(string base64cipher) diff --git a/enovaApi.Proxy/Program.cs b/enovaApi.Proxy/Program.cs index 6d9eb5e..f40ea96 100644 --- a/enovaApi.Proxy/Program.cs +++ b/enovaApi.Proxy/Program.cs @@ -1,4 +1,13 @@ using enovaApi.Proxy; +using Microsoft.Extensions.Configuration; + +Cryptography.Configure(); +if (args.Contains("--generateCrypto")) +{ + Console.WriteLine($"IV: {Cryptography.GetIV()}"); + Console.WriteLine($"Key: {Cryptography.GetKey()}"); + Environment.Exit(0); +} var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -8,6 +17,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); + builder.WebHost.UseKestrel(); if (args.Any(x => x.StartsWith("--urls"))) { @@ -24,9 +34,10 @@ app.UseSwaggerUI(); } + // key and iv can be changed by Cryptography.GetIV() and Cryptography.GetKey() then replaced below -Cryptography.SetIV("WfEwqyJ4Pm3b0F6nnrLLKQ=="); -Cryptography.SetKey("BJaVbPFXNBN59bZCn1ORpKTH7b7UrJ4zj7KFBrHMaSk="); +Cryptography.SetIV(builder.Configuration.GetValue("IV") ?? throw new Exception("IV value cannot be null.")); +Cryptography.SetKey(builder.Configuration.GetValue("Key") ?? throw new Exception("Key value cannot be null")); app.UseHttpsRedirection(); diff --git a/enovaApi.Proxy/appsettings.json b/enovaApi.Proxy/appsettings.json index 3604944..2b5f2e0 100644 --- a/enovaApi.Proxy/appsettings.json +++ b/enovaApi.Proxy/appsettings.json @@ -6,5 +6,7 @@ } }, "AllowedHosts": "*", - "enovaUrl": "http://localhost:5000/" + "enovaUrl": "http://localhost:5000/", + "IV": "WfEwqyJ4Pm3b0F6nnrLLKQ==", + "Key": "BJaVbPFXNBN59bZCn1ORpKTH7b7UrJ4zj7KFBrHMaSk=" } diff --git a/enovaApi.Proxy/enovaApi.Proxy.csproj b/enovaApi.Proxy/enovaApi.Proxy.csproj index 10c13c9..a42d38e 100644 --- a/enovaApi.Proxy/enovaApi.Proxy.csproj +++ b/enovaApi.Proxy/enovaApi.Proxy.csproj @@ -5,12 +5,13 @@ enable enable Krzysztof Krupa + 1.0.1 - - - + + + diff --git a/enovaApi.Proxy/keys.json b/enovaApi.Proxy/keys.json index 5f5b17f..1da3327 100644 --- a/enovaApi.Proxy/keys.json +++ b/enovaApi.Proxy/keys.json @@ -1,9 +1,9 @@ { "ApiKeys": [ { - "Key": "57cc175a465348149e7b1e1ef8c347a2", + "Key": "oK/7zSEXoy2ItM6Z+PPDac+dgyOmWD/9ITQHbu5hHU8=", "Operator": "webapi", - "Password": "3Bqb4znDxeB0rJfguEDsdHKTAgRqQ0NzyL3GZk/j1Cw=" + "Password": "RD7M5Y9f4EQAkNxp0w1FUg==" } ] } \ No newline at end of file