[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) public static Session? Login(this Credentials credentials)
{ {
if (credentials.Username.IsNull() || credentials.Password.IsNull()) if (credentials.Username.IsNullOrEmpty() || credentials.Password.IsNullOrEmpty())
return null; return null;
var user = Db.GetUserByEmail(credentials.Username); var user = Db.GetUserByEmail(credentials.Username);

View File

@ -3,6 +3,7 @@ using System.Net.Mail;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using CliWrap;
using InnovEnergy.App.Backend.Database; using InnovEnergy.App.Backend.Database;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Convert = System.Convert; using Convert = System.Convert;
@ -176,38 +177,38 @@ public static class UserMethods
} }
private static Byte[] HmacSha256Digest(String message, String secret) // private static Byte[] HmacSha256Digest(String message, String secret)
{ // {
// var encoding = new UTF8Encoding(); // // var encoding = new UTF8Encoding();
// var keyBytes = encoding.GetBytes(secret); // // var keyBytes = encoding.GetBytes(secret);
// var messageBytes = encoding.GetBytes(message); // // var messageBytes = encoding.GetBytes(message);
// var cryptographer = new HMACSHA256(keyBytes); // // var cryptographer = new HMACSHA256(keyBytes);
// return cryptographer.ComputeHash(messageBytes); // // return cryptographer.ComputeHash(messageBytes);
//
var keyBytes = UTF8.GetBytes(secret); // var keyBytes = UTF8.GetBytes(secret);
var messageBytes = UTF8.GetBytes(message); // var messageBytes = UTF8.GetBytes(message);
//
return HMACSHA256.HashData(keyBytes, messageBytes); // return HMACSHA256.HashData(keyBytes, messageBytes);
} // }
//
private static String BuildSignature(String method, String path, String data, Int64 time, String secret) // private static String BuildSignature(String method, String path, String data, Int64 time, String secret)
{ // {
var messageToSign = ""; // var messageToSign = "";
messageToSign += method + " /v2/" + path + "\n"; // messageToSign += method + " /v2/" + path + "\n";
messageToSign += data + "\n"; // messageToSign += data + "\n";
//
// query strings // // query strings
messageToSign += "\n"; // messageToSign += "\n";
// headers // // headers
messageToSign += "\n"; // messageToSign += "\n";
//
messageToSign += time; // messageToSign += time;
//
Console.WriteLine("Message to sign:\n" + messageToSign); // Console.WriteLine("Message to sign:\n" + messageToSign);
//
var hmac = HmacSha256Digest(messageToSign, secret); // var hmac = HmacSha256Digest(messageToSign, secret);
return Convert.ToBase64String(hmac); // return Convert.ToBase64String(hmac);
} // }
// public Object CreateAndSaveUserS3ApiKey(User user) // 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 // //EXOSCALE API URL
const String url = "https://api-ch-dk-2.exoscale.com/v2/"; // const String url = "https://api-ch-dk-2.exoscale.com/v2/";
const String path = "access-key"; // const String path = "access-key";
//
//TODO HIDE ME // //TODO HIDE ME
const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0"; // const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0";
const String apiKey = "EXOb98ec9008e3ec16e19d7b593"; // const String apiKey = "EXOb98ec9008e3ec16e19d7b593";
//
//
var jsonPayload = new JsonObject // var jsonPayload = new JsonObject
{ // {
["name"] = installation.Id, // ["name"] = installation.Id,
["operations"] = new JsonArray // ["operations"] = new JsonArray
{ // {
"list-sos-bucket", // "list-sos-bucket",
"get-sos-object" // "get-sos-object"
}, // },
["content"] = new JsonArray // ["content"] = new JsonArray
{ // {
new JsonObject // new JsonObject
{ // {
["domain"] = "sos", // ["domain"] = "sos",
["resource-name"] = installation.Name, // ["resource-name"] = installation.Name,
["resource-type"] = "bucket" // ["resource-type"] = "bucket"
} // }
} // }
}; // };
//
var stringPayload = jsonPayload.ToJsonString(); // var stringPayload = jsonPayload.ToJsonString();
//
var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 60; // var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 60;
//
var signature = BuildSignature("POST", path, stringPayload, unixExpiration, secret); // var signature = BuildSignature("POST", path, stringPayload, unixExpiration, secret);
//
var authHeader = "credential=" + apiKey + ",expires=" + unixExpiration + ",signature=" + signature; // var authHeader = "credential=" + apiKey + ",expires=" + unixExpiration + ",signature=" + signature;
//
var client = new HttpClient(); // var client = new HttpClient();
//
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader); // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader);
//
var content = new StringContent(stringPayload, UTF8, "application/json"); // var content = new StringContent(stringPayload, UTF8, "application/json");
//
var response = client.PostAsync(url + path, content).Result; // var response = client.PostAsync(url + path, content).Result;
//
if (response.StatusCode.ToString() != "OK") // if (response.StatusCode.ToString() != "OK")
{ // {
return response; // return response;
} // }
//
var responseString = response.Content.ReadAsStringAsync().Result; // var responseString = response.Content.ReadAsStringAsync().Result;
var newKey = Regex // var newKey = Regex
.Match(responseString, "key\\\":\\\"([A-Z])\\w+") // .Match(responseString, "key\\\":\\\"([A-Z])\\w+")
.ToString() // .ToString()
.Split('"') // .Split('"')
.Last(); // .Last();
// Cli.Wrap();
installation.S3Key = newKey; //
Db.Update(installation); //
return newKey; // 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(); var installation = Installations.First();
UserMethods.CreateAndSaveInstallationS3ApiKey(installation); UserMethods.CreateAndSaveInstallationS3BuckitUrl(installation);
Observable.Interval(TimeSpan.FromDays(1)) Observable.Interval(TimeSpan.FromDays(1))

View File

@ -14,7 +14,7 @@ public class Session : Relation<String, Int64>
[Indexed] public DateTime LastSeen { get; set; } [Indexed] public DateTime LastSeen { get; set; }
[Ignore] public Boolean Valid => DateTime.Now - LastSeen < MaxAge [Ignore] public Boolean Valid => DateTime.Now - LastSeen < MaxAge
&& !User.IsNull(); && !User.Email.IsNullOrEmpty();
[Ignore] public User User => _User ??= Db.GetUserById(UserId)!; [Ignore] public User User => _User ??= Db.GetUserById(UserId)!;
@ -40,5 +40,4 @@ public class Session : Relation<String, Int64>
return Convert.ToBase64String(token); return Convert.ToBase64String(token);
} }
} }

Binary file not shown.