remove S3 signature building stuff
This commit is contained in:
parent
405308d7d6
commit
7cf27a78bc
|
@ -175,154 +175,7 @@ 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);
|
||||
}
|
||||
|
||||
// public Object CreateAndSaveUserS3ApiKey(User user)
|
||||
// {
|
||||
// //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 installationList = User2Installation
|
||||
// .Where(i => i.UserId == user.Id)
|
||||
// .SelectMany(i => Installations.Where(f => i.InstallationId == f.Id))
|
||||
// .ToList();
|
||||
//
|
||||
//
|
||||
// var instList = new JsonArray();
|
||||
//
|
||||
// foreach (var installation in installationList)
|
||||
// {
|
||||
// instList.Add(new JsonObject {["domain"] = "sos",["resource-name"] = installation.Name,["resource-type"] = "bucket"});
|
||||
// }
|
||||
//
|
||||
// var jsonPayload = new JsonObject { ["name"] = user.Email, ["operations"] = new JsonArray{ "list-sos-bucket", "get-sos-object" }, ["content"] = instList};
|
||||
// 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, Encoding.UTF8, "application/json");
|
||||
//
|
||||
//
|
||||
// var response = client.PostAsync(url+path, content).Result;
|
||||
//
|
||||
// if (response.StatusCode.ToString() != "OK")
|
||||
// {
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// var responseString = response.Content.ReadAsStringAsync().Result;
|
||||
// return Enumerable.Last(Regex.Match(responseString, "key\\\":\\\"([A-Z])\\w+").ToString().Split('"'));
|
||||
// // return SetUserS3ApiKey(user, newKey);
|
||||
//
|
||||
// }
|
||||
|
||||
public static Object CreateAndSaveInstallationS3ApiKey(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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TODO
|
||||
private static Boolean IsValidEmail(String email)
|
||||
|
|
Loading…
Reference in New Issue