diff --git a/csharp/App/Backend/DataTypes/Methods/Credentials.cs b/csharp/App/Backend/DataTypes/Methods/Credentials.cs index 06db0d78c..1a8e24703 100644 --- a/csharp/App/Backend/DataTypes/Methods/Credentials.cs +++ b/csharp/App/Backend/DataTypes/Methods/Credentials.cs @@ -8,7 +8,7 @@ public static class CredentialsMethods { public static Session? Login(this Credentials credentials) { - if (credentials.Username.IsNull() || credentials.Password.IsNull()) + if (credentials.Username.IsNullOrEmpty() || credentials.Password.IsNullOrEmpty()) return null; var user = Db.GetUserByEmail(credentials.Username); diff --git a/csharp/App/Backend/DataTypes/Methods/User.cs b/csharp/App/Backend/DataTypes/Methods/User.cs index 99988f7e8..16de6065e 100644 --- a/csharp/App/Backend/DataTypes/Methods/User.cs +++ b/csharp/App/Backend/DataTypes/Methods/User.cs @@ -3,6 +3,7 @@ using System.Net.Mail; using System.Security.Cryptography; using System.Text.Json.Nodes; using System.Text.RegularExpressions; +using CliWrap; using InnovEnergy.App.Backend.Database; using InnovEnergy.Lib.Utils; using Convert = System.Convert; @@ -176,38 +177,38 @@ public static class UserMethods } - private static Byte[] HmacSha256Digest(String message, String secret) - { - // var encoding = new UTF8Encoding(); - // var keyBytes = encoding.GetBytes(secret); - // var messageBytes = encoding.GetBytes(message); - // var cryptographer = new HMACSHA256(keyBytes); - // return cryptographer.ComputeHash(messageBytes); - - var keyBytes = UTF8.GetBytes(secret); - var messageBytes = UTF8.GetBytes(message); - - return HMACSHA256.HashData(keyBytes, messageBytes); - } - - private static String BuildSignature(String method, String path, String data, Int64 time, String secret) - { - var messageToSign = ""; - messageToSign += method + " /v2/" + path + "\n"; - messageToSign += data + "\n"; - - // query strings - messageToSign += "\n"; - // headers - messageToSign += "\n"; - - messageToSign += time; - - Console.WriteLine("Message to sign:\n" + messageToSign); - - var hmac = HmacSha256Digest(messageToSign, secret); - return Convert.ToBase64String(hmac); - } + // private static Byte[] HmacSha256Digest(String message, String secret) + // { + // // var encoding = new UTF8Encoding(); + // // var keyBytes = encoding.GetBytes(secret); + // // var messageBytes = encoding.GetBytes(message); + // // var cryptographer = new HMACSHA256(keyBytes); + // // return cryptographer.ComputeHash(messageBytes); + // + // var keyBytes = UTF8.GetBytes(secret); + // var messageBytes = UTF8.GetBytes(message); + // + // return HMACSHA256.HashData(keyBytes, messageBytes); + // } + // + // private static String BuildSignature(String method, String path, String data, Int64 time, String secret) + // { + // var messageToSign = ""; + // messageToSign += method + " /v2/" + path + "\n"; + // messageToSign += data + "\n"; + // + // // query strings + // messageToSign += "\n"; + // // headers + // messageToSign += "\n"; + // + // messageToSign += time; + // + // Console.WriteLine("Message to sign:\n" + messageToSign); + // + // var hmac = HmacSha256Digest(messageToSign, secret); + // return Convert.ToBase64String(hmac); + // } // public Object CreateAndSaveUserS3ApiKey(User user) // { @@ -260,67 +261,70 @@ public static class UserMethods // // } - public static Object CreateAndSaveInstallationS3ApiKey(Installation installation) + public static Object CreateAndSaveInstallationS3BuckitUrl(Installation installation) { - //EXOSCALE API URL - const String url = "https://api-ch-dk-2.exoscale.com/v2/"; - const String path = "access-key"; - - //TODO HIDE ME - const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0"; - const String apiKey = "EXOb98ec9008e3ec16e19d7b593"; - - - var jsonPayload = new JsonObject - { - ["name"] = installation.Id, - ["operations"] = new JsonArray - { - "list-sos-bucket", - "get-sos-object" - }, - ["content"] = new JsonArray - { - new JsonObject - { - ["domain"] = "sos", - ["resource-name"] = installation.Name, - ["resource-type"] = "bucket" - } - } - }; - - var stringPayload = jsonPayload.ToJsonString(); - - var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 60; - - var signature = BuildSignature("POST", path, stringPayload, unixExpiration, secret); - - var authHeader = "credential=" + apiKey + ",expires=" + unixExpiration + ",signature=" + signature; - - var client = new HttpClient(); - - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader); - - var content = new StringContent(stringPayload, UTF8, "application/json"); - - var response = client.PostAsync(url + path, content).Result; - - if (response.StatusCode.ToString() != "OK") - { - return response; - } - - var responseString = response.Content.ReadAsStringAsync().Result; - var newKey = Regex - .Match(responseString, "key\\\":\\\"([A-Z])\\w+") - .ToString() - .Split('"') - .Last(); - - installation.S3Key = newKey; - Db.Update(installation); - return newKey; + // //EXOSCALE API URL + // const String url = "https://api-ch-dk-2.exoscale.com/v2/"; + // const String path = "access-key"; + // + // //TODO HIDE ME + // const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0"; + // const String apiKey = "EXOb98ec9008e3ec16e19d7b593"; + // + // + // var jsonPayload = new JsonObject + // { + // ["name"] = installation.Id, + // ["operations"] = new JsonArray + // { + // "list-sos-bucket", + // "get-sos-object" + // }, + // ["content"] = new JsonArray + // { + // new JsonObject + // { + // ["domain"] = "sos", + // ["resource-name"] = installation.Name, + // ["resource-type"] = "bucket" + // } + // } + // }; + // + // var stringPayload = jsonPayload.ToJsonString(); + // + // var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 60; + // + // var signature = BuildSignature("POST", path, stringPayload, unixExpiration, secret); + // + // var authHeader = "credential=" + apiKey + ",expires=" + unixExpiration + ",signature=" + signature; + // + // var client = new HttpClient(); + // + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader); + // + // var content = new StringContent(stringPayload, UTF8, "application/json"); + // + // var response = client.PostAsync(url + path, content).Result; + // + // if (response.StatusCode.ToString() != "OK") + // { + // return response; + // } + // + // var responseString = response.Content.ReadAsStringAsync().Result; + // var newKey = Regex + // .Match(responseString, "key\\\":\\\"([A-Z])\\w+") + // .ToString() + // .Split('"') + // .Last(); + // Cli.Wrap(); + // + // + // installation.S3Key = newKey; + // Db.Update(installation); + // return newKey; + return 0; } diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 3e2fd815c..2fc111844 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -40,7 +40,7 @@ public static partial class Db var installation = Installations.First(); - UserMethods.CreateAndSaveInstallationS3ApiKey(installation); + UserMethods.CreateAndSaveInstallationS3BuckitUrl(installation); Observable.Interval(TimeSpan.FromDays(1)) diff --git a/csharp/App/Backend/Relations/Session.cs b/csharp/App/Backend/Relations/Session.cs index 016ffa0ce..fa9a993d4 100644 --- a/csharp/App/Backend/Relations/Session.cs +++ b/csharp/App/Backend/Relations/Session.cs @@ -14,7 +14,7 @@ public class Session : Relation [Indexed] public DateTime LastSeen { get; set; } [Ignore] public Boolean Valid => DateTime.Now - LastSeen < MaxAge - && !User.IsNull(); + && !User.Email.IsNullOrEmpty(); [Ignore] public User User => _User ??= Db.GetUserById(UserId)!; @@ -40,5 +40,4 @@ public class Session : Relation return Convert.ToBase64String(token); } - } \ No newline at end of file diff --git a/csharp/App/Backend/db.sqlite b/csharp/App/Backend/db.sqlite index 2522ae1e7..c4bf0e94d 100644 Binary files a/csharp/App/Backend/db.sqlite and b/csharp/App/Backend/db.sqlite differ