[WIP] quick fix for login

This commit is contained in:
Kim 2023-03-16 08:51:22 +01:00
parent 4c37c92f73
commit d6248ead09
5 changed files with 99 additions and 96 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -40,7 +40,7 @@ public static partial class Db
var installation = Installations.First();
UserMethods.CreateAndSaveInstallationS3ApiKey(installation);
UserMethods.CreateAndSaveInstallationS3BuckitUrl(installation);
Observable.Interval(TimeSpan.FromDays(1))

View File

@ -14,7 +14,7 @@ public class Session : Relation<String, Int64>
[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<String, Int64>
return Convert.ToBase64String(token);
}
}

Binary file not shown.